forked from ceu-lang/ceu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.lua
2388 lines (2135 loc) · 68.4 KB
/
code.lua
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
CODE = {
has_goto = false, -- avoids "unused label"
pres = '',
constrs = '',
threads = '',
isrs = '',
functions = '',
stubs = '', -- maps input functions to ceu_app_call switch cases
}
-- Assert that all input functions have bodies.
local INPUT_FUNCTIONS = {
-- F1 = false, -- input function w/o body
-- F2 = true, -- input functino w/ body
}
function CONC_ALL (me, t)
t = t or me
for _, sub in ipairs(t) do
if AST.isNode(sub) then
CONC(me, sub)
end
end
end
function CONC (me, sub, tab)
sub = sub or me[1]
tab = string.rep(' ', tab or 0)
me.code = me.code .. string.gsub(sub.code, '(.-)\n', tab..'%1\n')
end
function CASE (me, lbl)
LINE(me, 'case '..lbl.id..':;', 0)
end
function DEBUG_TRAILS (me, lbl)
LINE(me, [[
#ifdef CEU_DEBUG_TRAILS
#ifndef CEU_OS
printf("\tOK!\n");
#endif
#endif
]])
end
function LINE (me, line, spc)
spc = spc or 4
spc = string.rep(' ', spc)
me.code = me.code .. [[
#line ]]..me.ln[2]..' "'..me.ln[1]..[["
]] .. spc..line
end
function LABEL_NO (me)
local no = '_CEU_NO_'..me.n..'_'
LINE(me, [[
]]..no..[[:
if (0) { goto ]]..no..[[; /* avoids "not used" warning */ }
]])
return no
end
function HALT (me, t)
if not t then
LINE(me, 'return;')
return
end
LINE(me, [[
_ceu_trl->evt = ]]..t.evt..[[;
_ceu_trl->lbl = ]]..t.lbl..[[;
_ceu_trl->seqno = ]]..(t.isEvery and '_ceu_app->seqno-1' or '_ceu_app->seqno')..[[;
]])
if t.evto then
LINE(me, [[
#ifdef CEU_ORGS
_ceu_trl->evto = ]]..t.evto..[[;
#endif
]])
end
if t.org_or_adt then
LINE(me, [[
#ifdef CEU_ORGS_AWAIT
#ifdef CEU_ADTS_AWAIT
_ceu_trl->is_org = ]]..t.is_org..[[;
#endif
#endif
_ceu_trl->org_or_adt = ]]..t.org_or_adt..[[;
]])
end
if t.evt == 'CEU_IN__ASYNC' then
LINE(me, [[
#ifdef ceu_out_async
ceu_out_async(_ceu_app);
#endif
_ceu_app->pendingAsyncs = 1;
]])
end
LINE(me, [[
return;
case ]]..t.lbl..[[:;
]])
if t.no and PROPS.has_pses then
local function __pause_or_dclcls (me)
return me.tag=='Pause' or me.tag=='Dcl_cls'
end
for pse in AST.iter(__pause_or_dclcls) do
if pse.tag == 'Dcl_cls' then
break
end
COMM(me, 'PAUSE: '..pse.dcl.var.id)
LINE(me, [[
if (]]..V(pse.dcl,'rval')..[[) {
goto ]]..t.no..[[;
}
]])
end
end
end
function GOTO (me, lbl)
CODE.has_goto = true
LINE(me, [[
_ceu_lbl = ]]..lbl..[[;
goto _CEU_GOTO_;
]])
end
function COMM (me, comm)
LINE(me, '/* '..comm..' */', 0)
end
local _iter = function (n)
if n.tag == 'Dcl_cls' then
return true
end
if n.tag == 'Block' and n.needs_clr then
return true
end
if n.tag == 'SetBlock' and n.needs_clr then
return true
end
if n.tag == 'Loop' and n.needs_clr then
return true
end
n = n.__par
if n and (n.tag == 'ParOr') then
return true -- par branch
end
end
-- TODO: check if all calls are needed
-- (e.g., cls outermost block should not!)
function CLEAR (me)
COMM(me, 'CLEAR: '..me.tag..' ('..me.ln[2]..')')
if ANA and me.ana.pos[false] then
return
end
if not me.needs_clr then
return
end
-- check if top will clear during same reaction
if (not me.has_orgs) and (not me.needs_clr_fin) then
if ANA then -- fin must execute before any stmt
local top = AST.iter(_iter)()
if top and top.needs_clr
and ANA.IS_EQUAL(top.ana.pos, me.ana.pos)
then
-- TODO: emit/kill in between the two breaks this
-- TODO: check on concat if previous is clear, and override it with outer
--return -- top will clear
end
end
end
LINE(me, [[
{
/* Reuse current stack frame.
* We know that CLEAR will not abort anything and return normally.
* Just save the previous "is_alive", call CLEAR, and restore it.
*/
#ifdef CEU_STACK_CLEAR
int __ceu_old = _ceu_stk->is_alive;
_ceu_stk->is_alive = 1;
#endif
tceu_evt evt;
evt.id = CEU_IN__CLEAR;
ceu_sys_go_ex(_ceu_app, &evt,
_ceu_stk,
_ceu_org,
]]..me.trails[1]..[[,
]]..(me.trails[2]+1)..[[);
#ifdef CEU_STACK_CLEAR
_ceu_stk->is_alive = __ceu_old;
ceu_sys_stack_clear(_ceu_stk, _ceu_org,
]]..me.trails[1]..','..me.trails[2]..[[);
#endif
}
]])
if me.has_orgs then
-- TODO: only if contains orgs of awaited classes
LINE(me, [[
#ifdef CEU_ORGS_AWAIT
/* signal ok_killed */
{
tceu_kill ps = { _ceu_org, _ceu_org->ret, ]]..me.trails[1]..','..me.trails[2]..[[ };
tceu_evt evt_;
evt_.id = CEU_IN__ok_killed;
evt_.param = &ps;
ceu_sys_go_ex(_ceu_app, &evt_, _ceu_stk,
_ceu_app->data, 0, _ceu_app->data->n);
}
#endif
]])
end
end
F = {
Node_pre = function (me)
me.code = me.code or '/* NODE: '..me.tag..' '..me.n..' */\n'
end,
Do = CONC_ALL,
Finally = CONC_ALL,
Dcl_constr = function (me)
CONC_ALL(me)
CODE.constrs = CODE.constrs .. [[
static void _ceu_constr_]]..me.n..[[ (tceu_app* _ceu_app, tceu_org* __ceu_this, tceu_org* _ceu_org) {
]] .. me.code .. [[
}
]]
end,
Stmts = function (me)
LINE(me, '{') -- allows C declarations for Spawn
CONC_ALL(me)
LINE(me, '}')
end,
Root = function (me)
for _, cls in ipairs(ENV.clss_cls) do
me.code = me.code .. cls.code_cls
end
-- assert that all input functions have bodies
for evt, v in pairs(INPUT_FUNCTIONS) do
ASR(v, evt.ln, 'missing function body')
end
end,
BlockI = CONC_ALL,
BlockI_pos = function (me)
-- Interface constants are initialized from outside
-- (another _ceu_go_org), need to use __ceu_org instead.
me.code_ifc = string.gsub(me.code, '_ceu_org', '__ceu_this')
me.code = ''
end,
Dcl_fun = function (me)
local pre, _, ins, out, id, blk = unpack(me)
if blk then
if me.var.fun.isExt then
local ps = {}
assert(ins.tup, 'bug found')
for i, _ in ipairs(ins) do
ps[#ps+1] = '(('..TP.toc(ins)..'*)((void*)param))->_'..i
end
ps = (#ps>0 and ',' or '')..table.concat(ps, ',')
CODE.functions = CODE.functions .. [[
#define ceu_in_call_]]..id..[[(app,org,param) ]]..me.id..[[(app,org ]]..ps..[[)
]]
local ret_value, ret_void
if TP.toc(out) == 'void' then
ret_value = '('
ret_void = 'return NULL;'
else
ret_value = 'return ((void*)'
ret_void = ''
end
CODE.stubs = CODE.stubs .. [[
case CEU_IN_]]..id..[[:
#line ]]..me.ln[2]..' "'..me.ln[1]..[["
]]..ret_value..me.id..'(_ceu_app, _ceu_app->data '..ps..[[));
]]..ret_void..'\n'
end
-- functions and threads receive __ceu_org as parameter
local code = string.gsub(blk.code, '_ceu_org', '__ceu_this')
CODE.functions = CODE.functions ..
me.proto..'{'..code..'}'..'\n'
end
-- assert that all input functions have bodies
local evt = ENV.exts[id]
if me.var.fun.isExt and evt and evt.pre=='input' then
INPUT_FUNCTIONS[evt] = INPUT_FUNCTIONS[evt] or blk or false
end
end,
Return = function (me)
local exp = unpack(me)
LINE(me, 'return '..(exp and V(exp,'rval') or '')..';')
end,
Dcl_cls_pos = function (me)
me.code_cls = me.code
me.code = '' -- do not inline in enclosing class
end,
Dcl_cls = function (me)
if me.is_ifc then
CONC_ALL(me)
return
end
if me.has_pre then
CODE.pres = CODE.pres .. [[
static void _ceu_pre_]]..me.n..[[ (tceu_app* _ceu_app, tceu_org* __ceu_this) {
]] .. me.blk_ifc[1][1].code_ifc .. [[
}
]]
end
CASE(me, me.lbl)
CONC_ALL(me)
if ANA and me.ana.pos[false] then
return -- never reachable
end
-- stop
if me == MAIN then
LINE(me, [[
#if defined(CEU_RET) || defined(CEU_OS)
_ceu_app->isAlive = 0;
#endif
]])
else
LINE(me, [[
ceu_sys_org_free(_ceu_app, _ceu_org);
#ifdef CEU_ORGS_AWAIT
{
/* signal ok_killed */
#ifdef CEU_STACK_CLEAR
tceu_stk stk_ = *_ceu_stk;
stk_.is_alive = 1;
stk_.down = _ceu_stk;
#endif
tceu_kill ps = { _ceu_org, _ceu_org->ret, 0, (tceu_ntrl)(_ceu_org->n-1) };
tceu_evt evt_;
evt_.id = CEU_IN__ok_killed;
evt_.param = &ps;
ceu_sys_go_ex(_ceu_app, &evt_,
#ifdef CEU_STACK_CLEAR
&stk_,
#else
NULL,
#endif
_ceu_app->data, 0, _ceu_app->data->n);
}
#endif
]])
end
HALT(me)
-- TODO-RESEARCH-2:
end,
-- TODO: C function?
_ORG = function (me, t)
COMM(me, 'start org: '..t.id)
--[[
class T with
<PRE> -- 1 org: me.lbls_pre[i].id
var int v = 0;
do
<BODY> -- 3 org: me.lbls_body[i].id
end
<...> -- 0 parent:
var T t with
<CONSTR> -- 2 org: no lbl (cannot call anything)
end;
<CONT> -- 4 parent:
]]
-- ceu_out_org, _ceu_constr_
local org = t.arr and '((tceu_org*) &'..t.val..'['..t.val_i..']'..')'
or '((tceu_org*) &'..t.val..')'
-- each org has its own trail on enclosing block
if t.arr then
LINE(me, [[
for (]]..t.val_i..[[=0; ]]..t.val_i..'<'..t.arr.sval..';'..t.val_i..[[++)
{
]]) end
LINE(me, [[
/* resets org memory and starts org.trail[0]=Class_XXX */
/* TODO: BUG: _ceu_org is not necessarily the parent for pool allocations */
ceu_out_org_init(_ceu_app, ]]..org..','..t.cls.trails_n..','..t.cls.lbl.id..[[,
]]..t.cls.n..[[,
]]..t.isDyn..[[,
]]..t.parent_org..','..t.parent_trl..[[);
/* TODO: currently idx is always "1" for all interfaces access because pools
* are all together there. When we have separate trls for pools, we'll have to
* indirectly access the offset in the interface. */
]])
-- traverse <...> with
-- var int x = y; // executes in _pre, before the constructor
-- do
if me.__adj_is_traverse_root then
LINE(me, [[
((]]..TP.toc(t.cls.tp)..'*)'..org..[[)->_out =
(__typeof__(((]]..TP.toc(t.cls.tp)..'*)'..org..[[)->_out)) _ceu_org;
]])
elseif me.__adj_is_traverse_rec then
LINE(me, [[
((]]..TP.toc(t.cls.tp)..'*)'..org..[[)->_out =
((]]..TP.toc(t.cls.tp)..[[*)_ceu_org)->_out;
]])
end
if t.cls.has_pre then
LINE(me, [[
_ceu_pre_]]..t.cls.n..[[(_ceu_app, ]]..org..[[);
]])
end
if t.constr then
LINE(me, [[
_ceu_constr_]]..t.constr.n..[[(_ceu_app, ]]..org..[[, _ceu_org);
]])
end
LINE(me, [[
{
#ifdef CEU_STACK_CLEAR
tceu_stk stk_ = { _ceu_stk, _ceu_org, ]]..me.trails[1]..[[, ]]..me.trails[2]..[[, 1 };
ceu_app_go(_ceu_app,NULL,
]]..org..[[, &]]..org..[[->trls[0],
&stk_);
if (!stk_.is_alive) {
return;
}
#else
ceu_app_go(_ceu_app,NULL,
]]..org..[[, &]]..org..[[->trls[0],
NULL);
#endif
]])
if t.set then
LINE(me, [[
if (!]]..org..[[->isAlive) {
]]..V(t.set,'rval')..' = '..string.upper(TP.toc(t.set.tp))..[[_pack(NULL);
}
]])
end
LINE(me, [[
}
]])
if t.arr then
LINE(me, [[
}
]])
end
end,
Dcl_var = function (me)
local _,_,_,constr = unpack(me)
local var = me.var
-- TODO
me.tp = var.tp
if var.cls then
F._ORG(me, {
id = var.id,
isDyn = 0,
cls = var.cls,
val = V(me,'rval'),
constr = constr,
arr = var.tp.arr,
val_i = TP.check(var.tp,'[]') and V({tag='Var',tp=var.tp,var=var.constructor_iterator},'rval'),
parent_org = '_ceu_org',
parent_trl = var.trl_orgs[1],
})
-- TODO: similar code in Block_pre for !BlockI
elseif AST.par(me,'BlockI') and TP.check(var.tp,'?') then
if not var.isTmp then -- ignore unused var
-- has be part of cls_pre to execute before possible binding in constructor
-- initialize to nil
local ID = string.upper(TP.opt2adt(var.tp))
LINE(me, [[
]]..V({tag='Var',tp=var.tp,var=var},'rval')..[[.tag = CEU_]]..ID..[[_NIL;
]])
end
end
end,
Adt_constr_root = function (me)
local dyn, one = unpack(me)
LINE(me, '{')
local set = assert(AST.par(me,'Set'), 'bug found')
local _,_,_,to = unpack(set)
if not dyn then
CONC(me, one)
F.__set(me, one, to)
else
local set = assert(AST.par(me,'Set'), 'bug found')
F.__set_adt_mut_conc_fr(me, set, one)
end
LINE(me, '}')
end,
ExpList = CONC_ALL,
Adt_constr_one = function (me)
local adt, params = unpack(me)
local id, tag = unpack(adt)
adt = assert(ENV.adts[id])
local root = AST.par(me, 'Adt_constr_root')
-- CODE-1: declaration, allocation
-- CODE-2: all children
-- CODE-3: assignment
-- { requires all children }
me.val = '__ceu_adt_'..me.n
-- CODE-1
if not adt.is_rec then
-- CEU_T t;
LINE(me, [[
CEU_]]..id..' '..me.val..[[;
]])
else
-- CEU_T* t;
LINE(me, [[
CEU_]]..id..'* '..me.val..[[;
]])
-- base case
if adt.is_rec and tag==adt.tags[1] then
LINE(me,
me.val..' = &CEU_'..string.upper(id)..[[_BASE;
]])
-- other cases
else
local tp = 'CEU_'..id
-- extract pool from set
-- to.x.y = new z;
-- i.e.,
-- to.root.pool
local set = assert( AST.par(me,'Set'), 'bug found' )
local _,_,_,to = unpack(set)
local pool = ADT.find_pool(to)
pool = '('..V(pool,'lval','adt_top')..'->pool)'
LINE(me, [[
#if defined(CEU_ADTS_NEWS_MALLOC) && defined(CEU_ADTS_NEWS_POOL)
if (]]..pool..[[ == NULL) {
]]..me.val..[[ = (]]..tp..[[*) ceu_out_realloc(NULL, sizeof(]]..tp..[[));
} else {
]]..me.val..[[ = (]]..tp..[[*) ceu_pool_alloc((tceu_pool*)]]..pool..[[);
}
#elif defined(CEU_ADTS_NEWS_MALLOC)
]]..me.val..[[ = (]]..tp..[[*) ceu_out_realloc(NULL, sizeof(]]..tp..[[));
#elif defined(CEU_ADTS_NEWS_POOL)
]]..me.val..[[ = (]]..tp..[[*) ceu_pool_alloc((tceu_pool*)]]..pool..[[);
#endif
]])
-- fallback to base case if fails
LINE(me, [[
if (]]..me.val..[[ == NULL) {
]]..me.val..[[ = &CEU_]]..string.upper(id)..[[_BASE;
} else /* rely on {,} that follows */
]])
end
end
LINE(me, '{') -- will ignore if allocation fails
-- CODE-2
CONC(me, params)
-- CODE-3
local op = (adt.is_rec and '->' or '.')
local blk,_
if tag then
-- t->tag = TAG;
if not (adt.is_rec and tag==adt.tags[1]) then
-- not required for base case
LINE(me, me.val..op..'tag = CEU_'..string.upper(id)..'_'..tag..';')
end
blk = ENV.adts[id].tags[tag].blk
tag = tag..'.'
else
_,_,blk = unpack(ENV.adts[id])
tag = ''
end
for i, p in ipairs(params) do
local field = blk.vars[i]
local amp = ''--(TP.check(field.tp,'&') and '&') or ''
LINE(me, me.val..op..tag..field.id..' = '..amp..V(p,'rval')..';')
end
LINE(me, '}') -- will ignore if allocation fails
end,
Kill = function (me)
local org, exp = unpack(me)
if exp then
LINE(me, [[
((tceu_org*)]]..V(org,'lval')..')->ret = '..V(exp,'rval')..[[;
]])
end
local org_cast = '((tceu_org*)'..V(org,'lval')..')'
LINE(me, [[
{
tceu_stk stk_ = { _ceu_stk, _ceu_org, ]]..me.trails[1]..[[, ]]..me.trails[2]..[[, 1 };
tceu_evt evt;
evt.id = CEU_IN__CLEAR;
ceu_sys_go_ex(_ceu_app, &evt, &stk_,
]]..org_cast..[[, 0, ]]..org_cast..[[->n);
ceu_sys_org_free(_ceu_app,]]..org_cast..[[);
#ifdef CEU_ORGS_AWAIT
/* signal ok_killed */
{
tceu_kill ps = { ]]..org_cast..','..org_cast..'->ret, 0, (tceu_ntrl)('..org_cast..[[->n-1) };
tceu_evt evt_;
evt_.id = CEU_IN__ok_killed;
evt_.param = &ps;
ceu_sys_go_ex(_ceu_app, &evt_, &stk_,
_ceu_app->data, 0, _ceu_app->data->n);
}
if (!stk_.is_alive) {
return;
}
#endif
}
]])
end,
Spawn = function (me)
local id, pool, constr = unpack(me)
local ID = '__ceu_new_'..me.n
local set = AST.par(me, 'Set')
LINE(me, [[
/*{*/
tceu_org* ]]..ID..[[;
]])
if pool and (type(pool.var.tp.arr)=='table') then
-- static
LINE(me, [[
]]..ID..[[ = (tceu_org*) ceu_pool_alloc(&]]..V(pool,'rval')..[[.pool);
]])
elseif TP.check(pool.var.tp,'&&') or TP.check(pool.var.tp,'&') then
-- pointer don't know if is dynamic or static
LINE(me, [[
#if !defined(CEU_ORGS_NEWS_MALLOC)
]]..ID..[[ = (tceu_org*) ceu_pool_alloc(&]]..V(pool,'rval')..[[.pool);
#elif !defined(CEU_ORGS_NEWS_POOL)
]]..ID..[[ = (tceu_org*) ceu_out_realloc(NULL, sizeof(CEU_]]..id..[[));
#else
if (]]..V(pool,'rval')..[[.pool.queue == NULL) {
]]..ID..[[ = (tceu_org*) ceu_out_realloc(NULL, sizeof(CEU_]]..id..[[));
} else {
]]..ID..[[ = (tceu_org*) ceu_pool_alloc(&]]..V(pool,'rval')..[[.pool);
}
#endif
]])
else
-- dynamic
LINE(me, [[
]]..ID..[[ = (tceu_org*) ceu_out_realloc(NULL, sizeof(CEU_]]..id..[[));
]])
end
if set then
local set_to = set[4]
LINE(me, V(set_to,'rval')..' = '..
'('..string.upper(TP.toc(set_to.tp))..'_pack('..
'((CEU_'..id..'*)__ceu_new_'..me.n..')));')
end
LINE(me, [[
if (]]..ID..[[ != NULL) {
]])
--if pool and (type(pool.var.tp.arr)=='table') or
--PROPS.has_orgs_news_pool or OPTS.os then
LINE(me, [[
#ifdef CEU_ORGS_NEWS_POOL
]]..ID..[[->pool = &]]..V(pool,'rval')..[[;
#endif
]])
--end
local org = '_ceu_org'
if pool and pool.org then
org = '((tceu_org*)&'..V(pool.org,'rval')..')'
end
F._ORG(me, {
id = 'dyn',
isDyn = 1,
cls = me.cls,
val = '(*((CEU_'..id..'*)'..ID..'))',
constr = constr,
arr = false,
parent_org = V(pool,'rval')..'.parent_org',
parent_trl = V(pool,'rval')..'.parent_trl',
set = set and set[4]
})
LINE(me, [[
}
/*}*/
]])
end,
Block_pre = function (me)
local cls = CLS()
if (not cls) or cls.is_ifc then
return
end
if me.fins then
LINE(me, [[
/* FINALIZE */
_ceu_org->trls[ ]]..me.trl_fins[1]..[[ ].evt = CEU_IN__CLEAR;
_ceu_org->trls[ ]]..me.trl_fins[1]..[[ ].lbl = ]]..me.lbl_fin.id..[[;
]])
for _, fin in ipairs(me.fins) do
LINE(me, fin.val..' = 0;')
end
end
-- declare tmps
-- initialize pools
-- initialize ADTs base cases
-- initialize Optional types to NIL
LINE(me, '{') -- close in Block_pos
for _, var in ipairs(me.vars) do
if var.isTmp then
local ID = '__ceu_'..var.id..'_'..var.n
if var.id == '_ret' then
LINE(me,'#ifdef CEU_RET\n') -- avoids "unused" warning
end
LINE(me, MEM.tp2dcl(var.pre, var.tp,ID,nil)..';\n')
if var.id == '_ret' then
LINE(me,'#endif\n') -- avoids "unused" warning
end
if var.is_arg then
-- function parameter
-- __ceu_a = a
LINE(me, ID..' = '..var.id..';')
end
end
if var.pre == 'var' then
local tp_id = TP.id(var.tp)
-- OPTION TYPE
if TP.check(var.tp,'?') then
-- TODO: similar code in Dcl_var for BlockI
if me~=cls.blk_ifc or cls.blk_ifc==cls.blk_body then
-- initialize to nil
-- has to execute before org initialization in Dcl_var
local ID = string.upper(TP.opt2adt(var.tp))
LINE(me, [[
]]..V({tag='Var',tp=var.tp,var=var},'rval')..[[.tag = CEU_]]..ID..[[_NIL;
]])
end
-- VECTOR
elseif TP.check(var.tp,'[]') and (not (var.cls or TP.is_ext(var.tp,'_'))) then
local tp_elem = TP.pop( TP.pop(var.tp,'&'), '[]' )
local max = (var.tp.arr.cval or 0)
local ID = (var.isTmp and '__ceu_'..var.id..'_'..var.n) or
CUR(me,var.id_)
local blki = AST.par(var.dcl,'BlockI') or me -- init inside _ceu_pre
F.Node_pre(blki)
LINE(blki, [[
ceu_vector_init(]]..'&'..ID..','..max..',sizeof('..TP.toc(tp_elem)..[[),
(byte*)]]..ID..[[_mem);
]])
if var.tp.arr == '[]' then
LINE(me, [[
/* FINALIZE VECTOR */
_ceu_org->trls[ ]]..var.trl_vector[1]..[[ ].evt = CEU_IN__CLEAR;
_ceu_org->trls[ ]]..var.trl_vector[1]..[[ ].lbl = ]]..(var.lbl_fin_free).id..[[;
]])
end
end
-- OPTION TO ORG or ORG[]
if ENV.clss[tp_id] and TP.check(var.tp,tp_id,'&&','?','-[]') then
-- TODO: repeated with Block_pos
LINE(me, [[
/* RESET OPT-ORG TO NULL */
_ceu_org->trls[ ]]..var.trl_optorg[1]..[[ ].evt = CEU_IN__ok_killed;
_ceu_org->trls[ ]]..var.trl_optorg[1]..[[ ].lbl = ]]..(var.lbl_optorg_reset).id..[[;
#ifdef CEU_ORGS_AWAIT
#ifdef CEU_ADTS_AWAIT
_ceu_org->trls[ ]]..var.trl_optorg[1]..[[ ].is_org = 1;
#endif
#endif
_ceu_org->trls[ ]]..var.trl_optorg[1]..[[ ].org_or_adt = NULL;
]])
end
elseif var.pre=='pool' and (var.cls or var.adt) then
-- real pool (not reference or pointer)
local cls = var.cls
local adt = var.adt
local top = cls or adt
local is_dyn = (var.tp.arr=='[]')
local tp_id = TP.id(var.tp)
if top or tp_id=='_TOP_POOL' then
local id = (adt and '_' or '') .. var.id_
if (not is_dyn) then
local tp_id_ = 'CEU_'..tp_id..(top.is_ifc and '_delayed' or '')
local pool
if cls then
pool = CUR(me,id)..'.pool'
local trl = assert(var.trl_orgs,'bug found')[1]
LINE(me, [[
]]..CUR(me,id)..[[.parent_org = _ceu_org;
]]..CUR(me,id)..[[.parent_trl = ]]..trl..[[;
]])
else
pool = CUR(me,id)
end
LINE(me, [[
ceu_pool_init(&]]..pool..','..var.tp.arr.sval..',sizeof('..tp_id_..[[),
(byte**)&]]..CUR(me,id)..'_queue, (byte*)&'..CUR(me,id)..[[_mem);
]])
elseif cls or tp_id=='_TOP_POOL' then
local trl = assert(var.trl_orgs,'bug found')[1]
LINE(me, [[
(]]..CUR(me,id)..[[).parent_org = _ceu_org;
(]]..CUR(me,id)..[[).parent_trl = ]]..trl..[[;
#ifdef CEU_ORGS_NEWS_POOL
(]]..CUR(me,id)..[[).pool.queue = NULL; /* dynamic pool */
#endif
]])
end
end
-- real pool
if adt and adt.is_rec then
-- create base case NIL and assign to "*l"
local tag = unpack( AST.asr(adt,'Dcl_adt', 3,'Dcl_adt_tag') )
local tp = 'CEU_'..adt.id
-- base case: use preallocated static variable
assert(adt.is_rec and tag==adt.tags[1], 'bug found')
local VAL_all = V({tag='Var',tp=var.tp,var=var}, 'lval','adt_top')
local VAL_pool = V({tag='Var',tp=var.tp,var=var}, 'lval','adt_pool')
if (not is_dyn) then
LINE(me, [[
#ifdef CEU_ADTS_NEWS_POOL
]]..VAL_all..[[->pool = ]]..VAL_pool..[[;
#endif
]])
else
LINE(me, [[
#ifdef CEU_ADTS_NEWS_POOL
]]..VAL_all..[[->pool = NULL;
#endif
]])
end
LINE(me, [[
]]..VAL_all..[[->root = &CEU_]]..string.upper(adt.id)..[[_BASE;
/* FINALIZE ADT */
_ceu_org->trls[ ]]..var.trl_adt[1]..[[ ].evt = CEU_IN__CLEAR;
_ceu_org->trls[ ]]..var.trl_adt[1]..[[ ].lbl = ]]..var.lbl_fin_kill_free.id..[[;
]])
end
end
-- initialize trails for ORG_STATS_I & ORG_POOL_I
-- "first" avoids repetition for STATS in sequence
-- TODO: join w/ ceu_out_org (removing start from the latter?)
if var.trl_orgs and var.trl_orgs_first then
LINE(me, [[
#ifdef CEU_ORGS
_ceu_org->trls[ ]]..var.trl_orgs[1]..[[ ].evt = CEU_IN__ORG;
_ceu_org->trls[ ]]..var.trl_orgs[1]..[[ ].org = NULL;
#endif
]])
end
end
end,
Block_pos = function (me)
local stmts = unpack(me)
local cls = CLS()
if (not cls) or cls.is_ifc then
return
end
if stmts.trails[1] ~= me.trails[1] then
LINE(me, [[
_ceu_trl = &_ceu_org->trls[ ]]..stmts.trails[1]..[[ ];
]])
end
CONC(me, stmts)
CLEAR(me)
LINE(me, [[
if (0) {
]])
if me.fins then
CASE(me, me.lbl_fin)
for i, fin in ipairs(me.fins) do
LINE(me, [[
if (]]..fin.val..[[) {
]] .. fin.code .. [[
}
]])
end
HALT(me)
end
for _, var in ipairs(me.vars) do
local is_arr = (TP.check(var.tp,'[]') and
(var.pre == 'var') and
(not TP.is_ext(var.tp,'_','@'))) and
(not var.cls)
local is_dyn = (var.tp.arr=='[]')
local tp_id = TP.id(var.tp)
if ENV.clss[tp_id] and TP.check(var.tp,tp_id,'&&','?','-[]') then
assert(var.pre ~= 'pool')
-- TODO: review both cases (vec vs no-vec)
-- possible BUG: pointer is tested after free
CASE(me, var.lbl_optorg_reset)
local tp_opt = TP.pop(var.tp,'[]')
local ID = string.upper(TP.opt2adt(tp_opt))
if TP.check(var.tp,'[]') then
local val = V({tag='Var',tp=var.tp,var=var}, 'lval')
LINE(me, [[
{
int __ceu_i;
for (__ceu_i=0; __ceu_i<ceu_vector_getlen(]]..val..[[); __ceu_i++) {
]]..TP.toc(tp_opt)..[[* __ceu_one = (]]..TP.toc(tp_opt)..[[*)
ceu_vector_geti(]]..val..[[, __ceu_i);
tceu_kill* __ceu_casted = (tceu_kill*)_ceu_evt->param;
if ( (__ceu_one->tag != CEU_]]..ID..[[_NIL) &&
(ceu_org_is_cleared((tceu_org*)__ceu_one->SOME.v,
(tceu_org*)__ceu_casted->org_or_adt,
__ceu_casted->t1,
__ceu_casted->t2)) )
{
__ceu_one->tag = CEU_]]..ID..[[_NIL;
/*
]]..TP.toc(tp_opt)..[[ __ceu_new = ]]..string.upper(TP.toc(tp_opt))..[[_pack(NULL);
ceu_vector_seti(]]..val..[[,__ceu_i, (byte*)&__ceu_new);
*/
}
}
}
]])
else
local val = V({tag='Var',tp=var.tp,var=var}, 'rval')
LINE(me, [[