forked from ceu-lang/ceu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.lua
More file actions
1300 lines (1140 loc) · 33.5 KB
/
Copy pathcode.lua
File metadata and controls
1300 lines (1140 loc) · 33.5 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
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"
threads = '',
functions = '',
stubs = '', -- maps input functions to ceu_app_call switch cases
native = '',
}
-- 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 ATTR (me, n1, n2)
LINE(me, V(n1)..' = '..V(n2)..';')
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
fprintf(stderr, "\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 HALT (me, ret, cond)
if cond then
LINE(me, 'if ('..cond..') {')
end
--LINE(me, '\tgoto _CEU_NEXT_;')
if ret then
LINE(me, '\treturn '..ret..';')
else
LINE(me, '\treturn RET_HALT;')
end
if cond then
LINE(me, '}')
end
end
function GOTO (me, lbl, org)
_CODE.has_goto = true
if org then
LINE(me, [[
_ceu_go->org = ]]..org..[[;
]])
end
LINE(me, [[
_ceu_go->lbl = ]]..lbl.id..[[;
goto _CEU_GOTO_;
/*return RET_GOTO;*/
]])
end
function AWAIT_PAUSE (me, no)
if not _PROPS.has_pses then
return
end
for pse in _AST.iter'Pause' do
COMM(me, 'PAUSE: '..pse.dcl.var.id)
LINE(me, [[
if (]]..V(pse.dcl.var)..[[) {
]])
if me.tag == 'AwaitInt' then
LINE(me, [[
_ceu_go->trl->seqno = _ceu_app->seqno-1; /* awake again */
]])
end
LINE(me, [[
goto ]]..no..[[;
}
]])
end
end
function COMM (me, comm)
LINE(me, '/* '..comm..' */', 0)
end
local _iter = function (n)
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
function CLEAR (me)
COMM(me, 'CLEAR: '..me.tag..' ('..me.ln[2]..')')
if not me.needs_clr then
return
end
-- TODO: put it back!
--[[
-- check if top will clear during same reaction
if (not me.needs_clr_fin) and _ANA then -- fin must execute before any stmt
local top = _AST.iter(_iter)()
if top and _ANA.CMP(top.ana.pos, me.ana.pos) then
return -- top will clear
end
end
]]
--LINE(me, 'ceu_trails_clr('..me.trails[1]..','..me.trails[2]..
--', _ceu_go->org);')
LINE(me, [[
/* trails[1] points to ORG blk */
{
tceu_trl* trl = &_ceu_go->org->trls[ ]]..me.trails[1]..[[ ];
trl->evt = CEU_IN__STK;
trl->stk = _ceu_go->stki;
trl->lbl = ]]..me.lbl_clr.id..[[;
}
_ceu_go->stk[_ceu_go->stki ].evtp = _ceu_go->evtp;
#ifdef CEU_INTS
#ifdef CEU_ORGS
_ceu_go->stk[_ceu_go->stki ].evto = _ceu_go->evto;
#endif
#endif
_ceu_go->stk[_ceu_go->stki++].evt = _ceu_go->evt;
/* [ trails[1]+1, trails[2] [ */
_ceu_go->trl = &_ceu_go->org->trls[ ]]..(me.trails[1]+1)..[[ ];
/* trails[1]+1 is in */
_ceu_go->stop = &_ceu_go->org->trls[ ]]..(me.trails[2]+1)..[[ ];
/* trails[2]+1 is out */
_ceu_go->evt = CEU_IN__CLEAR;
/*goto _CEU_CALL_TRL_;*/
return RET_TRL;
case ]]..me.lbl_clr.id..[[:;
]])
end
F = {
Node_pre = function (me)
me.code = ''
end,
Host = function (me)
-- unescape `##´ => `#´
local src = string.gsub(me[1], '^%s*##', '#')
src = string.gsub(src, '\n%s*##', '\n#')
_CODE.native = _CODE.native .. [[
#line ]]..me.ln[2]..' "'..me.ln[1]..[["
]] .. src
end,
Do = CONC_ALL,
Finally = CONC_ALL,
Dcl_constr = CONC_ALL,
Stmts = function (me)
LINE(me, '{') -- allows C declarations for New/Spawn
CONC_ALL(me)
LINE(me, '}')
end,
Root = function (me)
for _, cls in ipairs(_ENV.clss_cls) do
CONC(me, cls)
end
-- functions and threads receive __ceu_org as parameter
-- and do not require _ceu_go
_CODE.functions = string.gsub(_CODE.functions, '_ceu_go%-%>org', '__ceu_org')
_CODE.threads = string.gsub(_CODE.threads, '_ceu_go%-%>org', '__ceu_org')
-- assert that all input functions have bodies
for evt, v in pairs(INPUT_FUNCTIONS) do
ASR(v, evt.ln, 'missing 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_go%-%>org', '__ceu_org')
me.code = ''
end,
Dcl_fun = function (me)
local _, _, ins, out, id, blk = unpack(me)
if blk then
if me.var.fun.isExt then
_CODE.functions = _CODE.functions ..
'#define ceu_in_call_'..id..' '..me.id..'\n'
local ps = {}
if #ins > 1 then
for i, _ in ipairs(ins) do
ps[#ps+1] = ', (('..ins.tp..'*)param.ptr)->_'..i
end
elseif #ins == 1 then
local _,tp,_ = unpack(ins[1])
if tp == 'int' then
ps[#ps+1] = ', param.v'
else
ps[#ps+1] = ', param.ptr'
end
else
-- no parameters
end
ps = table.concat(ps)
local ret_value, ret_void
if out == 'void' then
ret_value = ''
ret_void = 'return (tceu_evtp)NULL;'
else
ret_value = 'return (tceu_evtp)'
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
end
_CODE.functions = _CODE.functions ..
me.proto..'{'..blk.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) or '')..';')
end,
Dcl_cls = function (me)
if me.is_ifc then
CONC_ALL(me)
return
end
CASE(me, me.lbl)
-- TODO: move to _ORG? (_MAIN does not call _ORG)
LINE(me, [[
#ifdef CEU_IFCS
_ceu_go->org->cls = ]]..me.n..[[;
#endif
]])
CONC_ALL(me)
-- TODO(rom): avoid clss w/o new
--if i_am_instantiable then
LINE(me, [[
#ifdef CEU_NEWS
if (_ceu_go->org->isSpw) {
]])
F.Free(me)
LINE(me, [[
}
#endif
]])
--end
if not (_ANA and me.ana.pos[false]) then
if me == _MAIN then
HALT(me, 'RET_END')
else
HALT(me)
end
end
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 par:
var T t with
<CONSTR> -- 2 org: no lbl (cannot call anything)
end;
<CONT> -- 4 par: me.lbls_cnt[i].id
]]
-- TODO: split in two loops:
-- In C: init, pre, constr
-- In Lua: body
-- each org has its own trail on enclosing block
for i=1, (t.arr and t.arr.sval or 1) do
local org = t.arr and
'((tceu_org*) '..t.val..'['..(i-1)..']'..')'
or
'((tceu_org*) '..t.val..')'
LINE(me, [[
/* resets org memory and starts org.trail[0]=Class_XXX */
ceu_out_org_init(_ceu_app, ]]..org..[[, ]]
..t.cls.trails_n..','
..t.cls.lbl.id..[[,
_ceu_go->stki+1, /* run now */
_ceu_go->org, ]]..t.par_trl_idx..[[);
#ifdef CEU_NEWS
]]..org..[[->isDyn = ]]..t.isDyn..[[;
]]..org..[[->isSpw = ]]..t.isSpw..[[;
#endif
]])
-- PRE & CONSTR
LINE(me, [[
{
tceu_org* __ceu_org = ]]..org..[[;
]])
if t.cls.has_pre then
LINE(me, t.cls.blk_ifc[1][1].code_ifc) -- Block->Stmts->BlockI
end
if t.constr then
CONC(me, t.constr) -- constructor before executing
end
LINE(me, [[
}
]])
-- BODY
LINE(me, [[
/* hold current blk trail: set to my continuation */
_ceu_go->trl->evt = CEU_IN__STK;
_ceu_go->trl->lbl = ]]..me.lbls_cnt[i].id..[[;
_ceu_go->trl->stk = _ceu_go->stki;
_ceu_go->stk[_ceu_go->stki ].evtp = _ceu_go->evtp;
#ifdef CEU_INTS
#ifdef CEU_ORGS
_ceu_go->stk[_ceu_go->stki ].evto = _ceu_go->evto;
#endif
#endif
_ceu_go->stk[_ceu_go->stki++].evt = _ceu_go->evt;
/* switch to ORG */
]]..org..[[->trls[0].evt = CEU_IN__STK;
]]..org..[[->trls[0].lbl = ]]..t.cls.lbl.id..[[;
]]..org..[[->trls[0].stk = _ceu_go->stki;
_ceu_go->org = ]]..org..[[;
_ceu_go->stop = &_ceu_go->org->trls[_ceu_go->org->n]; /* don't follow the up link */
/*goto _CEU_CALL_ORG_;*/
return RET_ORG;
case ]]..me.lbls_cnt[i].id..[[:;
]])
end
end,
Dcl_var = function (me)
local _,_,_,_,constr = unpack(me)
local var = me.var
if not var.cls then
return
end
F._ORG(me, {
id = var.id,
isDyn = 0,
isSpw = 0,
cls = var.cls,
val = '&'..var.val,
constr = constr,
arr = var.arr,
par_trl_idx = var.blk.trl_orgs[1],
})
end,
Spawn = 'New',
New = function (me)
local _, id, constr, set = unpack(me)
local pool = me.pool and CUR(me,me.pool) or me.cls.pool
LINE(me, [[
{
tceu_org* __ceu_new;
]])
if pool then
LINE(me, [[
__ceu_new = (tceu_org*) ceu_pool_alloc(&]]..pool..[[);
]])
else
LINE(me, [[
__ceu_new = (tceu_org*) ceu_out_malloc(_ceu_app, sizeof(]].._TP.c(id)..[[));
]])
end
if set then
CONC(me, set) -- <ptr=new T>, <ok=Spawn T>
end
LINE(me, [[
if (__ceu_new != NULL) {
]])
if pool then
LINE(me, '__ceu_new->pool = &'..pool..';')
elseif _PROPS.has_news_pool then
LINE(me, '__ceu_new->pool = NULL;')
end
F._ORG(me, {
id = 'dyn',
isDyn = 1,
isSpw = (me.tag=='New' and 0) or 1,
cls = me.cls,
val = '__ceu_new',
constr = constr,
arr = false,
par_trl_idx = me.blk.trl_orgs[1],
})
LINE(me, [[
}
}
]])
end,
Free = function (me)
local exp = unpack(me)
local cls, val
if me.tag == 'Free' then
cls = me.cls
val = V(exp)
else -- Dcl_cls
cls = me
val = '_ceu_go->org'
end
local lbls = table.concat(cls.lbls,',')
LINE(me, [[
{
tceu_org* __ceu_tofree = (tceu_org*) ]]..val..[[;
if (__ceu_tofree != NULL)
{
/* TODO: assert isDyn */
]])
if me.tag == 'Free' then
-- only if freeing someone else
LINE(me, [[
/* save my continuation */
_ceu_go->trl->evt = CEU_IN__STK;
_ceu_go->trl->stk = _ceu_go->stki;
_ceu_go->trl->lbl = ]]..me.lbl_clr.id..[[;
]])
end
LINE(me, [[
/* clear all __ceu_tofree [ trls[0], ... [ */
/* this will call free() */
_ceu_go->stop = __ceu_tofree;
_ceu_go->trl = &__ceu_tofree->trls[0];
]])
if me.tag == 'Free' then -- (or __ceu_tofree is already me)
LINE(me, [[
_ceu_go->org = __ceu_tofree;
]])
end
LINE(me, [[
_ceu_go->stk[_ceu_go->stki ].evtp = _ceu_go->evtp;
#ifdef CEU_INTS
#ifdef CEU_ORGS
_ceu_go->stk[_ceu_go->stki ].evto = _ceu_go->evto;
#endif
#endif
_ceu_go->stk[_ceu_go->stki++].evt = _ceu_go->evt;
_ceu_go->evt = CEU_IN__CLEAR;
/*goto _CEU_CALL_TRL_;*/
return RET_TRL;
}
}
case ]]..me.lbl_clr.id..[[:;
]])
end,
Block_pre = function (me)
local cls = CLS()
if cls.is_ifc then
return
end
if me.trl_orgs then
LINE(me, [[
_ceu_go->org->trls[ ]]..me.trl_orgs[1]..[[ ].evt = CEU_IN__ORG;
_ceu_go->org->trls[ ]]..me.trl_orgs[1]..[[ ].lnks =
(tceu_org_lnk*) &]]..me.trl_orgs.val..[[;
]]..me.trl_orgs.val..'[0].nxt = (tceu_org*) &'..me.trl_orgs.val..'[1]'..[[;
]]..me.trl_orgs.val..'[1].prv = (tceu_org*) &'..me.trl_orgs.val..'[0]'..[[;
]]..me.trl_orgs.val..'[1].nxt = '..[[_ceu_go->org;
]]..me.trl_orgs.val..'[1].n = '..[[0;
]]..me.trl_orgs.val..'[1].lnk = '..me.trl_orgs[1]..[[+1;
]])
end
if me.fins then
LINE(me, [[
/* FINALIZE */
_ceu_go->org->trls[ ]]..me.trl_fins[1]..[[ ].evt = CEU_IN__CLEAR;
_ceu_go->org->trls[ ]]..me.trl_fins[1]..[[ ].lbl = ]]..me.lbl_fin.id..[[;
_ceu_go->org->trls[ ]]..me.trl_fins[1]..[[ ].seqno = _ceu_app->seqno-1; /* awake now */
]])
for _, fin in ipairs(me.fins) do
LINE(me, fin.val..' = 0;')
end
end
-- initialize pools for new/spawn
if me.pools then
for node, n in pairs(me.pools) do
local pre = CUR(me,node.pool)
LINE(me, [[
ceu_pool_init(&]]..pre..', '..n..', sizeof(CEU_'..node.cls.id..'), '
..'(char**)'..pre..'_queue, (char*)'..pre..[[_mem);
]])
end
end
-- declare tmps
LINE(me, '{') -- close in Block_pos
for _, var in ipairs(me.vars) do
if var.isTmp then
if var.arr then
LINE(me, _TP.c(_TP.deref(var.tp))
..' '..V(var)..'['..V(var.arr)..']')
else
LINE(me, _TP.c(var.tp)..' '..V(var))
end
if var.isFun then
-- __ceu_a = a
LINE(me, ' = '..var.id)
end
LINE(me, ';')
end
end
end,
Block_pos = function (me)
local stmts = unpack(me)
if CLS().is_ifc then
return
end
-- TODO: try to remove this need
if me.trails[1] ~= stmts.trails[1] then
LINE(me, [[
/* switch to blk trail */
_ceu_go->trl = &_ceu_go->org->trls[ ]]..stmts.trails[1]..[[ ];
]])
end
CONC(me, blk)
if me.fins then
GOTO(me, me.lbl_fin_cnt)
CASE(me, me.lbl_fin)
for i, fin in ipairs(me.fins) do
LINE(me, [[
if (]]..fin.val..[[) {
]] .. fin.code .. [[
}
]])
end
HALT(me)
CASE(me, me.lbl_fin_cnt)
end
CLEAR(me)
LINE(me, '}') -- open in Block_pre
-- TODO: remove!
if not (_ANA and me.ana.pos[false]) then
LINE(me, [[
/* switch to 1st trail */
/* TODO: only if not joining with outer prio */
/*_ceu_go->trl = &_ceu_go->org->trls[ ]]..me.trails[1]..[[ ]; */
]])
end
end,
Pause = CONC_ALL,
-- TODO: meaningful name
PauseX = function (me)
local psed = unpack(me)
LINE(me, [[
ceu_pause(&_ceu_go->org->trls[ ]]..me.blk.trails[1]..[[ ],
&_ceu_go->org->trls[ ]]..me.blk.trails[2]..[[ ],
]]..psed..[[);
]])
end,
-- TODO: more tests
Op2_call_pre = function (me)
local _, f, exps, fin = unpack(me)
if fin and fin.active then
LINE(_AST.iter'Stmts'(), fin.val..' = 1; /* XXX */')
end
end,
Finalize = function (me)
-- enable finalize
local set,fin = unpack(me)
if fin.active then
LINE(me, fin.val..' = 1;')
end
if set then
CONC(me, set)
end
end,
SetExp = function (me)
local _, fr, to, fin = unpack(me)
COMM(me, 'SET: '..tostring(to[1])) -- Var or C
ATTR(me, to, fr)
if to.tag=='Var' and to.var.id=='_ret' then
LINE(me, [[
#ifdef CEU_RET
_ceu_app->ret = ]]..V(to)..[[;
#endif
]])
end
-- enable finalize
if fin and fin.active then
LINE(me, fin.val..' = 1;')
end
end,
SetBlock_pos = function (me)
local blk,_ = unpack(me)
CONC(me, blk)
HALT(me) -- must escape with `escape´
CASE(me, me.lbl_out)
if me.has_escape then
CLEAR(me)
LINE(me, [[
/* switch to 1st trail */
/* TODO: only if not joining with outer prio */
_ceu_go->trl = &_ceu_go->org->trls[ ]] ..me.trails[1]..[[ ];
]])
end
end,
Escape = function (me)
GOTO(me, _AST.iter'SetBlock'().lbl_out)
end,
_Par = function (me)
-- Ever/Or/And spawn subs
COMM(me, me.tag..': spawn subs')
for i, sub in ipairs(me) do
if i > 1 then
LINE(me, [[
{
tceu_trl* trl = &_ceu_go->org->trls[ ]]..sub.trails[1]..[[ ];
trl->evt = CEU_IN__STK;
trl->lbl = ]]..me.lbls_in[i].id..[[;
trl->stk = _ceu_go->stki;
}
]])
end
end
end,
ParEver = function (me)
F._Par(me)
for i, sub in ipairs(me) do
if i > 1 then
CASE(me, me.lbls_in[i])
end
CONC(me, sub)
-- only if trail terminates
if not sub.ana.pos[false] then
HALT(me)
end
end
end,
ParOr_pos = function (me)
F._Par(me)
for i, sub in ipairs(me) do
if i > 1 then
CASE(me, me.lbls_in[i])
end
CONC(me, sub)
if not (_ANA and sub.ana.pos[false]) then
COMM(me, 'PAROR JOIN')
GOTO(me, me.lbl_out)
end
end
if not (_ANA and me.ana.pos[false]) then
CASE(me, me.lbl_out)
CLEAR(me)
LINE(me, [[
/* switch to 1st trail */
/* TODO: only if not joining with outer prio */
_ceu_go->trl = &_ceu_go->org->trls[ ]]..me.trails[1]..[[ ];
]])
end
end,
ParAnd = function (me)
-- close AND gates
COMM(me, 'close ParAnd gates')
for i=1, #me do
LINE(me, V(me)..'_'..i..' = 0;')
end
F._Par(me)
for i, sub in ipairs(me) do
if i > 1 then
CASE(me, me.lbls_in[i])
end
CONC(me, sub)
LINE(me, V(me)..'_'..i..' = 1;')
GOTO(me, me.lbl_tst)
end
-- AFTER code :: test gates
CASE(me, me.lbl_tst)
for i, sub in ipairs(me) do
HALT(me, nil, '!'..V(me)..'_'..i)
end
LINE(me, [[
/* switch to 1st trail */
/* TODO: only if not joining with outer prio */
_ceu_go->trl = &_ceu_go->org->trls[ ]]..me.trails[1]..[[ ];
]])
end,
If = function (me)
local c, t, f = unpack(me)
-- TODO: If cond assert(c==ptr or int)
LINE(me, [[
if (]]..V(c)..[[) {
]] ..t.code..[[
} else {
]] ..f.code..[[
}
]])
end,
Loop_pos = function (me)
local body = unpack(me)
LINE(me, [[
for (;;) {
]])
CONC(me)
local async = _AST.iter'Async'()
if async then
LINE(me, [[
#ifdef ceu_out_pending
if (ceu_out_pending()) {
#else
{
#endif
_ceu_go->trl->evt = CEU_IN__ASYNC;
_ceu_go->trl->lbl = ]]..me.lbl_asy.id..[[;
]])
HALT(me, 'RET_ASYNC')
LINE(me, [[
}
case ]]..me.lbl_asy.id..[[:;
]])
end
LINE(me, [[
}
]])
if me.has_break and ( not (_AST.iter(_AST.pred_async)()
or _AST.iter'Dcl_fun'()) )
then
CLEAR(me)
LINE(me, [[
/* switch to 1st trail */
/* TODO: only if not joining with outer prio */
_ceu_go->trl = &_ceu_go->org->trls[ ]]..me.trails[1]..[[ ];
]])
end
end,
Break = function (me)
LINE(me, 'break;')
end,
CallStmt = function (me)
local call = unpack(me)
LINE(me, V(call)..';')
end,
EmitExt = function (me)
local op, ext, param = unpack(me)
local evt = ext.evt
if evt.pre~='input' or op~='emit' then
if not me.__ast_set then
LINE(me, V(me)..';') -- already on <v = emit E>
end
return
end
-- emit INPUT
-- only async's need to split in two (to avoid stack growth)
if _AST.iter'Async'() then
LINE(me, [[
_ceu_go->trl->evt = CEU_IN__ASYNC;
_ceu_go->trl->lbl = ]]..me.lbl_cnt.id..[[;
]])
end
if _AST.iter'Thread'() then
-- HACK_2: never terminates
error'not supported'
else
LINE(me, V(me)..[[;
#if defined(CEU_RET) || defined(CEU_OS)
if (! _ceu_app->isAlive)
return RET_END;
#endif
]])
end
if _AST.iter'Async'() then
HALT(me, 'RET_ASYNC')
LINE(me, [[
case ]]..me.lbl_cnt.id..[[:;
]])
end
end,
EmitT = function (me)
local exp = unpack(me)
-- only async's need to split in two (to avoid stack growth)
if _AST.iter'Async'() then
LINE(me, [[
_ceu_go->trl->evt = CEU_IN__ASYNC;
_ceu_go->trl->lbl = ]]..me.lbl_cnt.id..[[;
]])
end
local emit = [[
{
ceu_out_go(_ceu_app, CEU_IN__WCLOCK, (tceu_evtp)(]]..V(exp)..[[));
while (
#if defined(CEU_RET) || defined(CEU_OS)
_ceu_app->isAlive &&
#endif
_ceu_app->wclk_min<=0) {
ceu_out_go(_ceu_app, CEU_IN__WCLOCK, (tceu_evtp)0);
}
#if defined(CEU_RET) || defined(CEU_OS)
if (! _ceu_app->isAlive)
return RET_END;
#endif
}
]]
if _AST.iter'Thread'() then
emit = 'CEU_ATOMIC( '..emit..' )\n'
end
LINE(me, [[
#ifdef CEU_WCLOCKS
]]..emit..[[
#endif
]])
if _AST.iter'Async'() then
HALT(me, 'RET_ASYNC')
LINE(me, [[
case ]]..me.lbl_cnt.id..[[:;
]])
end
end,
EmitInt = function (me)
local _, int, exp = unpack(me)
-- [ ... | me=stk | ... | oth=stk ]
LINE(me, [[
_ceu_go->stk[_ceu_go->stki].evtp = _ceu_go->evtp;
#ifdef CEU_INTS
#ifdef CEU_ORGS
_ceu_go->stk[_ceu_go->stki].evto = _ceu_go->evto;
#endif
#endif
_ceu_go->stk[_ceu_go->stki].evt = _ceu_go->evt; /* 3rd (stk) other trails */
_ceu_go->trl->evt = CEU_IN__STK;
_ceu_go->trl->stk = _ceu_go->stki++; /* 2nd (stk) me */
_ceu_go->trl->lbl = ]]..me.lbl_cnt.id..[[;
/* 1st (stk+1) my lsts */
/* TRIGGER EVENT */
_ceu_go->evt = ]]..(int.ifc_idx or int.var.evt.idx)..[[;
#ifdef CEU_ORGS
_ceu_go->evto = (tceu_org*) ]]..((int.org and int.org.val) or '_ceu_go->org')..[[;
#endif
]])
if exp then
local field = _TP.deref(exp.tp) and 'ptr' or 'v'
LINE(me, [[
_ceu_go->evtp.]]..field..' = '..V(exp)..[[;
]])
end
LINE(me, [[
#ifdef CEU_ORGS
_ceu_go->org = _ceu_app->data; /* TODO(speed): check if is_ifc */
#endif
/*goto _CEU_CALL_ORG_;*/
return RET_ORG;
case ]]..me.lbl_cnt.id..[[:;
]])
end,
AwaitN = function (me)
HALT(me)
end,
AwaitT = function (me)
local exp = unpack(me)
local no = '_CEU_NO_'..me.n..'_'
LINE(me, [[
ceu_trails_set_wclock(_ceu_app, &]]..me.val_wclk..[[, (s32)]]..V(exp)..[[);
]]..no..[[:
_ceu_go->trl->evt = CEU_IN__WCLOCK;
_ceu_go->trl->lbl = ]]..me.lbl.id..[[;
]])
HALT(me)