]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/64/unix/unix.factor
Solution to Project Euler problem 65
[factor.git] / basis / cpu / x86 / 64 / unix / unix.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays sequences math splitting make assocs kernel
4 layouts system alien.c-types cpu.architecture
5 cpu.x86.assembler cpu.x86.assembler.operands cpu.x86 compiler.codegen
6 compiler.cfg.registers ;
7 QUALIFIED: alien.structs
8 QUALIFIED: classes.struct
9 IN: cpu.x86.64.unix
10
11 M: int-regs param-regs
12     drop { RDI RSI RDX RCX R8 R9 } ;
13
14 M: float-regs param-regs
15     drop { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 } ;
16
17 M: x86.64 reserved-area-size 0 ;
18
19 SYMBOL: (stack-value)
20 ! The ABI for passing structs by value is pretty great
21 << void* c-type clone \ (stack-value) define-primitive-type
22 stack-params \ (stack-value) c-type (>>rep) >>
23
24 : struct-types&offset ( struct-type -- pairs )
25     fields>> [
26         [ type>> ] [ offset>> ] bi 2array
27     ] map ;
28
29 : split-struct ( pairs -- seq )
30     [
31         [ 8 mod zero? [ t , ] when , ] assoc-each
32     ] { } make { t } split harvest ;
33
34 : flatten-small-struct ( c-type -- seq )
35     struct-types&offset split-struct [
36         [ c-type c-type-rep reg-class-of ] map
37         int-regs swap member? void* double ? c-type
38     ] map ;
39
40 : flatten-large-struct ( c-type -- seq )
41     heap-size cell align
42     cell /i \ (stack-value) c-type <repetition> ;
43
44 : flatten-struct ( c-type -- seq )
45     dup heap-size 16 > [
46         flatten-large-struct
47     ] [
48         flatten-small-struct
49     ] if ;
50
51 M: alien.structs:struct-type flatten-value-type ( type -- seq )
52     flatten-struct ;
53 M: classes.struct:struct-c-type flatten-value-type ( type -- seq )
54     flatten-struct ;
55
56 M: x86.64 return-struct-in-registers? ( c-type -- ? )
57     heap-size 2 cells <= ;
58
59 M: x86.64 dummy-stack-params? f ;
60
61 M: x86.64 dummy-int-params? f ;
62
63 M: x86.64 dummy-fp-params? f ;
64
65 M: x86.64 temp-reg R8 ;