1 USING: accessors arrays combinators kernel llvm.core
2 locals math.parser math multiline
3 namespaces parser peg.ebnf sequences
4 sequences.deep specialized-arrays.alien strings vocabs words ;
8 ! Type resolution strategy:
10 ! create the type with uprefs mapped to opaque types
11 ! cache typerefs in enclosing types for pass 2
12 ! if our type is concrete, then we are done
15 ! wrap our abstract type in a type handle
16 ! create a second type, using the cached enclosing type info
17 ! resolve the first type to the second
19 GENERIC: (>tref) ( type -- LLVMTypeRef )
20 GENERIC: ((tref>)) ( LLVMTypeRef type -- type )
21 GENERIC: c-type ( type -- str )
23 ! default implementation for simple types
24 M: object ((tref>)) nip ;
25 : unsupported-type ( -- )
26 "cannot generate c-type: unsupported llvm type" throw ;
27 M: object c-type unsupported-type ;
32 M: integer (>tref) size>> LLVMIntType ;
33 M: integer ((tref>)) swap LLVMGetIntTypeWidth >>size ;
34 M: integer c-type size>> {
42 SINGLETONS: float double x86_fp80 fp128 ppc_fp128 ;
44 M: float (>tref) drop LLVMFloatType ;
45 M: double (>tref) drop LLVMDoubleType ;
46 M: double c-type drop "double" ;
47 M: x86_fp80 (>tref) drop LLVMX86FP80Type ;
48 M: fp128 (>tref) drop LLVMFP128Type ;
49 M: ppc_fp128 (>tref) drop LLVMPPCFP128Type ;
51 SINGLETONS: opaque label void metadata ;
53 M: opaque (>tref) drop LLVMOpaqueType ;
54 M: label (>tref) drop LLVMLabelType ;
55 M: void (>tref) drop LLVMVoidType ;
56 M: void c-type drop "void" ;
57 M: metadata (>tref) drop
58 "metadata types unsupported by llvm c bindings" throw ;
60 ! enclosing types cache their llvm refs during
61 ! the first pass, used in the second pass to
63 TUPLE: enclosing cached ;
65 GENERIC: clean ( type -- )
66 GENERIC: clean* ( type -- )
67 M: object clean drop ;
68 M: enclosing clean f >>cached clean* ;
70 ! builds the stack of types that uprefs need to refer to
72 :: push-type ( type quot: ( type -- LLVMTypeRef ) -- LLVMTypeRef )
74 type quot call( type -- LLVMTypeRef )
75 types get pop over >>cached drop ;
78 :: push-ref ( ref quot: ( LLVMTypeRef -- type ) -- type )
80 [ types get length swap - <up-ref> ] [
82 ref quot call( LLVMTypeRef -- type )
86 GENERIC: (>tref)* ( type -- LLVMTypeRef )
87 M: enclosing (>tref) [ (>tref)* ] push-type ;
90 GENERIC: (tref>)* ( LLVMTypeRef type -- type )
91 M: enclosing ((tref>)) [ (tref>)* ] curry push-ref ;
93 : (tref>) ( LLVMTypeRef -- type ) dup type-kind ((tref>)) ;
95 TUPLE: pointer < enclosing type ;
96 : <pointer> ( t -- o ) pointer new swap >>type ;
98 M: pointer (>tref)* type>> (>tref) 0 LLVMPointerType ;
99 M: pointer clean* type>> clean ;
100 M: pointer (tref>)* swap LLVMGetElementType (tref>) >>type ;
101 M: pointer c-type type>> 8 <integer> = "char*" "void*" ? ;
103 TUPLE: vector < enclosing size type ;
104 : <vector> ( s t -- o )
106 swap >>type swap >>size ;
108 M: vector (>tref)* [ type>> (>tref) ] [ size>> ] bi LLVMVectorType ;
109 M: vector clean* type>> clean ;
111 over LLVMGetElementType (tref>) >>type
112 swap LLVMGetVectorSize >>size ;
114 TUPLE: struct < enclosing types packed? ;
115 : <struct> ( ts p? -- o )
117 swap >>packed? swap >>types ;
120 [ types>> [ (>tref) ] map >void*-array ]
122 [ packed?>> 1 0 ? ] tri LLVMStructType ;
123 M: struct clean* types>> [ clean ] each ;
125 over LLVMIsPackedStruct 0 = not >>packed?
126 swap dup LLVMCountStructElementTypes <void*-array>
127 [ LLVMGetStructElementTypes ] keep >array
128 [ (tref>) ] map >>types ;
130 TUPLE: array < enclosing size type ;
131 : <array> ( s t -- o )
133 swap >>type swap >>size ;
135 M: array (>tref)* [ type>> (>tref) ] [ size>> ] bi LLVMArrayType ;
136 M: array clean* type>> clean ;
138 over LLVMGetElementType (tref>) >>type
139 swap LLVMGetArrayLength >>size ;
142 TUPLE: function < enclosing return params vararg? ;
143 : <function> ( ret params var? -- o )
145 swap >>vararg? swap >>params swap >>return ;
147 M: function (>tref)* {
149 [ params>> [ (>tref) ] map >void*-array ]
152 } cleave LLVMFunctionType ;
153 M: function clean* [ return>> clean ] [ params>> [ clean ] each ] bi ;
155 over LLVMIsFunctionVarArg 0 = not >>vararg?
156 over LLVMGetReturnType (tref>) >>return
157 swap dup LLVMCountParamTypes <void*-array>
158 [ LLVMGetParamTypes ] keep >array
159 [ (tref>) ] map >>params ;
161 : type-kind ( LLVMTypeRef -- class )
163 { LLVMVoidTypeKind [ void ] }
164 { LLVMFloatTypeKind [ float ] }
165 { LLVMDoubleTypeKind [ double ] }
166 { LLVMX86_FP80TypeKind [ x86_fp80 ] }
167 { LLVMFP128TypeKind [ fp128 ] }
168 { LLVMPPC_FP128TypeKind [ ppc_fp128 ] }
169 { LLVMLabelTypeKind [ label ] }
170 { LLVMIntegerTypeKind [ integer new ] }
171 { LLVMFunctionTypeKind [ function new ] }
172 { LLVMStructTypeKind [ struct new ] }
173 { LLVMArrayTypeKind [ array new ] }
174 { LLVMPointerTypeKind [ pointer new ] }
175 { LLVMOpaqueTypeKind [ opaque ] }
176 { LLVMVectorTypeKind [ vector new ] }
179 TUPLE: up-ref height ;
183 types get length swap height>> - types get nth
184 cached>> [ LLVMOpaqueType ] unless* ;
186 : resolve-types ( typeref typeref -- typeref )
187 over LLVMCreateTypeHandle [ LLVMRefineType ] dip
188 [ LLVMResolveTypeHandle ] keep LLVMDisposeTypeHandle ;
190 : >tref-caching ( type -- LLVMTypeRef )
191 V{ } clone types [ (>tref) ] with-variable ;
193 : >tref ( type -- LLVMTypeRef )
194 [ >tref-caching ] [ >tref-caching ] [ clean ] tri
195 2dup = [ drop ] [ resolve-types ] if ;
197 : tref> ( LLVMTypeRef -- type )
198 V{ } clone types [ (tref>) ] with-variable ;
202 "type-info" LLVMModuleCreateWithName
203 [ "t" rot LLVMAddTypeName drop ]
205 [ LLVMDisposeModule ] tri ;
211 Zero = "0" => [[ drop 0 ]]
214 Number = LeadingDigit:d (DecimalDigit)*:ds => [[ ds d prefix string>number ]]
215 WhiteNumberSpace = WhiteSpace Number:n WhiteSpace => [[ n ]]
216 WhiteZeroSpace = WhiteSpace (Zero | Number):n WhiteSpace => [[ n ]]
218 Integer = "i" Number:n => [[ n <integer> ]]
219 FloatingPoint = ( "float" | "double" | "x86_fp80" | "fp128" | "ppc_fp128" ) => [[ "llvm.types" vocab lookup ]]
220 LabelVoidMetadata = ( "label" | "void" | "metadata" | "opaque" ) => [[ "llvm.types" vocab lookup ]]
221 Primitive = LabelVoidMetadata | FloatingPoint
222 Pointer = T:t WhiteSpace "*" => [[ t <pointer> ]]
223 Vector = "<" WhiteNumberSpace:n "x" Type:t ">" => [[ n t <vector> ]]
224 StructureTypesList = "," Type:t => [[ t ]]
225 Structure = "{" Type:t (StructureTypesList)*:ts "}" => [[ ts t prefix >array f <struct> ]]
226 Array = "[" WhiteZeroSpace:n "x" Type:t "]" => [[ n t <array> ]]
227 NoFunctionParams = "(" WhiteSpace ")" => [[ drop { } ]]
228 VarArgs = WhiteSpace "..." WhiteSpace => [[ drop ... ]]
229 ParamListContinued = "," (Type | VarArgs):t => [[ t ]]
230 ParamList = "(" Type:t (ParamListContinued*):ts ")" => [[ ts t prefix ]]
231 Function = T:t WhiteSpace ( ParamList | NoFunctionParams ):ts => [[ ... ts member? dup [ ... ts delete ] when t ts >array rot <function> ]]
232 PackedStructure = "<" WhiteSpace "{" Type:ty (StructureTypesList)*:ts "}" WhiteSpace ">" => [[ ts ty prefix >array t <struct> ]]
233 UpReference = "\\" Number:n => [[ n <up-ref> ]]
234 Name = '%' ([a-zA-Z][a-zA-Z0-9]*):id => [[ id flatten >string ]]
236 T = Pointer | Function | Primitive | Integer | Vector | Structure | PackedStructure | Array | UpReference | Name
238 Type = WhiteSpace T:t WhiteSpace => [[ t ]]
244 SYNTAX: TYPE: ";" parse-multiline-string parse-type parsed ;