Partial move to new generator.
[panamaz] / src / types.api
1 # -*- Mode:text; tab-width:4; electric-indent-mode: nil; indent-line-function:insert-tab; -*-
2
3 #
4 # TODO: add a 'mode' to type matching
5 #       this woudld allow per-field overrides, e.g. char * field use String or MemorySegment or ByteArray depending on option
6
7 # special case for bitfields
8 # TODO: well this is bit of a mess, maybe should use <init> like code:method?
9 type /bitfield/ copy=<common> {
10         layout;
11         type            {{ bitfieldType($m) }}
12         carrier         {{ "{type}" }}
13         tonative        {{ "({type})({value})" }}
14         tojava          {{ "({type})({value})" }}
15
16         getshiftl       {{ bitfieldSize($m) - $m->{size} - bitfieldOffset($m) }}
17         getshiftr       {{ bitfieldSize($m) - $m->{size} }}
18         getshiftop      {{ ($m->{type} =~ m/^i/) ? ">>" : ">>>" }}
19
20         setmask         {{ bitfieldMask($m) }}
21         setoffset       {{ bitfieldOffset($m) }}
22
23         varindex        {{ bitfieldIndex($m) }}
24
25         getnative       {{
26                 "({type})((({type}){name}\$VH.get({segment}) << {getshiftl}) {getshiftop} {getshiftr})"
27         }}
28         setnative       {{
29                 "$m->{name}\$VH.set({segment}, ({type})((({type}){name}\$VH.get({segment}) & ~{setmask}) | ((({value}) << {setoffset}) & {setmask})))"
30         }}
31         varhandle       {{ "final static VarHandle {name}\$VH = MemoryLayout.sequenceLayout(Memory.".uc(bitfieldType($m)).").varHandle(MemoryLayout.PathElement.sequenceElement({varindex}));\n" }}
32 }
33
34 # void, only for return type
35 type /void/ {
36         type    {{ 'void' }}
37         carrier {{ 'void' }}
38         getnative;
39         setnative;
40         tonative;
41         tojava;
42 }
43
44 # 1d array of pointer to pointer
45 type /^\[(\d+)u64:u64.*\]$/ {
46 }
47
48 # 1d array of pointer to primitive
49 type /^\[(?<length>\d+)u64:(?<ctype>[uif]\d+)\]$/
50                 copy=<pointer> template=code:getbyvalue,code:getsetelement {
51
52         type            {{ !$m->{array} ? 'PointerArray' : 'HandleArray<{typei}>' }}
53         layout          {{ 'MemoryLayout.sequenceLayout({length}, Memory.POINTER)' }}
54         tojava          {{ !$m->{array} ? 'PointerArray.createArray({value}, {length}, {scope})' : 'HandleArray.createArray({value}, {length}, {typei}::create, {scope})' }}
55
56         getsegment      {{ '(MemorySegment){name}$SH.invokeExact({segment})' }}
57         getnative       {{ !$m->{array} ? 'PointerArray.create({getsegment})' : 'HandleArray.create({getsegment}, (a, s) -> {typei}.createArray(a, Long.MAX_VALUE, s))' }}
58         setnative;
59         varhandle       {{
60                 'final static MethodHandle {name}$SH = LAYOUT.sliceHandle(MemoryLayout.PathElement.groupElement("{name}"));'."\n".
61                 'final static VarHandle {name}$EH = LAYOUT.varHandle(MemoryLayout.PathElement.groupElement("{name}"), MemoryLayout.PathElement.sequenceElement());'."\n"
62         }}
63
64         typei           {{ ucfirst($typeSizes{$match->{ctype}}).'Array' }}
65         getnativei      {{ '{typei}.createArray((MemoryAddress){name}$EH.get({segment}, {index}), Long.MAX_VALUE, {scope})' }}
66         setnativei      {{ '{name}$EH.set({segment}, {index}, (jdk.incubator.foreign.Addressable)Memory.address({value}))' }}
67
68 }
69
70 # 1d array of pointer to type (struct or union or function?)
71 type /^\[(?<length>\d+)u64:\$\{(\w+)\}\]$/ {
72         type            {{ "HandleArray<$data->{$m->{type}}->{rename}>" }}
73         layout          {{ "MemoryLayout.sequenceLayout({length}, Memory.POINTER)" }}
74 }
75
76 # 1d array of (struct or union or function?)
77 type /^\[(?<length>\d+)\$\{(\w+)\}\]$/ {
78         type            {{ "$data->{$m->{type}}->{rename}" }}
79         layout          {{ "MemoryLayout.sequenceLayout({length}, {type}.LAYOUT)" }}
80 }
81
82 # 1d array of primitive
83 type /^\[(?<length>\d+)(?<ctype>[uif]\d+)\]$/
84                 copy=<pointer> template=code:getbyvalue,code:getsetelement {
85         layout     {{ "MemoryLayout.sequenceLayout({length}, Memory.".uc($typeSizes{$match->{ctype}}).")" }}
86         type       {{ ucfirst($typeSizes{$match->{ctype}})."Array" }}
87         tojava     {{ "{type}.createArray({value}, $match->{length}, {scope})" }}
88         tonative   {{ "(jdk.incubator.foreign.Addressable)Memory.address({value}" }}
89
90         getnative  {{ "{type}.create((MemorySegment)$m->{name}\$SH.invokeExact({segment}))" }}
91         setnative;
92         varhandle  {{
93                 "final static MethodHandle $m->{name}\$SH = LAYOUT.sliceHandle(MemoryLayout.PathElement.groupElement(\"$m->{name}\"));\n".
94                 "final static VarHandle $m->{name}\$EH = LAYOUT.varHandle(MemoryLayout.PathElement.groupElement(\"$m->{name}\"), MemoryLayout.PathElement.sequenceElement());\n"
95         }}
96
97         typei      {{ "$typeSizes{$match->{ctype}}" }}
98         getnativei {{ "({typei})$m->{name}\$EH.get({segment}, {index})" }}
99         setnativei {{ "$m->{name}\$EH.set({segment}, {index}, ({typei}){value})" }}
100 }
101
102 # 2d array of primitive
103 type /^\[(?<length0>\d+)\[(?<length1>\d+)(?<ctype>[uif]\d+)\]\]$/
104                 copy=<pointer> template=code:getbyvalue,code:getsetelement2d {
105         layout     {{ "MemoryLayout.sequenceLayout({length0}, MemoryLayout.sequenceLayout({length1}, Memory.".uc($typeSizes{$match->{ctype}})."))" }}
106         type       {{ ucfirst($typeSizes{$match->{ctype}})."Array" }}
107         tojava     {{ "{type}.create({value})" }}
108         tonative   {{ "(jdk.incubator.foreign.Addressable)Memory.address({value})" }}
109
110         getnative  {{ "{type}.create((MemorySegment)$m->{name}\$SH.invokeExact({segment}))" }}
111         setnative;
112
113         typei      {{ "$typeSizes{$match->{ctype}}" }}
114         getnativei {{ "({typei})$m->{name}\$EH.get({segment}, {index0}, {index1}))" }}
115         setnativei {{ "$m->{name}\$EH.set({segment}, {index0}, {index1}, ({typei}){value}))" }}
116
117         varhandle  {{
118                 "final static MethodHandle $m->{name}\$SH = LAYOUT.sliceHandle(MemoryLayout.PathElement.groupElement(\"$m->{name}\"));\n".
119                 "final static VarHandle $m->{name}\$EH = LAYOUT.varHandle(MemoryLayout.PathElement.groupElement(\"$m->{name}\"), MemoryLayout.PathElement.sequenceElement(), MemotyLayout.PathElement.sequenceElement());\n"
120         }}
121 }
122
123 # function pointer
124 type /^u64:\(/ copy=<pointer> {
125         type    {{ "FunctionPointer<$data->{$m->{type}}->{rename}>" }}
126         tojava  {{ "$data->{$m->{type}}->{rename}.downcall({value}, {scope})" }}
127 }
128
129 # **primitive
130 type /^u64:u64:(?<ctype>[uif]\d+)$/ copy=<pointer> {
131         length  {{ $m->{'array-size'} ? 'get'.($m->{'array-size'}->{rename}).'()' : 'Long.MAX_VALUE' }}
132         type    {{ !$m->{array} ? 'PointerArray' : 'HandleArray<{typei}>' }}
133         tojava  {{
134                 !$m->{array}
135                 ? 'PointerArray.createArray({value}, {length}, {scope})'
136                 : 'HandleArray.createArray({value}, {length}, (a, s) -> {typei}.createArray(a, {length}, s), {scope})'
137         }}
138         typei   {{ ucfirst($typeSizes{$match->{ctype}})."Array" }}
139 }
140
141 # **type
142 type /^u64:u64:\$\{(\w+)\}$/ copy=<pointer> {
143         length          {{ $m->{'array-size'} ? 'get'.($m->{'array-size'}->{rename}).'()' : 'Long.MAX_VALUE' }}
144         type            {{ !$m->{array} ? 'PointerArray' : 'HandleArray<{typei}>' }}
145         tojava          {{
146                 !$m->{array}
147                 ? 'PointerArray.createArray({value}, {length}, {scope})'
148                 : 'HandleArray.createArray({value}, {length}, {typei}::create, {scope})'
149         }}
150         # tojavai ... ?
151         carrieri        {{ "MemoryAddress" }}
152         typei           {{ "$data->{$m->{type}}->{rename}" }}
153         tojavai         {{ "{typei}.create({value}, {scope})" }}
154 }
155
156 # **void
157 type /^u64:u64:v$/ copy=<pointer> {
158         typei           {{ 'PointerArray' }}
159         type            {{ "HandleArray<{typei}>" }}
160         tojava          {{ "HandleArray.createArray({value}, Long.MAX_VALUE, PointerArray"."::create, {scope})" }}
161 }
162
163 # *primitive, or string for [iNNu8]
164
165 # idea for when i implement 'flag' matching for types
166 # *i8 with 'array' flag
167 # TODO: length, multiple flags?
168 type /^u64:(?<ctype>i8)$/ select=array copy=<pointer> {
169         type            {{ ucfirst($typeSizes{$match->{ctype}}).'Array' }}
170         tojava          {{ '{type}.createArray({value}, Long.MAX_VALUE, {scope})' }}
171         carrieri        {{ "{typei}" }}
172         typei           {{ $typeSizes{$match->{ctype}} }}
173         tojavai         {{ "({typei}){value}" }}
174 }
175
176 # *i8 with 'segment' flag, length must be supplied (somewhere??)
177 type /^u64:(?<ctype>i8)$/ select=segment copy=<pointer> {
178         type            {{ 'MemorySegment' }}
179         tojava          {{ '{value}' }}
180         carrieri        {{ "{typei}" }}
181         typei           {{ 'byte' }}
182         tojavai         {{ "({typei}){value}" }}
183 }
184
185 # *i8 with no flag = String
186 type /^u64:(?<ctype>i8)$/ copy=<pointer> {
187         type            {{ 'String' }}
188         tonative        {{ '(jdk.incubator.foreign.Addressable)frame$.copy({value})' }}
189
190         setnative       {{ '{name}$VH.set({segment}, {copynative})' }}
191         copynative      {{ 'SegmentAllocator.nativeAllocator(segment.scope()).allocateUtf8String({value})' }}
192
193         tojava          {{ '({value}).getUtf8String(0L)' }}
194 }
195
196 # *Primitive fallback
197 type /^u64:(?<ctype>[uif]\d+)$/ copy=<pointer> {
198         type            {{ ucfirst($typeSizes{$match->{ctype}})."Array" }}
199         tonative        {{ '(jdk.incubator.foreign.Addressable)Memory.address({value})' }}
200         tojava          {{ ucfirst($typeSizes{$match->{ctype}})."Array.createArray({value}, Long.MAX_VALUE, {scope})" }}
201         carrieri        {{ "{typei}" }}
202         typei           {{ $typeSizes{$match->{ctype}} }}
203         tojavai         {{ "({typei}){value}" }}
204 }
205
206 # *type  struct?  handle?
207 type /^u64:\$\{(\w+)\}$/ copy=<pointer> {
208         type            {{ "$data->{$m->{type}}->{rename}" }}
209         tojava          {{ "$data->{$m->{type}}->{rename}.create({value}, {scope})" }}
210 }
211
212 # *void with size
213 type /^u64:v$/ select=array-size copy=<pointer> {
214         type            {{ 'MemorySegment' }}
215         tojava          {{ "MemorySegment.ofAddress({value}, $m->{'array-size'}->{name}, {scope})" }}
216 }
217
218 # *void
219 type /^u64:v$/ copy=<pointer> {
220 }
221
222 # primitive
223 type /^(?<ctype>[uif]\d+)$/ copy=<common> {
224         layout     {{ "Memory.".uc($typeSizes{$match->{ctype}}) }}
225         carrier    {{ "$typeSizes{$match->{ctype}}" }}
226         type       {{ "$typeSizes{$match->{ctype}}" }}
227         tonative   {{ '({type})({value})' }}
228         tojava     {{ '({type})({value})' }}
229 }
230
231 # type inline
232 type /^\$\{(\w+)\}$/ byvalue template=code:getbyvalue {
233         layout          {{ '{type}.LAYOUT' }}
234         carrier         {{ 'MemoryAddress' }}
235         type            {{ "$data->{$m->{type}}->{rename}" }}
236         tojava          {{ '{type}.create({value}, {scope})' }}
237         tonative        {{ '(jdk.incubator.foreign.Addressable)Memory.address({value})' }}
238         getnative       {{ '{type}.create(({carrier}){name}$SH.invokeExact({segment}), {scope})' }}
239         setnative;
240         varhandle       {{ 'final static MethodHandle {name}$SH = LAYOUT.sliceHandle(MemoryLayout.PathElement.groupElement("{name}"));'."\n" }}
241 }
242
243 type <common> template=code:getset {
244         getnative       {{ '({carrier}){name}$VH.get({segment})' }}
245         setnative       {{ '{name}$VH.set({segment}, {tonative})' }}
246         varhandle       {{ 'final static VarHandle {name}$VH = LAYOUT.varHandle(MemoryLayout.PathElement.groupElement("{name}"));'."\n" }}
247         tojava          {{ '{value}' }}
248         tonative        {{ '{value}' }}
249 }
250
251 type <pointer> copy=<common> {
252         layout          {{ 'Memory.POINTER' }}
253         carrier         {{ 'MemoryAddress' }}
254         type            {{ 'MemoryAddress' }}
255         tonative        {{ '(jdk.incubator.foreign.Addressable)Memory.address({value})' }}
256 }