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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
|
#!/usr/bin/env python3
#
# A C89-to-Python transpiler that is meant to behave in a maximally weird way
# that is still standards compliant. The main purpose is for ensuring that C
# programs are portable. For example, it has 100-bit char/int/long instead of
# the usual sizes.
#
# Copyright © 2026 Samuel Lidén Borell <samuel@kodafritt.se>
#
# SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later
#
import argparse
import os.path
import re
import sys
__all__ = ['Ds9kCCompiler', 'CSyntaxError']
spaces = re.compile(' +')
commaspace = re.compile(', *')
eolcomments = re.compile('/\\*([^*]|\\*[^/])*\\*/ *$')
startcomments = re.compile('^ */\\*([^*]|\\*[^/])*\\*/')
c_keywords = [
'auto',
'break',
'case',
'char',
'const',
'continue',
'default',
'do',
'double',
'else',
'enum',
'extern',
'float',
'for',
'goto',
'if',
'int',
'long',
'register',
'restrict',
'return',
'short',
'signed',
'sizeof',
'static',
'struct',
'switch',
'typedef',
'union',
'unsigned',
'void',
'volatile',
'while'
]
c_int_types = [
'%char',
'%int',
'%long',
'%short',
'%signed',
'%unsigned',
]
c_elementary_types = c_int_types + ['%void', '_P9KCC_FILE', '_P9KCC_va_list']
c_tags = [
'%enum',
'%struct',
'%union',
]
c_quals = [
'%auto',
'%const',
'%extern',
'%register',
'%static',
'%volatile',
]
c_type_prefixes = c_elementary_types + c_tags + c_quals
decl_end_tokens = [';', ',', '}']
# TODO right/left associativity
c_infix_postfix_operators = {
'=': (1, 'infix', 'R'),
'+=': (1, 'infix', 'R'),
'-=': (1, 'infix', 'R'),
'*=': (1, 'infix', 'R'),
'/=': (1, 'infix', 'R'),
'%=': (1, 'infix', 'R'),
'~=': (1, 'infix', 'R'),
'&=': (1, 'infix', 'R'),
'|=': (1, 'infix', 'R'),
'^=': (1, 'infix', 'R'),
'<<=': (1, 'infix', 'R'),
'>>=': (1, 'infix', 'R'),
',': (2, 'infix', '?'),
'||': (3, 'infix', 'L'),
'|': (3, 'infix', 'L'),
'&&': (4, 'infix', 'L'),
'&': (4, 'infix', 'L'),
'^': (4, 'infix', 'L'),
'==': (5, 'infix', 'L'),
'!=': (5, 'infix', 'L'),
'<': (5, 'infix', 'L'),
'>': (5, 'infix', 'L'),
'<=': (5, 'infix', 'L'),
'>=': (5, 'infix', 'L'),
'+': (6, 'infix', 'L'),
'-': (6, 'infix', 'L'),
'*': (7, 'infix', 'L'),
'/': (7, 'infix', 'L'),
'%': (7, 'infix', 'L'),
'.': (9, 'infix', '?'),
'->': (9, 'infix', '?'),
# check precedences of these
'++': (5, 'post', '?'),
'--': (5, 'post', '?'),
'[': (8, 'post', '?'),
}
c_prefix_operators = {
# check precedences of these
'!': (5, 'prefix', 'R'),
'*': (5, 'prefix', 'R'),
'&': (5, 'prefix', 'R'),
'#': (8, 'prefix', 'R'),
}
CALL_PRECEDENCE = 8
NUM_PREFIX = '$'
NUM_ZERO = NUM_PREFIX + '0'
NUM_ONE = NUM_PREFIX + '1'
class Ds9kCCompiler:
def __init__(self):
self.arg_parser = argparse.ArgumentParser(
usage='%(prog)s [-c] [-o output] sourcefile.c...',
description='"DeathStation 9000"-like C Compiler')
self.arg_parser.add_argument(
'sourcefiles',
action='extend', nargs='+',
help='Source files', metavar='SOURCEFILE')
self.arg_parser.add_argument(
'-c',
action='store_true',
dest='compile_to_obj',
help='Compile to (pseudo-)object file')
self.arg_parser.add_argument(
'-o',
dest='output',
help='Output file', metavar='OUTFILE')
self.arg_parser.add_argument(
'-I',
action='append',
dest='include_dirs', default=[],
help='Include directory', metavar='DIR')
self.arg_parser.add_argument(
'-D',
action='append',
dest='initial_defines', default=[],
help='Define a macro or function-macro', metavar='NAME=VALUE')
self.arg_parser.add_argument(
'-W',
dest='warnings',
help='Enable/disable warnings (ignored)', metavar='NAME')
self.arg_parser.add_argument(
'-f',
dest='flags',
help='Enable/disable flags (ignored)', metavar='FLAG')
self.arg_parser.add_argument(
'-g',
action='store_true',
dest='debuginfo',
help='Enable debuginfo (ignored)')
self.arg_parser.add_argument(
'-std',
dest='c_standard',
help='C standard version (ignored, always c89)', metavar='CSTD')
self.arg_parser.add_argument(
'-ansi',
action='store_true',
dest='ansi_mode',
help='Enable ANSI mode (default, ignored)')
self.arg_parser.add_argument(
'-pedantic',
action='store_true',
dest='pedantic_mode',
help='Enable pedantic warnings (default, ignored)')
self.arg_parser.add_argument(
'-x',
dest='language_name',
help='Language name (ignored, always \'c\')', metavar='LANG')
self.arg_parser.add_argument(
'-E',
action='store_true',
dest='preprocess_only',
help='Only run pre-processor')
self.src_filename = None
self.line = None
self.defines = {}
self.in_func_body = False
self.scope = Scope(self, None)
def init_from_argv(self, argv=None):
if argv is None:
argv = sys.argv[1:]
self.options = self.arg_parser.parse_args(argv)
if not self.options.output:
if self.options.preprocess_only:
self.options.output = '-'
elif self.options.compile_to_obj:
self.options.output = self.options.sourcefiles[-1].removesuffix(".c") + ".o"
else:
self.options.output = "a.out"
def error(self, message):
raise CSyntaxError(self.src_filename, self.line, message)
def joinlines(self, lines):
buffer = ''
self.line = 0
for line in lines:
self.line += 1
if line.endswith('\\\n'):
buffer += line.removesuffix('\\\n') + ' '
else:
yield buffer + line
buffer = ''
if len(buffer) > 0:
yield buffer
def preprocess(self, filename):
with open(filename, 'r') as file:
ignorelevel = 0
skip_if = False
skip_else = False
in_comment = False
self.src_filename = filename
for line in self.joinlines(file):
if len(line) > 509:
self.error("Line too long")
line = line.strip()
if in_comment:
if '*/' in line:
_, line = line.split('*/', 1)
in_comment = False
else:
continue
# FIXME comments inside lines are not yet supported
line = eolcomments.sub('', line)
if not line.startswith('#'):
line = startcomments.sub('', line)
if line.startswith('#'):
splitted = spaces.split(line[1:].strip(), 1)
ppop = splitted[0]
ppvalue = splitted[1] if len(splitted) >= 2 else None
if ppop == 'ifdef':
if ignorelevel >= 1 or skip_if:
ignorelevel += 1
elif ppvalue in self.defines:
skip_else = True
else:
skip_if = True
elif ppop == 'ifndef':
if ignorelevel >= 1 or skip_if:
ignorelevel += 1
elif not ppvalue in self.defines:
skip_else = True
else:
skip_if = True
elif ppop == 'if':
if ignorelevel >= 1 or skip_if:
ignorelevel += 1
elif self.eval_ppexpr(ppvalue):
skip_else = True
else:
skip_if = True
elif ppop == 'endif':
if ignorelevel >= 1:
ignorelevel -= 1
else:
skip_else = False
skip_if = False
elif ppop == 'else' or ppop == 'elif':
if ignorelevel >= 1:
pass
elif skip_else:
ignorelevel = 1
else:
if ppop == 'else' or self.eval_ppexpr(ppvalue):
skip_else = True
skip_if = False
else:
skip_if = True
elif ignorelevel >= 1:
pass
elif ppop == 'define':
# TODO check for incorrect redefinitions
arr = spaces.split(ppvalue, 1)
name = arr[0]
if '(' in name:
arr = ppvalue.split(')', 1)
name = arr[0] + ')'
value = arr[1] if len(arr) >= 2 else None
else:
value = arr[1] if len(arr) >= 2 else None
self.define(name, value)
elif ppop == 'undef':
try:
del self.defines[ppvalue]
except KeyError:
pass
elif ppop == 'error':
self.error(ppvalue)
elif ppop == 'include':
dirs = None
if ppvalue.startswith('<') and ppvalue.endswith('>'):
dirs = [os.path.dirname(__file__) + '/include']
elif ppvalue.startswith('"') and ppvalue.endswith('"'):
dirs = [os.path.dirname(filename)] + \
self.options.include_dirs
else:
self.error('Invalid include type %s' % (ppvalue,))
incfilename = ppvalue[1:-1]
saved_line = self.line
for dir in dirs:
path = dir + '/' + incfilename
if os.path.exists(path):
yield from self.preprocess(path)
break
else:
self.error('#include not found: \'%s\''
% (incfilename,))
self.src_filename = filename
self.line = saved_line
else:
self.error("Unknown preprocessor '#%s'" % (ppop,))
elif line.startswith('/*'):
assert not '*/' in line
in_comment = True
elif ignorelevel >= 1 or skip_if:
pass
elif len(line) > 0:
yield from self.yield_tokens(line)
def parse_exprstr_to_rpn(self, s):
it = self.yield_tokens(s)
tok = next(it)
expr, tok = self.parse_expr_to_rpn(it, tok)
if tok is not None:
self.error('Expression could not be parsed beyond \'%s\'' %
(tok,))
if next(it, None) is not None:
self.error('Stopped parsing expression before \'%s\'' % (tok,))
return expr
def parse_expr_to_rpn(self, it, first_tok):
opstack = []
out = []
operator_expected = False
#print("---------------- parse expr to rpn")
while True:
tok = None
if first_tok is not None:
tok = first_tok
first_tok = None
else:
tok = next(it, None)
if tok is None:
break
#print(tok, operator_expected)
#print(' ',opstack)
#print(' ',out)
if tok == ')' or tok == ',':
# End of grouping parenthesis OR end of parameter
noargs = False
if tok == ',':
if not operator_expected:
self.error('Unexpected comma')
elif not operator_expected:
noargs = True
popped_ops = False
st_tok = None
while len(opstack) >= 1:
st_tok, st_op = opstack[-1]
if st_tok == '%call' or st_tok == '(':
break
opstack.pop()
out.append(st_tok)
popped_ops = True
else:
# Reached at end of in enum value
# (they end with a comma).
# Also reached when there are unbalanced parentheses.
break
if tok == ',':
# TODO does anything need to be done here?
out.append('%nextarg')
operator_expected = False
else:
opstack.pop()
if noargs:
out.append('%noargs')
if st_tok == '%call':
out.append('%call')
elif operator_expected:
try:
op = c_infix_postfix_operators[tok]
op_level, op_xfix, op_assoc = op
# Pop operators with higher precedence level
#print(' pop higher: ', tok, op_level)
while len(opstack) >= 1:
st_tok, st_op = opstack[-1]
st_level, st_xfix, st_assoc = st_op
if st_level < op_level:
break
elif st_level == op_level and st_assoc == 'R':
break
#print(' pop: ', st_tok, st_level)
opstack.pop()
if st_tok != '(':
out.append(st_tok)
opstack.append((tok, op))
operator_expected = False
except KeyError:
if tok == '(' or tok == '[':
# Start of function call or array index.
# Pop operators with higher precedence,
# like the member operator.
while len(opstack) >= 1:
st_tok, st_op = opstack[-1]
st_level, st_xfix, st_assoc = st_op
if st_level < CALL_PRECEDENCE:
break
elif st_level == CALL_PRECEDENCE and \
st_assoc == 'R':
break
opstack.pop()
out.append(st_tok)
if tok == '(':
opstack.append(('%call', CALL_PRECEDENCE))
else:
opstack.append(('%arrind', CALL_PRECEDENCE))
operator_expected = False
elif tok in [';', ']', '}']:
# End of: bitfield size, array length, last enum value
break
else:
self.error(
'Unexpected symbol \'%s\'. Expected operator' %
(tok,))
else: # not operator_expected
try:
op = c_prefix_operators[tok]
op_xfix = op[1]
opstack.append((tok, op))
operator_expected = (op_xfix != 'prefix')
except KeyError:
c = tok[0]
if (c == '"' or c == "'" or c == NUM_PREFIX or
(c >= 'A' and c <= 'Z') or # identifiers
(c >= 'a' and c <= 'z') or c == '_' or
c == '%'): # types with keywords
if self.is_type_token(tok) or c == '%':
# Skip type cast
if len(opstack) == 0 or opstack[-1][0] != '(':
self.error('Unexpected type in expression'
if c != '%' else
'Unexpected keyword in expression')
decl, tok = self.parse_decl(tok, it)
if decl.ident is not None:
self.error('Cannot have declared identifier '
'in type cast')
if tok != ')':
self.error('Expected \')\' at end of '
'type cast')
opstack.pop()
else:
# Terminal token (string, number or identifier)
out.append(tok)
operator_expected = True
elif c == '(':
# Start of grouping parenthesis
# or type cast
while len(opstack) >= 1:
st_tok, st_op = opstack[-1]
st_level, st_xfix, st_assoc = st_op
if st_level <= CALL_PRECEDENCE:
break
opstack.pop()
out.append(st_tok)
opstack.append(('(', (0, '?', 'R')))
else:
self.error('Unexpected symbol \'%s\'' % (tok,))
if not operator_expected:
self.error('Unexpected end of expression')
while len(opstack) >= 1:
st_tok, st_op = opstack.pop()
out.append(st_tok)
return out, tok # = RPN
def tok2bool(self, tok):
if tok.startswith(NUM_PREFIX):
# TODO check range
return tok != NUM_ZERO
else:
self.error('Invalid value for boolean operation')
def tok2int(self, tok):
if tok.startswith(NUM_PREFIX):
# TODO check range
return int(tok.removeprefix(NUM_PREFIX))
elif tok.startswith("'"):
return ord(tok[1])
else:
self.error('Not an integer value')
def scalar_op(self, op, a, b):
if not a.startswith(NUM_PREFIX) or not b.startswith(NUM_PREFIX):
# TODO implement short-circuiting properly, so type
# checking can be enabled.
if op in ['==','!=','<','<=','>','>=']:
return False
else:
return 0
#self.error(
# 'Operator %s requires scalar types. Was: \'%s\', \'%s\'' %
# (op, a, b))
a = int(a[1:])
b = int(b[1:])
if op == '==': return a == b
elif op == '!=': return a != b
elif op == '<' : return a < b
elif op == '<=': return a <= b
elif op == '>' : return a > b
elif op == '>=': return a >= b
elif op == '+': return a + b
elif op == '-': return a - b
elif op == '*': return a * b
elif op == '/': return a / b
elif op == '%': return a % b
elif op == '&': return a & b
elif op == '|': return a | b
elif op == '^': return a ^ b
else:
self.error('Unknown scalar operator')
def eval_parsed_expr(self, expr):
terms = []
for op in expr:
#print(op)
#print(' ', terms)
if op == '||':
b = terms.pop()
a = terms.pop()
res = self.tok2bool(a) or self.tok2bool(b)
terms.append(bool2tok(res))
elif op == '&&':
b = terms.pop()
a = terms.pop()
res = self.tok2bool(a) and self.tok2bool(b)
terms.append(bool2tok(res))
elif op == '!':
a = terms.pop()
res = not self.tok2bool(a)
terms.append(bool2tok(res))
elif op in ['==', '!=', '>', '>=', '<', '<=',
'+', '-', '*', '/', '%', '&', '|', '^']:
b = terms.pop()
a = terms.pop()
res = self.scalar_op(op, a, b)
terms.append(bool2tok(res))
elif op == '%nextarg':
terms.append(op)
elif op == '%call':
#print('CALL',terms)
param = terms.pop()
args = []
funcname = None
if param == '%noargs':
funcname = terms.pop()
else:
while True:
args.append(param)
param = terms.pop()
if param != '%nextarg':
funcname = param
break
#print('CALLEND',terms)
#print('CALLFUNC',funcname)
#print('CALLARGS',args)
# FIXME 1. this is actually an operator.
# FIXME 2. what is the order of evaluation of macros vs operators in pp-exprs?
# FIXME 3. this should be unavailable in enum initialisers
if funcname == 'defined':
if len(args) != 1:
self.error('Wrong number of arguments to defined()')
else:
name = args[0]
res = bool2tok(name in self.defines)
terms.append(res)
else:
self.error(
'Unknown macro or pre-processor function \'%s\'' %
(funcname,))
else:
terms.append(op)
#print('RESULT:',terms, " of ", s)
if len(terms) != 1:
self.error('Expected exactly one subexpression after evaluation')
return terms[0]
def eval_ppexpr(self, s):
expr = self.parse_exprstr_to_rpn(s)
value = self.eval_parsed_expr(expr)
return self.tok2bool(value)
def define(self, name, value=None):
if '(' in name:
name, paramsstr = name.split('(', 1)
if not paramsstr.endswith(')'):
self.error('Function-like macro is missing \')\'')
params = commaspace.split(paramsstr.removesuffix(')'))
self.defines[name] = Define(self, params, value)
else:
self.defines[name] = Define(self, None, value)
def predefines(self):
self.define('__P9KCC__', None)
for arg in self.options.initial_defines:
arr = arg.split('=', 1)
name = arr[0]
value = None
if len(arr) == 2:
value = arr[1]
self.define(name, value)
def all_tokens(self):
for fn in self.options.sourcefiles:
self.src_filename = fn
yield from self.preprocess(fn) # yields tokens
def parse_typedef(self, it):
tok = next(it)
decl, tok = self.parse_decl(tok, it)
self.scope.register_typedef(decl.ident, decl)
return tok
def parse_type_prefix(self, tok, it):
type = Type(self)
prefix = []
while tok == '*' or self.is_type_token(tok):
if tok == '*':
type.require_finalised()
prefix.append(type)
type = Type(self)
type.set_kind('%pointer')
elif tok in c_quals:
type.add_qualifier(tok)
elif tok in c_elementary_types:
type.set_kind(tok)
elif tok in c_tags:
tagtype = tok
type.set_kind(tagtype)
tok = next(it)
# Map tag identifier
tagname = None
if self.is_ident_token(tok):
tagname = tok
tok = next(it)
tag = self.scope.map_tag_ident(tagtype, tagname)
type.set_tag(tag)
if tok != '{':
continue
# Parse enum/struct/union
tag.parse_definition(it)
elif self.scope.typedef_exists(tok):
type.set_kind('%typedef')
type.set_typedef(tok)
else:
self.error('Invalid token \'%s\' in type prefix' % (tok,))
tok = next(it)
prefix.append(type)
return prefix, tok
def parse_type_suffix(self, tok, it, outer_type):
suffix = []
while tok in ['[', '(']:
if tok == '[':
# Array
tok = next(it)
lengthexpr = None
if tok == ']':
lengthexpr = None
else:
lengthexpr, tok = self.parse_expr_to_rpn(it, tok)
if tok != ']':
self.error('Expected ] not \'%s\' after array length'
% (tok,))
type = Type(self)
type.set_kind('%array')
type.set_arraylength(lengthexpr)
suffix.append(type)
elif tok == '(':
# Function
funcparams = []
self.enter_scope()
tok = next(it)
if tok == ')':
self.error('Must specify \'void\' for zero-parameter '
'function')
is_varargs = False
while True:
if tok == '...':
if len(funcparams) == 0:
self.error('varargs function must have at least '
'1 parameter')
is_varargs = True
tok = next(it, '')
if tok != ')':
self.error('Expected \')\' after \'...\'')
break
paramdecl, tok = self.parse_decl(tok, it)
if paramdecl.is_void():
if len(funcparams) != 0:
self.error('void after other parameters')
break
funcparams.append(paramdecl)
if tok == ')':
break
elif tok != ',':
self.error('Expected \',\' or \')\' after parameter')
tok = next(it)
self.leave_scope()
type = Type(self)
type.set_kind('%funcparams')
type.set_funcparams(funcparams)
type.set_varargs(is_varargs)
suffix.append(type)
tok = next(it)
# FIXME 1. this is only correct in basic cases
# FIXME 2. also, neither this function or parse_type_prefix handl
# parentheses in declarations, like `char (* stuff)()`.
outer_type += suffix[::-1]
return tok
def parse_decl(self, tok, it):
type, tok = self.parse_type_prefix(tok, it)
ident = None
if tok in decl_end_tokens:
return Decl(self, type, None), tok
elif self.is_ident_token(tok):
ident = tok
tok = next(it)
if tok not in decl_end_tokens:
tok = self.parse_type_suffix(tok, it, type)
decl = Decl(self, type, ident)
if ident is not None:
# TODO add definition, and check if compatible if already exists
pass
if tok == '{':
# Function body
pass
return decl, tok
def parse_repeated_decl(self, tok, it, template_decl):
decl, tok = self.parse_decl('%int', it)
decl.type[0] = template_decl.type[0]
return decl, tok
def is_ident_token(self, tok):
c = tok[0]
return (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') or c == '_'
def is_type_token(self, tok):
if tok in c_type_prefixes:
return True
elif self.scope.typedef_exists(tok):
return True
else:
return False
def parse_decl_or_expr(self, tok, it):
if tok is None:
tok = next(it, None)
if tok == None:
return False
elif tok == '%typedef':
tok = self.parse_typedef(it)
if tok != ';':
self.error('Parsed end of typedef but found no \';\'')
return True
# FIXME this does not detect all declarations
if self.is_type_token(tok):
# Declaration
decl, tok = self.parse_decl(tok, it)
while tok == ',':
decl, tok = self.parse_repeated_decl(
tok, it, decl)
if tok == '{':
self.parse_funcbody(it, decl)
elif tok != ';':
self.error('Parsed end of declaration but found no \';\'')
return True
else:
# Expression
if not self.in_func_body:
self.error('Parsed this as a statement, but this isn\'t '
'inside a function')
self.parse_statement(tok, it)
pass
def parse_statement(self, tok, it):
if tok == ';':
pass
else:
expr, tok = self.parse_expr_to_rpn(it, tok)
# TODO add expr to current bblock
if tok != ';':
self.error('Expected \';\' at end of statement')
def parse_funcbody(self, it, funcdecl):
functype = funcdecl.type[-1]
if functype.kind != '%funcparams':
self.error('Interpreted \'{\' as function body, but the declaration '
'is not a function declaration')
if self.scope.outer is not None:
self.error('Function must be at top-level')
self.enter_scope()
for funcparam in functype.funcparams:
self.scope.register_decl(funcparam.ident, funcparam)
assert not self.in_func_body
self.in_func_body = True
while True:
tok = next(it)
if tok == '}':
break
elif tok is None:
self.error('Reached EOF without closing \'}\' of \'%s\'' %
(funcdecl.ident,))
self.parse_decl_or_expr(tok, it)
self.in_func_body = False
self.leave_scope()
def enter_scope(self):
scope = Scope(self, self.scope)
self.scope = scope
def leave_scope(self):
assert self.scope.outer is not None
self.scope = self.scope.outer
def run(self):
self.predefines()
if self.options.preprocess_only:
preprocessed_code = format(self.all_tokens())
if self.options.output == '-':
outfile = sys.stdout
else:
outfile = open(self.options.output, 'w')
print(preprocessed_code, file=outfile)
return
it = self.all_tokens()
while self.parse_decl_or_expr(None, it):
pass
# TODO generate output code
def yield_tokens(self, s):
it = iter(s)
nextch = None
while True:
ch = next(it, '') if nextch is None else nextch
if ch == '':
break
nextch = next(it, '')
combo = ch+nextch
if combo in ['<<', '>>']:
nextch = next(it, '')
if nextch == '=':
yield combo+nextch # <<= or >>=
nextch = None # consume '='
else:
yield combo # << or >>
continue
elif combo == '..':
nextch = next(it, '')
if nextch == '.':
nextch = None
yield '...'
continue
else:
self.error('Bad token \'..\'')
combo = ch+nextch
if combo in ['==', '!=', '<=', '>=', '&&', '||',
'++', '--', '+=', '-=', '*=', '/=', '~=',
'|=', '&=', '^=', '%=', '->']:
nextch = None # consume it
yield combo
elif ch in '+-*/%&|^~!=<>?:;,.()[]{}#':
yield ch
elif ch == '"' or ch == "'":
# String
start = ch
string = ''
ch = nextch
nextch = None
while True:
if ch == '':
self.error('Unexpected end of string')
elif ch == start:
break
elif ch != '\\':
string += ch
else:
ch = next(it, '')
if ch == '\\' or ch == '"' or ch == "'":
string += ch
elif ch == 'n':
string += '\n'
elif ch == 'r':
string += '\r'
elif ch == 't':
string += '\t'
elif ch == '0':
# TODO octal escapes
string += '\0'
elif ch == 'x' or ch == 'X':
d1 = next(it, '')
d2 = next(it, '')
# FIXME the string is a UTF-16 string here.
string += chr(int(d1+d2, 16))
else:
self.error('Invalid escape')
ch = next(it, '')
yield start + string
elif ch == ' ' or ch == '\t':
pass
elif (ch >= 'A' and ch <= 'Z') or \
(ch >= 'a' and ch <= 'z') or \
ch == '_':
# Identifier, keyword or define/macro
ident = ch
while (nextch >= 'A' and nextch <= 'Z') or \
(nextch >= 'a' and nextch <= 'z') or \
(nextch >= '0' and nextch <= '9') or nextch == '_':
ident += nextch
nextch = next(it, '')
is_macro = False
if ident in self.defines:
define = self.defines[ident]
if define.params is not None and nextch == '(':
# Macro with parameters
args = []
arg = ''
nextch = None
plevel = 0
while True:
ch = next(it, None)
if ch == None:
raise NotImplementedError('Line breaks inside '
'argument lists of macro invocations '
'are not yet implemented')
elif ch == '(':
plevel += 1
elif ch == ')':
if plevel == 0:
args.append(arg.strip())
break
plevel -= 1
elif ch == '"' or ch == "'":
# Strings could contain commas or parentheses,
# so skip them entirely.
quote = ch
ch = next(it)
while ch != quote:
ch = next(it)
if ch == '\\':
ch = next(it)
elif ch == ',':
args.append(arg.strip())
arg = ''
else:
arg += ch
yield from define.expand(args)
is_macro = True
elif define.params is None:
# Define without parameters
yield from define.expand(None)
is_macro = True
if is_macro:
pass
elif ident == '__FILE__':
yield '"' + self.src_filename
elif ident == '__LINE__':
yield NUM_PREFIX + str(self.line)
elif ident in c_keywords:
yield '%' + ident
else:
yield ident
elif ch == '0' and nextch == 'x':
# Hex number
nextch = next(it, '')
num = ''
while (nextch >= '0' and nextch <= '9') or \
(nextch >= 'A' and nextch <= 'F') or \
(nextch >= 'a' and nextch <= 'f'):
num += nextch
nextch = next(it, '')
while nextch == 'U' or nextch == 'L':
nextch = next(it, '')
yield NUM_PREFIX + str(int(num, 16))
elif ch >= '0' and ch <= '9':
# Number
# (floating point is not implemented)
num = ch
while (nextch >= '0' and nextch <= '9'):
num += nextch
nextch = next(it, '')
while nextch == 'U' or nextch == 'L':
nextch = next(it, '')
yield NUM_PREFIX + num
else:
self.error('Bad token')
def bool2tok(b):
return NUM_ONE if b else NUM_ZERO
def escape_string(s):
s = s.replace('\\', '\\\\')
s = s.replace('\0','\\0')
s = s.replace('\"', '\\"')
s = s.replace("\'", "\\'")
return s
def format_token(tok):
if tok.startswith('%'): # keyword
return tok.removeprefix('%')
elif tok.startswith(NUM_PREFIX):
return tok.removeprefix(NUM_PREFIX)
elif tok.startswith('"') or tok.startswith("'"):
quote = tok[0]
return quote + escape_string(tok[1:]) + quote
else:
return tok
def format(tokens):
s = ''
indentlevel = 0
linebreak = '\n'
space = ''
last_tok = None
for tok in tokens:
# FIXME do/while gets formatted incorrectly
# (with deceptive line break before while)
if tok != '%else':
s += (space if not tok in [',', ';'] else '') + format_token(tok)
else:
s = s.strip() + ' else'
if tok == '{':
indentlevel += 1
linebreak = '\n' + (' ' * (indentlevel * 4))
s += linebreak
space = ''
elif tok == '}':
# FIXME this adds a line break in the last line of typedef struct
indentlevel -= 1
linebreak = '\n' + (' ' * (indentlevel * 4))
if last_tok in [';', '}']:
s = s[:-1].strip() + linebreak + '}'
s += linebreak
space = ''
elif tok == ';':
s += linebreak
space = ''
elif tok in [',']:
space = ' '
else:
space = ' '
last_tok = tok
return s
class Define:
def __init__(self, compiler, params=None, value=None):
self.compiler = compiler
self.params = params
self.value = value
def expand(self, args=None):
if self.params is not None and args is None:
self.compiler.error("Expected arguments to macro")
elif self.params is not None and len(self.params) != len(args):
self.compiler.error(
"Wrong number of arguments to macro, was %d but expected %d" %
(len(args), len(self.params)))
elif self.value is None:
pass # Nothing to expand
elif self.params is None:
assert args is None
yield from self.compiler.yield_tokens(self.value)
else:
make_string = False
for tok in self.compiler.yield_tokens(self.value):
if tok == '#':
make_string = True
continue
try:
i = self.params.index(tok)
if make_string:
yield '"' + args[i]
make_string = False
else:
yield from self.compiler.yield_tokens(args[i])
except ValueError:
yield tok
class Decl:
def __init__(self, compiler, type, ident):
self.compiler = compiler
self.type = type
self.ident = ident
def __repr__(self):
return 'Decl(%s, %s)' % (repr(self.type), self.ident)
def is_void(self):
#print(self.type)
return len(self.type) == 1 and self.type[0].kind == '%void'
class EnumValueDecl(Decl):
def __init__(self, compiler, ident, num):
super().__init__(compiler, INT_DECLTYPE, ident)
self.num = num
class Type:
def __init__(self, compiler):
self.compiler = compiler
self.kind = None
self.arraylength = None
self.funcparams = None
self.is_varargs = False
self.typedef = None
self.tag = None
self.quals = set()
def __repr__(self):
if self.kind == '%funcparams':
return 'Type(%s, %s)' % (self.kind, self.funcparams)
elif self.kind == '%array':
return 'Type(%s, %s)' % (self.kind, self.arraylength)
else:
return 'Type(%s)' % (self.kind,)
def set_kind(self, kind):
assert kind is not None
if self.kind is None:
self.kind = kind
elif self.kind in ['%unsigned','%signed']:
if kind != '%int':
self.kind += ' ' + kind
else:
self.compiler.error('Repeated type')
def add_qualifier(self, tok):
if tok in self.quals:
self.compiler.error('Duplicate qualifier')
self.quals.add(tok)
def set_arraylength(self, lengthexpr):
if lengthexpr != None:
if len(lengthexpr) != 1:
self.compiler.error('Array length is too complex')
lenvalue = lengthexpr[0]
if lenvalue.startswith(NUM_PREFIX):
lenvalue = int(lenvalue.removeprefix(NUM_PREFIX))
else:
# FIXME hack to be able to support enum values as array lengths
try:
decl = self.compiler.scope.search_decl(lenvalue)
lenvalue = decl.num
except KeyError:
self.compiler.error('Array length must be constant integer')
self.arraylength = lenvalue
def set_funcparams(self, funcparams):
assert funcparams is not None
self.funcparams = funcparams
def set_varargs(self, is_varargs):
self.is_varargs = is_varargs
def set_typedef(self, typedef):
assert typedef is not None
self.typedef = typedef
def set_tag(self, tag):
assert tag is not None
self.tag = tag
def require_finalised(self):
if self.kind is None:
self.compiler.error('Must specify root type before pointer')
inttype = Type(None)
inttype.set_kind('%int')
INT_DECLTYPE = [inttype]
del inttype
class Tag:
def __init__(self, compiler, tagtype, ident):
self.compiler = compiler
self.tagtype = tagtype
self.ident = ident
self.scope = compiler.scope
def parse_definition(self, it):
if self.tagtype == '%struct' or self.tagtype == '%union':
self.parse_fields(it)
else:
self.parse_enumvalues(it)
def parse_fields(self, it):
self.fields_list = []
self.fields_map = {}
tok = next(it)
while tok != '}':
fielddecl, tok = self.compiler.parse_decl(tok, it)
while True:
name = fielddecl.ident
if name in self.fields_map:
self.compiler.error('Duplicate field \'%s\'' % (name,))
self.fields_map[name] = fielddecl
self.fields_list.append(fielddecl)
if tok == ':':
# Bitfields. For now, these are just ignored
tok = next(it)
bitsize, tok = self.compiler.parse_expr_to_rpn(it, tok)
if tok == ',':
fielddecl, tok = self.compiler.parse_repeated_decl(
tok, it, fielddecl)
else:
break
if tok != ';':
self.compiler.error('Expected \';\' after field')
tok = next(it)
if len(self.fields_list) == 0:
self.error('C89 does not allow empty %ss' % (self.tagtype,))
def parse_enumvalues(self, it):
self.enumvalues_list = []
tok = next(it)
num = 0
while True:
name = tok
tok = next(it)
if tok == '=':
tok = next(it)
numexpr, tok = self.compiler.parse_expr_to_rpn(it, tok)
numtok = self.compiler.eval_parsed_expr(numexpr)
num = self.compiler.tok2int(numtok)
self.add_enumvalue(name, num)
if tok == ',':
tok = next(it)
if tok == '}':
self.error('C89 does not allow trailing comma')
num += 1
elif tok == '}':
break
else:
self.error('Unexpected token in enum')
def add_enumvalue(self, name, num):
self.enumvalues_list.append(name)
val = EnumValueDecl(self.compiler, name, num)
self.scope.register_decl(name, val)
class Scope:
def __init__(self, compiler, outer):
self.compiler = compiler
self.outer = outer
self.typedefs = {}
self.decls = {}
self.tags = {}
def typedef_exists(self, name):
try:
self.search_typedef(name)
return True
except KeyError:
return False
def search_decl(self, name):
if name in self.decls:
return self.decls[name]
elif self.outer is not None:
self.outer.search_decl(name)
else:
raise KeyError('Decl \'%s\' not found' % (name,))
def search_typedef(self, name):
if name in self.typedefs:
return self.typedefs[name]
elif self.outer is not None:
self.outer.search_typedef(name)
else:
raise KeyError('Typedef \'%s\' not found' % (name,))
def register_typedef(self, name, decl):
if name in self.typedefs:
self.compiler.error('Type \'%s\' already exists' % (name,))
self.typedefs[name] = decl
def register_decl(self, name, decl):
if name in self.decls:
self.compiler.error('Identifier \'%s\' is already declared' %
(name,))
self.decls[name] = decl
def search_tag_ident(self, name):
if name in self.tags:
return self.tags[name]
elif self.outer is not None:
return self.outer.search_tag_ident(name)
else:
raise KeyError('Tag not found')
def map_tag_ident(self, tagtype, name):
if name is None:
return Tag(self.compiler, tagtype, None)
try:
tag = self.search_tag_ident(name)
if tag.tagtype != tagtype:
self.compiler.error('Referenced tag \'%s\' as %s but it '
'is %s' % (name, tagtype, tag.tagtype))
return tag
except KeyError:
tag = Tag(self.compiler, tagtype, name)
self.tags[name] = tag
return tag
class CSyntaxError(Exception):
def __init__(self, filename, line, message):
self.file = filename
self.line = line
self.message = message
if __name__ == '__main__':
cc = Ds9kCCompiler()
cc.init_from_argv()
try:
cc.run()
except CSyntaxError as e:
red = ''
norm = ''
if sys.stderr.isatty():
red = '\x1b[1;31m'
norm = '\x1b[0m'
if e.file is not None:
msg = "%s:%d: %serror%s: %s" % (e.file, e.line, red, norm, e.message)
exitstatus = 1
else:
msg = "%scommand-line error%s: %s" % (red, norm, e.message)
exitstatus = 2
print(msg, file=sys.stderr)
sys.exit(exitstatus)
except Exception as e:
# ICE go home!
e.add_note("Internal compiler error at %s:%d" %
(cc.src_filename, cc.line))
raise e
except KeyboardInterrupt as e:
e.add_note("Aborted at %s:%d" %
(cc.src_filename, cc.line))
raise e
|