forked from ceu-lang/ceu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathval.lua
More file actions
410 lines (370 loc) · 11.7 KB
/
Copy pathval.lua
File metadata and controls
410 lines (370 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
local t2n = {
us = 10^0,
ms = 10^3,
s = 10^6,
min = 60*10^6,
h = 60*60*10^6,
}
local _ceu2c = { ['or']='||', ['and']='&&', ['not']='!' }
local function ceu2c (op)
return _ceu2c[op] or op
end
function V (me)
ASR(me.val, me, 'invalid expression')
return me.val
end
function CUR (me, id)
if id then
return '(('.._TP.c(CLS().id)..'*)_ceu_go->org)->'..id
else
return '(('.._TP.c(CLS().id)..'*)_ceu_go->org)'
end
end
F =
{
Block_pre = function (me)
local cls = CLS()
for _, var in ipairs(me.vars) do
if var.pre == 'var' then
if var.isTmp then
var.val = '__ceu_'..var.id..'_'..var.n
else
var.val = CUR(me, var.id_)
end
elseif var.pre == 'function' then
var.val = 'CEU_'..cls.id..'_'..var.id
end
end
if me.trl_orgs then
me.trl_orgs.val = CUR(me, '__lnks_'..me.n)
end
if me.fins then
for i, fin in ipairs(me.fins) do
fin.val = CUR(me, '__fin_'..me.n..'_'..i)
end
end
end,
ParAnd = function (me)
me.val = CUR(me, '__and_'..me.n)
end,
Global = function (me)
me.val = '(_ceu_app->data)'
end,
This = function (me)
if _AST.iter'Dcl_constr'() then
me.val = '__ceu_org' -- set when calling constr
else
me.val = '_ceu_go->org'
end
me.val = '(*(('.._TP.c(me.tp)..'*)'..me.val..'))'
end,
Var = function (me)
me.val = me.var.val
end,
SetExp = function (me)
local _, fr, to = unpack(me)
V(fr) -- error on reads of internal events
end,
-- SetExp is inside and requires .val
New_pre = function (me)
me.val = '(('.._TP.c(me[2])..'*)__ceu_new)'
-- defined by _New (code.lua)
end,
Spawn_pre = function (me)
me.val = '(__ceu_new != NULL)'
end,
Thread = function (me)
me.thread_id = CUR(me, '__thread_id_'..me.n)
me.thread_st = CUR(me, '__thread_st_'..me.n)
me.val = '(*('..me.thread_st..') > 0)'
end,
EmitExt = function (me)
local op, ext, param = unpack(me)
local DIR, dir, ptr, mode
if ext.evt.pre == 'input' then
DIR = 'IN'
dir = 'in'
if op == 'call' then
ptr = '_ceu_app->data'
else
ptr = '_ceu_app'
end
else
DIR = 'OUT'
dir = 'out'
ptr = '_ceu_app'
end
local tup = _TP.isTuple(ext.evt.ins)
if op == 'call' or dir == 'in' or
(not tup) or (tup == 1) then
mode = 'val'
else
mode = 'buf'
end
local t1 = { }
if ext.evt.pre=='input' and op=='call' then
t1[#t1+1] = '_ceu_app' -- to access `app´
t1[#t1+1] = ptr -- to access `this´
end
local t2 = { ptr, 'CEU_'..DIR..'_'..ext.evt.id }
if param then
local tp = _TP.deref(ext.evt.ins, true)
local val
if tp then
val = '(void*)'..V(param)
else
val = V(param)
end
t1[#t1+1] = val
if tup and #tup>1 then
if mode == 'val' then
t2[#t2+1] = '(tceu_evtp)(void*)'..val
else
t2[#t2+1] = 'sizeof('..ext.evt.ins..')'
t2[#t2+1] = '(char*)'..val
end
else
assert(mode == 'val')
t2[#t2+1] = '(tceu_evtp)'..val
end
else
if mode == 'val' then
t2[#t2+1] = '(tceu_evtp)NULL'
else
t2[#t2+1] = '0'
t2[#t2+1] = '(char*)NULL'
end
end
t2 = table.concat(t2, ', ')
t1 = table.concat(t1, ', ')
local ret = ''
if _OPTS.os and op=='call' then
-- when the call crosses the process,
-- the return val must be unpacked from tceu_evtp
if me.__ast_set then
if ext.evt.out == 'int' then
ret = '.v'
else
ret = '.ptr'
end
end
end
local op = (op=='emit' and 'emit') or 'call'
me.val = '\n'..[[
#if defined(ceu_]]..dir..'_'..op..'_'..ext.evt.id..[[)
ceu_]]..dir..'_'..op..'_'..ext.evt.id..'('..t1..[[)
#elif defined(ceu_]]..dir..'_'..op..'_'..mode..[[)
ceu_]]..dir..'_'..op..'_'..mode..'('..t2..')'..ret..[[
#else
#error ceu_]]..dir..'_'..op..[[_* is not defined
#endif
]]
end,
AwaitInt = 'AwaitExt',
AwaitExt = function (me)
local e1 = unpack(me)
local tp = (e1.evt or e1.var.evt).ins
if _TP.deref(tp) then
me.val = '(('.._TP.c( (e1.evt or e1.var.evt).ins )..')_ceu_go->evtp.ptr)'
elseif _TP.isTuple(tp) then
me.val = '(('.._TP.c( (e1.evt or e1.var.evt).ins )..'*)_ceu_go->evtp.ptr)'
else
me.val = '(_ceu_go->evtp.v)'
--me.val = '*(('.._TP.c(e1.evt.ins)..'*)_ceu_go->evtp.ptr)'
end
end,
AwaitT = function (me)
me.val = '_ceu_app->wclk_late'
me.val_wclk = CUR(me, '__wclk_'..me.n)
end,
--[[
AwaitS = function (me)
me.val = '__ceu_'..me.n..'_AwaitS'
end,
]]
Op2_call = function (me)
local _, f, exps = unpack(me)
local ps = {}
if f.var and f.var.fun then
ps[#ps+1] = '_ceu_app'
if f.org then
local op = (_ENV.clss[f.org.tp].is_ifc and '') or '&'
ps[#ps+1] = '('..op..V(f.org)..')' -- only native
else
ps[#ps+1] = CUR(me)
end
end
for i, exp in ipairs(exps) do
ps[#ps+1] = V(exp)
end
me.val = V(f)..'('..table.concat(ps,',')..')'
end,
Op2_idx = function (me)
local _, arr, idx = unpack(me)
me.val = V(arr)..'['..V(idx)..']'
end,
Op2_any = function (me)
local op, e1, e2 = unpack(me)
me.val = '('..V(e1)..ceu2c(op)..V(e2)..')'
end,
['Op2_-'] = 'Op2_any',
['Op2_+'] = 'Op2_any',
['Op2_%'] = 'Op2_any',
['Op2_*'] = 'Op2_any',
['Op2_/'] = 'Op2_any',
['Op2_|'] = 'Op2_any',
['Op2_&'] = 'Op2_any',
['Op2_<<'] = 'Op2_any',
['Op2_>>'] = 'Op2_any',
['Op2_^'] = 'Op2_any',
['Op2_=='] = 'Op2_any',
['Op2_!='] = 'Op2_any',
['Op2_>='] = 'Op2_any',
['Op2_<='] = 'Op2_any',
['Op2_>'] = 'Op2_any',
['Op2_<'] = 'Op2_any',
['Op2_or'] = 'Op2_any',
['Op2_and'] = 'Op2_any',
Op1_any = function (me)
local op, e1 = unpack(me)
me.val = '('..ceu2c(op)..V(e1)..')'
end,
['Op1_~'] = 'Op1_any',
['Op1_-'] = 'Op1_any',
['Op1_not'] = 'Op1_any',
['Op1_*'] = function (me)
local op, e1 = unpack(me)
local cls = _ENV.clss[_TP.deref(e1.tp)]
if cls and cls.is_ifc then
me.val = V(e1)
else
me.val = '('..ceu2c(op)..V(e1)..')'
end
end,
['Op1_&'] = function (me)
local op, e1 = unpack(me)
me.val = '('..ceu2c(op)..V(e1)..')'
end,
['Op2_.'] = function (me)
if me.org then
local cls = _ENV.clss[me.org.tp]
local gen = '((tceu_org*)'..me.org.val..')'
if cls and cls.is_ifc then
if me.var.pre == 'var' then
me.val = [[(*(
(]].._TP.c(me.var.tp)..[[*) (
((char*)]]..me.org.val..[[) + _CEU_APP.ifcs_flds[]]..gen..[[->cls][
]].._ENV.ifcs.flds[me.var.ifc_id]..[[
]
)
))]]
elseif me.var.pre == 'function' then
me.val = [[(*(
(]].._TP.c(me.var.tp)..[[*) (
_CEU_APP.ifcs_funs[]]..gen..[[->cls][
]].._ENV.ifcs.funs[me.var.ifc_id]..[[
]
)
))]]
else -- event
me.val = nil -- cannot be used as variable
me.ifc_idx = '(_CEU_APP.ifcs_evts['..gen..'->cls]['
.._ENV.ifcs.evts[me.var.ifc_id]
..'])'
end
else
if me.c then
me.val = me.c.id_
elseif me.var.pre == 'var' then
me.val = me.org.val..'.'..me.var.id_
elseif me.var.pre == 'event' then
me.val = nil -- cannot be used as variable
me.org.val = '(&'..me.org.val..')' -- always via reference
else -- function
me.val = me.var.val
end
end
else
local op, e1, id = unpack(me)
me.val = '('..V(e1)..ceu2c(op)..id..')'
end
end,
Op1_cast = function (me)
local tp, exp = unpack(me)
local val = V(exp)
local _tp = _TP.deref(tp)
local cls = _tp and _ENV.clss[_tp]
if cls then
if cls.is_ifc then
-- TODO: out of bounds acc
val = '(('..val..' == NULL) ? NULL : '..
'((_CEU_APP.ifcs_clss[((tceu_org*)'..val..')->cls]'
..'['..cls.n..']) ?'..val..' : NULL)'..
')'
else
val = '(('..val..' == NULL) ? NULL : '..
'((((tceu_org*)'..val..')->cls == '..cls.n..') ? '
..val..' : NULL)'..
')'
end
end
me.val = '(('.._TP.c(tp)..')'..val..')'
end,
WCLOCKK = function (me)
local h,min,s,ms,us = unpack(me)
me.us = us*t2n.us + ms*t2n.ms + s*t2n.s + min*t2n.min + h*t2n.h
me.val = me.us
ASR(me.us>0 and me.us<=2000000000, me, 'constant is out of range')
end,
WCLOCKE = function (me)
local exp, unit = unpack(me)
me.us = nil
me.val = V(exp) .. '*' .. t2n[unit]-- .. 'L'
end,
RawExp = function (me)
me.val = unpack(me)
-- handle org iterators
local blk = _AST.iter'Do'()
blk = blk and blk[1]
if me.iter_ini then
if blk.trl_orgs then
me.val = [[
( (_ceu_go->org->trls[ ]]..blk.trl_orgs[1]..[[ ].lnks[0].nxt->n == 0) ?
NULL :
_ceu_go->org->trls[ ]]..blk.trl_orgs[1]..[[ ].lnks[0].nxt )
]]
else
me.val = 'NULL'
end
elseif me.iter_nxt then
if blk.trl_orgs then
local var = me.iter_nxt.var.val
me.val = '(('..var..'->nxt->n==0) ? NULL : '..var..'->nxt)'
else
me.val = 'NULL'
end
end
end,
Nat = function (me)
me.val = string.sub(me[1], 2)
end,
SIZEOF = function (me)
--me.val = me.sval
local tp = unpack(me)
if type(tp) == 'string' then
me.val = 'sizeof('.._TP.c(me[1])..')'
else
me.val = 'sizeof('..tp.val..')'
end
end,
STRING = function (me)
me.val = me[1]
end,
NUMBER = function (me)
me.val = me[1]
end,
NULL = function (me)
me.val = '((void *)0)'
end,
}
_AST.visit(F)