forked from totalspectrum/spin2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutcpp.c
More file actions
1233 lines (1152 loc) · 38.9 KB
/
Copy pathoutcpp.c
File metadata and controls
1233 lines (1152 loc) · 38.9 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
//
// C++ source code output for spin2cpp
//
// Copyright 2012-2023 Total Spectrum Software Inc.
// see the file COPYING for conditions of redistribution
//
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#include <ctype.h>
#include "spinc.h"
#include "outcpp.h"
#include "becommon.h"
/*
* print out a declaration for the dat[] array
* "tail" is appended to the declaration
* if "classname" is TRUE, then add the class name as well
*/
static void
PrintDatArrayName(Flexbuf *f, Module *parse, const char *tail, bool classname)
{
char *datname = parse->datname;
flexbuf_printf(f, "unsigned char ");
if (parse->datannotations) {
PrintAnnotationList(f, parse->datannotations, ' ');
}
if (classname) {
flexbuf_printf(f, "%s::", parse->classname);
}
flexbuf_printf(f, "%s[]%s", datname, tail);
}
/*
* print out a comment string
*/
void
PrintCommentString(Flexbuf *f, const char *str, int indent)
{
int numLines = 0;
int lastNewLine = 0;
const char *p;
if (!str) return;
for (p = str; *p; p++) {
if (*p =='\n') {
numLines++; lastNewLine = 1;
} else {
lastNewLine = 0;
}
}
if (indent > 0)
flexbuf_printf(f, "%*c", indent, ' ');
if (numLines == 1 && lastNewLine == 1) {
flexbuf_printf(f, "//");
// C++ comments look much better with a space after the //; Spin
// comments don't matter so much, so people leave them out.
// optionally add the space
if (!isspace(*str)) {
flexbuf_printf(f, " ");
}
flexbuf_printf(f, "%s", str); // note the string ended in a newline
return;
}
flexbuf_printf(f, "/* ");
while (str[0]) {
if (str[0] == '*' && str[1] == '/') {
flexbuf_putc('*', f);
flexbuf_putc('%', f);
str += 2;
} else {
flexbuf_putc(str[0], f);
lastNewLine = (str[0] == '\n');
if (lastNewLine && indent > 0) {
flexbuf_printf(f, "%*c", indent, ' ');
}
str++;
}
}
if (numLines > 0 && !lastNewLine) {
flexbuf_printf(f, "\n");
if (indent > 0)
flexbuf_printf(f, "%*c", indent, ' ');
}
flexbuf_printf(f, " */\n");
}
/*
* print out a list of comments
*/
void
PrintIndentedComment(Flexbuf *f, AST *ast, int indent)
{
while (ast) {
if (ast->kind == AST_SRCCOMMENT) {
LineInfo *info = GetLineInfo(ast);
if (info && info->linedata) {
flexbuf_printf(f, "// %s", info->linedata);
}
} else {
if (ast->kind != AST_COMMENT) {
ERROR(ast, "Internal error, expected comment");
return;
}
PrintCommentString(f, ast->d.string, indent);
}
ast = ast->right;
}
}
/*
* print out a header file
*/
/*
* print constant declarations
*/
static void
PrintConstant(Flexbuf *f, AST *ast)
{
if (IsFloatConst(ast)) {
PrintFloat(f, EvalConstExpr(ast), PRINTEXPR_DEFAULT);
return;
}
PrintExpr(f, ast, PRINTEXPR_DEFAULT);
}
static void
PrintConstantDecl(Flexbuf *f, AST *ast)
{
const char *name = ast->d.string;
AST *expr = NULL;
Symbol *sym;
if (!IsConstExpr(ast)) {
ERROR(ast, "%s is not constant", name);
return;
}
sym = LookupSymbol(name);
if (!sym) {
ERROR(ast, "constant symbol %s not declared??", name);
return;
}
expr = (AST *)sym->v.ptr;
if (gl_output == OUTPUT_C || gl_gas_dat) {
flexbuf_printf(f, "#define ");
PrintSymbol(f, sym, PRINTEXPR_USECONST);
flexbuf_printf(f, " (");
PrintConstant(f, expr);
flexbuf_printf(f, ")\n");
} else {
flexbuf_printf(f, " static const int %s = ", ast->d.string);
PrintConstant(f, expr);
flexbuf_printf(f, ";\n");
}
}
static int
PrintAllVarListsOfSize(Flexbuf *f, Module *parse, int siz, int flags)
{
AST *ast;
AST *upper;
AST *comment;
AST *idlist;
AST *typ;
int n = 0;
int astsiz;
for (upper = parse->finalvarblock; upper; upper = upper->right) {
if (upper->kind != AST_LISTHOLDER) {
ERROR(upper, "Internal error, expected listholder");
return n;
}
ast = upper->left;
comment = NULL;
if (ast->kind == AST_COMMENTEDNODE) {
comment = ast->right;
ast = ast->left;
}
switch(ast->kind) {
case AST_BYTELIST:
astsiz = 1;
typ = ast_type_byte;
idlist = ast->left;
break;
case AST_WORDLIST:
astsiz = 2;
typ = ast_type_word;
idlist = ast->left;
break;
case AST_LONGLIST:
astsiz = 4;
typ = NULL;
idlist = ast->left;
break;
case AST_DECLARE_VAR:
typ = ast->left;
astsiz = TypeSize(typ);
idlist = ast->right;
if (IsClassType(typ)) {
flags &= ~ISVOLATILE;
}
break;
default:
ERROR(ast, "Internal error, Unexpected declaration");
return n;
}
if (astsiz == siz || ( (siz == 4 || siz == 0) && (astsiz == 0 || astsiz >= siz))) {
if (comment)
PrintComment(f, comment);
n += PrintVarList(f, typ, idlist, flags);
}
}
return n;
}
static void
PrintSubHeaders(Flexbuf *f, Module *parse)
{
AST *ast, *sub;
Module *objstate;
int already_done;
/* include any needed object headers */
for (ast = parse->objblock; ast; ast = ast->right) {
if (ast->kind != AST_OBJECT) {
ERROR(ast, "Internal error, expected an OBJECT");
break;
}
/* see if we've already printed this header */
objstate = (Module *)ast->d.ptr;
already_done = 0;
for (sub = parse->objblock; sub && sub != ast; sub = sub->right) {
if (sub->d.ptr == objstate) {
already_done = 1;
break;
}
}
if (!already_done) {
flexbuf_printf(f, "#include \"%s.h\"\n", objstate->basename);
}
}
flexbuf_printf(f, "\n");
}
static void
PrintAllConstants(Flexbuf *f, Module *parse)
{
AST *ast, *upper;
/* print the constant declarations */
for (upper = parse->conblock; upper; upper = upper->right) {
ast = upper->left;
if (ast->kind == AST_COMMENTEDNODE) {
PrintComment(f, ast->right);
ast = ast->left;
}
switch (ast->kind) {
case AST_ENUMSKIP:
PrintConstantDecl(f, ast->left);
break;
case AST_IDENTIFIER:
PrintConstantDecl(f, ast);
break;
case AST_ASSIGN:
PrintConstantDecl(f, ast->left);
break;
default:
/* do nothing */
break;
}
}
}
static void
PrintCHeaderFile(Flexbuf *f, Module *parse)
{
int n;
int flags = PRIVATE;
CppModData *be = ModData(parse);
/* things we always need */
if (gl_header1 && gl_header2) {
flexbuf_printf(f, "// %s", gl_header1);
flexbuf_printf(f, "// %s", gl_header2);
}
flexbuf_printf(f, "#ifndef %s_Class_Defined__\n", parse->classname);
flexbuf_printf(f, "#define %s_Class_Defined__\n\n", parse->classname);
flexbuf_printf(f, "#include <stdint.h>\n");
PrintSubHeaders(f, parse);
PrintAllConstants(f, parse);
if (be->needsTuple) {
int n, i;
for (n = 0; n < MAX_TUPLE; n++) {
if (be->needsTuple & (1<<n)) {
flexbuf_printf(f, "#ifndef Tuple%d__\n", n);
flexbuf_printf(f, " struct tuple%d__ {", n);
for (i = 0; i < n; i++) {
flexbuf_printf(f, " int32_t v%d; ", i);
}
flexbuf_printf(f, "};\n");
flexbuf_printf(f, "# define Tuple%d__ struct tuple%d__\n", n, n);
flexbuf_printf(f, " static Tuple%d__ MakeTuple%d__(", n, n);
for (i = 0; i < n; i++) {
flexbuf_printf(f, "int32_t x%d%c", i, (i==n-1) ? ')' : ',');
}
flexbuf_printf(f, " {\n Tuple%d__ t;\n", n);
for (i = 0; i < n; i++) {
flexbuf_printf(f, " t.v%d = x%d;\n", i, i);
}
flexbuf_printf(f, " return t;\n }\n");
flexbuf_printf(f, "#endif\n\n");
}
}
}
if (parse->volatileVariables)
flags |= ISVOLATILE;
/* print the structure definition */
flexbuf_printf(f, "\ntypedef struct %s {\n", parse->classname);
if ( IsSpinLang(parse->mainLanguage)) {
n = PrintAllVarListsOfSize(f, parse, 4, flags);
n += PrintAllVarListsOfSize(f, parse, 2, flags);
n += PrintAllVarListsOfSize(f, parse, 1, flags);
} else {
n = PrintAllVarListsOfSize(f, parse, 0, flags);
}
/* needed to avoid problems with empty structures on Catalina */
if (n == 0)
flexbuf_printf(f, " char dummy__;\n");
flexbuf_printf(f, "} %s;\n\n", parse->classname);
/* now the public members */
PrintPublicFunctionDecls(f, parse);
/* now the private methods */
/* PrintPrivateFunctionDecls(f, parse); */
flexbuf_printf(f, "#endif\n");
}
static void
PrintCppHeaderFile(Flexbuf *f, Module *parse)
{
int flags = PRIVATE;
CppModData *be = ModData(parse);
/* things we always need */
if (gl_header1 && gl_header2) {
flexbuf_printf(f, "// %s", gl_header1);
flexbuf_printf(f, "// %s", gl_header2);
}
flexbuf_printf(f, "#ifndef %s_Class_Defined__\n", parse->classname);
flexbuf_printf(f, "#define %s_Class_Defined__\n\n", parse->classname);
flexbuf_printf(f, "#include <stdint.h>\n");
/* include any needed object headers */
PrintSubHeaders(f, parse);
if (be->needsTuple) {
int n, i;
for (n = 0; n < MAX_TUPLE; n++) {
if (be->needsTuple & (1<<n)) {
flexbuf_printf(f, "#ifndef Tuple%d__\n", n);
flexbuf_printf(f, " struct tuple%d__ {", n);
for (i = 0; i < n; i++) {
flexbuf_printf(f, " int32_t v%d; ", i);
}
flexbuf_printf(f, "};\n");
flexbuf_printf(f, "# define Tuple%d__ struct tuple%d__\n", n, n);
flexbuf_printf(f, " static Tuple%d__ MakeTuple%d__(", n, n);
for (i = 0; i < n; i++) {
flexbuf_printf(f, "int32_t x%d%c", i, (i==n-1) ? ')' : ',');
}
flexbuf_printf(f, " {\n Tuple%d__ t;\n", n);
for (i = 0; i < n; i++) {
flexbuf_printf(f, " t.v%d = x%d;\n", i, i);
}
flexbuf_printf(f, " return t;\n }\n");
flexbuf_printf(f, "#endif\n\n");
}
}
}
/* print the constant declarations */
flexbuf_printf(f, "class %s {\npublic:\n", parse->classname);
PrintAllConstants(f, parse);
/* data block, if applicable */
if (parse->datblock && !gl_gas_dat) {
flexbuf_printf(f, " static ");
PrintDatArrayName(f, parse, ";\n", false);
}
/* now the public members */
PrintPublicFunctionDecls(f, parse);
/* now the private members */
/* Note that Spin sorts these, outputing first the 32 bit
vars, then 16, finally 8
*/
if (parse->volatileVariables)
flags |= ISVOLATILE;
flexbuf_printf(f, "private:\n");
if (IsSpinLang(parse->mainLanguage)) {
PrintAllVarListsOfSize(f, parse, 4, flags);
PrintAllVarListsOfSize(f, parse, 2, flags);
PrintAllVarListsOfSize(f, parse, 1, flags);
} else {
PrintAllVarListsOfSize(f, parse, 0, flags);
}
/* now the private methods */
PrintPrivateFunctionDecls(f, parse);
flexbuf_printf(f, "};\n\n");
flexbuf_printf(f, "#endif\n");
}
static void
PrintMacros(Flexbuf *f, Module *parse)
{
CppModData *be = ModData(parse);
bool needsIfdef = false;
bool needsInline = false;
if (gl_nospin)
return;
if (gl_output == OUTPUT_C) {
needsIfdef = true;
needsInline = true;
}
if (be->needsBitEncode
|| be->needsMinMax
|| be->needsRotate
|| be->needsShr
|| be->needsLookup
|| be->needsLookdown)
{
needsInline = true;
}
if (needsIfdef) {
flexbuf_printf(f, "#if defined(__GNUC__)\n");
}
if (needsInline) flexbuf_printf(f, "#define INLINE__ static inline\n");
if (be->needsYield) {
// old way
//flexbuf_printf(f, "#include <sys/thread.h>\n");
//flexbuf_printf(f, "#define Yield__() (__napuntil(_CNT))\n");
// new way -- not as thread friendly, but much faster
flexbuf_printf(f, "#define Yield__() __asm__ volatile( \"\" ::: \"memory\" )\n");
}
if (be->needsHighmult) {
flexbuf_printf(f, "#define Highmult__(X, Y) ( ( (X) * (int64_t)(Y) ) >> 32 )\n");
}
if (be->needsBitEncode) {
flexbuf_printf(f, "#define BitEncode__(X) (32 - __builtin_clz(X))\n");
}
if (needsIfdef)
{
flexbuf_printf(f, "#else\n");
if (needsInline) flexbuf_printf(f, "#define INLINE__ static\n");
if (be->needsYield) {
flexbuf_printf(f, "#define Yield__()\n");
}
if (gl_output == OUTPUT_C && !gl_p2) {
flexbuf_printf(f, "#ifndef __FLEXC__\n");
flexbuf_printf(f, "#define waitcnt(n) _waitcnt(n)\n");
if (be->needsLockFuncs) {
flexbuf_printf(f, "#define locknew() _locknew()\n");
flexbuf_printf(f, "#define lockret(i) _lockret(i)\n");
flexbuf_printf(f, "#define lockset(i) _lockset(i)\n");
flexbuf_printf(f, "#define lockclr(i) _lockclr(i)\n");
}
flexbuf_printf(f, "#define coginit(id, code, par) _coginit((unsigned)(par)>>2, (unsigned)(code)>>2, id)\n");
flexbuf_printf(f, "#define cognew(code, par) coginit(0x8, (code), (par))\n");
flexbuf_printf(f, "#define cogstop(i) _cogstop(i)\n");
flexbuf_printf(f, "#endif /* __FLEXC__ */\n");
flexbuf_printf(f, "#ifdef __CATALINA__\n");
flexbuf_printf(f, "#define _CNT CNT\n");
flexbuf_printf(f, "#define _clkfreq _clockfreq()\n");
flexbuf_printf(f, "#endif\n");
if (be->needsHighmult) {
flexbuf_printf(f, "static int32_t Highmult__(int32_t a, int32_t b) {\n");
flexbuf_printf(f, " int sign = (a^b)>>31;\n");
flexbuf_printf(f, " uint32_t ua = a < 0 ? -a : a;\n");
flexbuf_printf(f, " uint32_t ub = b < 0 ? -b : b;\n");
flexbuf_printf(f, " uint32_t rhi = 0, rlo = 0;\n");
flexbuf_printf(f, " int i;\n");
flexbuf_printf(f, " for (i = 0; i < 32; i++) {\n");
flexbuf_printf(f, " rhi = (rhi << 1) | (rlo >> 31);\n");
flexbuf_printf(f, " rlo = rlo << 1;\n");
flexbuf_printf(f, " if (ua >> 31) {rlo += ub; if (rlo < ub) rhi++;}\n");
flexbuf_printf(f, " ua = ua<<1;\n");
flexbuf_printf(f, " }\n");
flexbuf_printf(f, " if (sign) { rhi = -rhi; rhi -= (rlo != 0); }\n");
flexbuf_printf(f, " return rhi;\n");
flexbuf_printf(f, "}\n");
}
if (be->needsBitEncode) {
flexbuf_printf(f, "INLINE__ int32_t BitEncode__(uint32_t a) {\n");
flexbuf_printf(f, " int r=0;\n");
flexbuf_printf(f, " while (a != 0) { a = a>>1; r++; }\n");
flexbuf_printf(f, " return r;\n");
flexbuf_printf(f, "}\n");
}
}
flexbuf_printf(f, "#endif\n");
flexbuf_printf(f, "\n");
}
if (be->needsMinMax) {
flexbuf_printf(f, "INLINE__ int32_t Min__(int32_t a, int32_t b) { return a < b ? a : b; }\n");
flexbuf_printf(f, "INLINE__ int32_t Max__(int32_t a, int32_t b) { return a > b ? a : b; }\n");
}
if (be->needsAbortdef) {
if (gl_output == OUTPUT_CPP)
flexbuf_printf(f, "extern \"C\" {\n");
flexbuf_printf(f, "#include <setjmp.h>\n");
if (gl_output == OUTPUT_CPP)
flexbuf_printf(f, "}\n");
flexbuf_printf(f, "typedef struct { jmp_buf jmp; int32_t val; } AbortHook__;\n");
flexbuf_printf(f, "AbortHook__ *abortChain__ __attribute__((common));\n\n");
}
if (be->needsRotate) {
flexbuf_printf(f, "INLINE__ int32_t Rotl__(uint32_t a, uint32_t b) { return (a<<b) | (a>>(32-b)); }\n");
flexbuf_printf(f, "INLINE__ int32_t Rotr__(uint32_t a, uint32_t b) { return (a>>b) | (a<<(32-b)); }\n");
}
if (be->needsShr) {
flexbuf_printf(f, "INLINE__ int32_t Shr__(uint32_t a, uint32_t b) { return (a>>b); }\n");
}
if (be->needsLookup) {
flexbuf_printf(f, "INLINE__ int32_t Lookup__(int32_t x, int32_t b, int32_t a[], int32_t n) { int32_t i = (x)-(b); return ((unsigned)i >= n) ? 0 : (a)[i]; }\n");
flexbuf_printf(f, "\n");
}
if (be->needsLookdown) {
flexbuf_printf(f, "INLINE__ int32_t Lookdown__(int32_t x, int32_t b, int32_t a[], int32_t n) { int32_t i, r; r = 0; for (i = 0; i < n; i++) { if (a[i] == x) { r = i+b; break; } }; return r; }\n");
flexbuf_printf(f, "\n");
}
if (be->needsRand) {
flexbuf_printf(f, "static uint32_t LFSR__(uint32_t x, uint32_t forward) {\n");
flexbuf_printf(f, " uint32_t y, c, a;\n");
flexbuf_printf(f, " if (x < 1) x = 1;\n");
flexbuf_printf(f, " a = forward ? 0x8000000B : 0x17;\n");
flexbuf_printf(f, " for (y = 0; y < 32; y++) {\n");
flexbuf_printf(f, " c = __builtin_parity(x & a);\n");
flexbuf_printf(f, " if (forward) x = (x<<1) | c;\n");
flexbuf_printf(f, " else x = (x>>1) | (c<<31);\n");
flexbuf_printf(f, " }\n");
flexbuf_printf(f, " return x;\n");
flexbuf_printf(f, "}\n");
flexbuf_printf(f, "#define RandForw__(x) ((x) = LFSR__((x), 1))\n");
flexbuf_printf(f, "#define RandBack__(x) ((x) = LFSR__((x), 0))\n");
}
if (be->needsSqrt) {
flexbuf_printf(f, "static uint32_t Sqrt__(uint32_t a) {\n");
flexbuf_printf(f, " uint32_t res = 0;\n");
flexbuf_printf(f, " uint32_t bit = 1<<30;\n");
flexbuf_printf(f, " while (bit > a) bit = bit>>2;\n");
flexbuf_printf(f, " while (bit != 0) {\n");
flexbuf_printf(f, " if (a >= res+bit) {\n");
flexbuf_printf(f, " a = a - (res+bit);\n");
flexbuf_printf(f, " res = (res>>1) + bit;\n");
flexbuf_printf(f, " } else res = res >> 1;\n");
flexbuf_printf(f, " bit = bit >> 2;\n");
flexbuf_printf(f, " }\n");
flexbuf_printf(f, " return res;\n");
flexbuf_printf(f, "}\n");
}
if (be->needsCogAccess) {
// we need to execute code that looks like:
// mov r0, <addr>
// jmp <retaddr>
// this is a bit tricky; we build it in r0,r1 and then jmp to it
// with a jmpret r1,#0
flexbuf_printf(f, "__asm__(\n");
flexbuf_printf(f, "\" .text\\n\"\n");
flexbuf_printf(f, "\" .balign 4\\n\"\n");
flexbuf_printf(f, "\"__cog_xfer\\n\"\n");
flexbuf_printf(f, "\" fcache #(.Lend - .Lstart)\\n\"\n");
flexbuf_printf(f, "\" .compress off\\n\"\n");
flexbuf_printf(f, "\".Lstart\\n\"\n");
flexbuf_printf(f, "\" mov pc, lr\\n\"\n");
flexbuf_printf(f, "\" mov lr, __LMM_RET\\n\"\n");
flexbuf_printf(f, "\" movd (.Linstr - .Lstart) + __LMM_FCACHE_START,r0\\n\"\n");
flexbuf_printf(f, "\" movs (.Linstr - .Lstart) + __LMM_FCACHE_START,r1\\n\"\n");
flexbuf_printf(f, "\" mov r0,r2\\n\"\n");
flexbuf_printf(f, "\".Linstr\\n\"\n");
flexbuf_printf(f, "\" mov 0-0,0-0\\n\"\n");
flexbuf_printf(f, "\" jmp lr\\n\"\n");
flexbuf_printf(f, "\".Lend\\n\"\n");
flexbuf_printf(f, "\" .compress default\\n\"\n");
flexbuf_printf(f, ");\n");
flexbuf_printf(f, "extern ");
if (gl_output == OUTPUT_CPP) {
flexbuf_printf(f, "\"C\" ");
}
flexbuf_printf(f, "int32_t _cog_xfer(int32_t dst, int32_t src, int32_t retval);\n");
flexbuf_printf(f, "#define cogmem_get__(addr) _cog_xfer(0, (addr), 0)\n");
flexbuf_printf(f, "#define cogmem_put__(addr,data) _cog_xfer((addr), 0, (data))\n");
flexbuf_printf(f, "\n");
}
if (be->needsCoginit) {
flexbuf_printf(f, "typedef void (*Cogfunc__)(void *a, void *b, void *c, void *d);\n");
flexbuf_printf(f, "static void Cogstub__(void *argp) {\n");
flexbuf_printf(f, " void **arg = (void **)argp;\n");
flexbuf_printf(f, " Cogfunc__ func = (Cogfunc__)(arg[0]);\n");
flexbuf_printf(f, " func(arg[1], arg[2], arg[3], arg[4]);\n");
flexbuf_printf(f, "}\n");
#if 1
// this is more efficient, but relies on the clone_cog
// function which is only available in newer libaries
flexbuf_printf(f, "__asm__(\".global _cogstart\\n\"); // force clone_cog to link if it is present\n");
if (gl_output == OUTPUT_CPP) {
flexbuf_printf(f, "extern \"C\" ");
} else {
flexbuf_printf(f, "extern ");
}
flexbuf_printf(f, "void _clone_cog(void *tmp) __attribute__((weak));\n");
if (gl_output == OUTPUT_CPP) {
flexbuf_printf(f, "extern \"C\" ");
} else {
flexbuf_printf(f, "extern ");
}
flexbuf_printf(f, "long _load_start_kernel[] __attribute__((weak));\n");
flexbuf_printf(f, "static int32_t Coginit__(int cogid, void *stackbase, size_t stacksize, void *func, int32_t arg1, int32_t arg2, int32_t arg3, int32_t arg4) {\n");
flexbuf_printf(f, " void *tmp = _load_start_kernel;\n");
flexbuf_printf(f, " unsigned int *sp = ((unsigned int *)stackbase) + stacksize/4;\n");
flexbuf_printf(f, " static int32_t cogargs__[5];\n");
flexbuf_printf(f, " int r;\n");
flexbuf_printf(f, " cogargs__[0] = (int32_t) func;\n");
flexbuf_printf(f, " cogargs__[1] = arg1;\n");
flexbuf_printf(f, " cogargs__[2] = arg2;\n");
flexbuf_printf(f, " cogargs__[3] = arg3;\n");
flexbuf_printf(f, " cogargs__[4] = arg4;\n");
flexbuf_printf(f, " if (_clone_cog) {\n");
flexbuf_printf(f, " tmp = __builtin_alloca(1984);\n");
flexbuf_printf(f, " _clone_cog(tmp);\n");
flexbuf_printf(f, " }\n");
flexbuf_printf(f, " *--sp = 0;\n");
flexbuf_printf(f, " *--sp = (unsigned int)cogargs__;\n");
flexbuf_printf(f, " *--sp = (unsigned int)Cogstub__;\n");
flexbuf_printf(f, " r = coginit(cogid, tmp, sp);\n");
flexbuf_printf(f, " return r;\n");
flexbuf_printf(f, "}\n");
#else
// NOTE: this one does not actually let you specify a cog :(
flexbuf_printf(f, "static int32_t Coginit__(int cogid, void *stackbase, size_t stacksize, void *func, int32_t arg1, int32_t arg2, int32_t arg3, int32_t arg4) {\n");
flexbuf_printf(f, " static int32_t cogargs__[5];\n");
flexbuf_printf(f, " int r;\n");
flexbuf_printf(f, " cogargs__[0] = (int32_t) func;\n");
flexbuf_printf(f, " cogargs__[1] = arg1;\n");
flexbuf_printf(f, " cogargs__[2] = arg2;\n");
flexbuf_printf(f, " cogargs__[3] = arg3;\n");
flexbuf_printf(f, " cogargs__[4] = arg4;\n");
flexbuf_printf(f, " r = cogstart(Cogstub__, cogargs__, stackbase, stacksize);\n");
flexbuf_printf(f, " return r;\n");
flexbuf_printf(f, "}\n");
#endif
}
}
#define BYTES_PER_LINE 16 /* must be at least 4 */
static int datacount;
static void
outputByteHex(Flexbuf *f, int c)
{
if (datacount == 0) {
flexbuf_printf(f, " ");
}
flexbuf_printf(f, "0x%02x, ", c);
datacount++;
if (datacount == BYTES_PER_LINE) {
flexbuf_printf(f, "\n");
datacount = 0;
}
}
static void IfdefPropeller(Flexbuf *f)
{
if (gl_cc) {
flexbuf_printf(f, "#ifdef __propeller__\n");
}
}
static void EndIfdefPropeller(Flexbuf *f)
{
if (gl_cc) {
flexbuf_printf(f, "#endif\n");
}
}
static void IfdefPropGCC(Flexbuf *f)
{
flexbuf_printf(f, "#if defined(__propeller__) && defined(__GNUC__)\n");
}
static void EndIfdefPropGCC(Flexbuf *f)
{
flexbuf_printf(f, "#endif\n");
}
static char *
reloc_func =
"static void reloc_add_(uint8_t *ptr, uint32_t offset)\n"
"{\n"
" uint32_t d;\n"
" d = ptr[0] + (ptr[1]<<8) + (ptr[2]<<16) + (ptr[3]<<24);\n"
" d += offset;\n"
" ptr[0] = d & 0xff;\n"
" ptr[1] = (d>>8) & 0xff;\n"
" ptr[2] = (d>>16) & 0xff;\n"
" ptr[3] = (d>>24) & 0xff;\n"
"}\n"
"\n"
"static void _dorelocs(uint8_t *dat, struct reloc__ *reloc)\n"
"{\n"
" int32_t offset = (int32_t)dat;\n"
" while (reloc->kind != 0) {\n"
" switch(reloc->kind) {\n"
" case RELOC_KIND_I32:\n"
" reloc_add_((uint8_t*)&dat[reloc->where], offset);\n"
" break;\n"
" }\n"
" reloc++;\n"
" }\n"
"}\n"
"static void _doreloc(void) __attribute__((constructor));\n"
"static void _doreloc(void)\n"
"{ static int done = 0;\n"
" if (!done) {\n"
" _dorelocs( (uint8_t*)&%s[0], &_reloc_dat[0]);\n"
" done = 1;\n"
" }\n"
"}\n\n"
;
static void
PrintCppRelocs(Flexbuf *f, Module *P, Flexbuf *relocs)
{
int numrelocs = flexbuf_curlen(relocs) / sizeof(Reloc);
int i;
Reloc *relocarray;
Reloc *nextreloc;
char *prefix;
if (numrelocs == 0) {
return;
}
relocarray = (Reloc *)flexbuf_peek(relocs);
// skip over any leading debug relocs
for (i = 0; i < numrelocs; i++) {
nextreloc = &relocarray[i];
if (nextreloc->kind != RELOC_KIND_DEBUG) {
break;
}
}
if (i == numrelocs) {
return;
}
WARNING(NULL, "PASM code must be relocated at run time; you may need to manually tweak the C/C++ output if __attribute__((constructor)) is not supported");
flexbuf_printf(f, "#define RELOC_KIND_I32 %d\n", RELOC_KIND_I32);
flexbuf_printf(f, "#define RELOC_KIND_AUGS %d\n", RELOC_KIND_AUGS);
flexbuf_printf(f, "#define RELOC_KIND_AUGD %d\n", RELOC_KIND_AUGD);
flexbuf_printf(f, "static struct reloc__ {\n");
flexbuf_printf(f, " int kind;\n");
flexbuf_printf(f, " int where;\n");
flexbuf_printf(f, " int value;\n");
flexbuf_printf(f, "} _reloc_dat[] = {\n");
for (; i < numrelocs; i++) {
int32_t value = 0;
char *kindstr = "RELOC_INVALID";
nextreloc = &relocarray[i];
switch (nextreloc->kind) {
case RELOC_KIND_NONE:
case RELOC_KIND_DEBUG:
continue;
case RELOC_KIND_I32:
value = nextreloc->symoff;
kindstr = "RELOC_KIND_I32";
break;
case RELOC_KIND_AUGS:
ERROR(NULL, "Cannot handle AUGS relocation yet");
break;
case RELOC_KIND_AUGD:
ERROR(NULL, "Cannot handle AUGD relocation yet");
break;
default:
ERROR(NULL, "Cannot handle this relocation yet");
break;
}
flexbuf_printf(f, " { %s, %d, %d },\n",
kindstr, nextreloc->addr, value);
}
flexbuf_printf(f, " { 0, 0, 0 }\n");
flexbuf_printf(f, "};\n");
prefix = (char *)calloc(1, strlen(P->datname)+strlen(P->classname)+32);
if (gl_output == OUTPUT_C) {
sprintf(prefix, "%s", P->datname);
} else {
sprintf(prefix, "%s::%s", P->classname, P->datname);
}
flexbuf_printf(f, reloc_func, prefix, P->datname);
}
static void
PrintCppFile(Flexbuf *f, Module *parse)
{
Flexbuf relocbuf;
flexbuf_init(&relocbuf, 1024);
/* things we always need */
if (gl_header1 && gl_header2) {
flexbuf_printf(f, "// %s", gl_header1);
flexbuf_printf(f, "// %s", gl_header2);
}
if (parse->topcomment) {
PrintComment(f, parse->topcomment);
}
if (ModData(parse)->needsStdlib) {
flexbuf_printf(f, "#include <stdlib.h>\n");
}
if (ModData(parse)->needsString) {
flexbuf_printf(f, "#include <string.h>\n");
}
IfdefPropeller(f);
flexbuf_printf(f, "#define __SPIN2CPP__\n");
if (gl_p2) {
flexbuf_printf(f, "#include <propeller2.h>\n");
} else {
flexbuf_printf(f, "#include <propeller.h>\n");
}
if (gl_gas_dat) {
flexbuf_printf(f, "#undef clkset\n");
flexbuf_printf(f, "#undef cogid\n");
flexbuf_printf(f, "#undef cogstop\n");
flexbuf_printf(f, "#undef locknew\n");
flexbuf_printf(f, "#undef lockret\n");
flexbuf_printf(f, "#undef lockclr\n");
flexbuf_printf(f, "#undef lockset\n");
flexbuf_printf(f, "#undef waitcnt\n");
flexbuf_printf(f, "#undef waitpeq\n");
flexbuf_printf(f, "#undef waitpne\n");
flexbuf_printf(f, "#define _waitcnt(x) __builtin_propeller_waitcnt((x), 0)\n");
}
EndIfdefPropeller(f);
flexbuf_printf(f, "#include \"%s.h\"\n", parse->basename);
flexbuf_printf(f, "\n");
PrintMacros(f, parse);
/* declare static functions and variables */
if (gl_output == OUTPUT_C && !gl_nospin) {
int n;
n = PrintPrivateFunctionDecls(f, parse);
if (n > 0)
flexbuf_printf(f, "\n");
}
/* print data block, if applicable */
if (parse->datblock) {
if (gl_gas_dat) {
flexbuf_printf(f, "extern ");
PrintDatArrayName(f, parse, " __asm__(\"..dat_start\");\n", false);
PrintDataBlockForGas(f, parse, 1);
} else {
Flexbuf datb;
unsigned char *datbytes;
unsigned int siz;
flexbuf_init(&datb, 2048);
if (gl_output == OUTPUT_C) {
flexbuf_printf(f, "static ");
PrintDatArrayName(f, parse, " = {\n", false);
} else {
PrintDatArrayName(f, parse, " = {\n", true);
}
datacount = 0;
PrintDataBlock(&datb, parse->datblock, NULL, &relocbuf);
siz = flexbuf_curlen(&datb);
datbytes = (unsigned char *)flexbuf_get(&datb);
while (siz > 0) {
outputByteHex(f, *datbytes++);
--siz;
}
if (datacount != 0) {
flexbuf_printf(f, "\n");
}
flexbuf_printf(f, "};\n");
}
}
/* relocations, if necessary */
PrintCppRelocs(f, parse, &relocbuf);
/* functions */
PrintFunctionBodies(f, parse);
/* any closing comments */
if (parse->botcomment) {
PrintComment(f, parse->botcomment);
}
}
// output an assembly language statement
void
OutputAsmEquate(Flexbuf *f, const char *str, unsigned int value)
{
flexbuf_printf(f, "__asm__( \" .global %s\\n\" );\n", str);
flexbuf_printf(f, "__asm__( \" %s = 0x%x\\n\" );\n", str, value);
}
void
OutputClkFreq(Flexbuf *f, Module *P)
{
unsigned int clkfreq;
unsigned int clkreg;
if (gl_p2) {
/* nothing yet */
} else {
if (GetClkFreq(P, &clkfreq, &clkreg)) {
IfdefPropGCC(f);
// now output the clkfreq and clkmode settings
OutputAsmEquate(f, "__clkfreqval", clkfreq);
OutputAsmEquate(f, "__clkmodeval", clkreg);
EndIfdefPropGCC(f);
}
}
}
static void
SetCppFlags(CppModData *bedata, AST *ast)
{
if (!ast) return;
switch(ast->kind) {
case AST_YIELD:
bedata->needsYield = 1;
break;
case AST_CATCH:
case AST_THROW:
case AST_TRYENV:
bedata->needsAbortdef = 1;
bedata->needsStdlib = 1;
break;
case AST_SPRREF:
bedata->needsCogAccess = 1;
break;
case AST_OPERATOR:
{
switch (ast->d.ival) {
case K_HIGHMULT:
bedata->needsHighmult = 1;
break;
case K_LIMITMIN:
case K_LIMITMAX:
bedata->needsMinMax = 1;
break;
case K_ROTL:
case K_ROTR:
bedata->needsRotate = 1;
break;
case K_SHR:
bedata->needsShr = 1;
break;
case K_ABS:
bedata->needsStdlib = 1;
break;
case K_SQRT:
bedata->needsSqrt = 1;
break;
case K_ENCODE:
bedata->needsBitEncode = 1;
break;
case '?':
bedata->needsRand = 1;
break;
default:
break;
}
}
break;
case AST_LOOKUP:
bedata->needsLookup = 1;
break;
case AST_LOOKDOWN:
bedata->needsLookdown = 1;
break;
case AST_RANGEREF:
bedata->needsStdlib = 1; // range calc may invoke abs
break;
case AST_IDENTIFIER:
{