-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.c
6147 lines (5200 loc) · 182 KB
/
main.c
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
/*
* This file main.c is part of L1vm.
*
* (c) Copyright Stefan Pietzonke (jay-t@gmx.net), 2017
*
* L1vm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* L1vm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with L1vm. If not, see <http://www.gnu.org/licenses/>.
*/
// l1com RISC compiler
//
// line 1694: assign to normal variable code
#include "../include/global.h"
#include "../include/opcodes.h"
#include "translate.h"
#include "main.h"
#include "if.h"
S8 linenum ALIGN = 1;
S8 label_ind ALIGN = -1;
S8 call_label_ind ALIGN = -1;
struct ast ast[MAXBRACKETLEVEL];
S8 ast_level ALIGN;
S8 data_ind ALIGN = -1;
// assembly text output
U1 **data = NULL;
U1 **code = NULL;
S8 line_len ALIGN = MAXLINES;
S8 data_line ALIGN = 0;
S8 code_line ALIGN = 0;
U1 regi[MAXREG][MAXSTRLEN];
U1 regd[MAXREG][MAXSTRLEN];
U1 save_regi[MAXREG];
U1 save_regd[MAXREG];
struct t_var t_var;
struct data_info data_info[MAXDATAINFO];
struct label label[MAXLABELS];
struct call_label call_label[MAXLABELS];
U1 inline_asm = 0; // set to one, if inline assembly is used
U1 optimize_if = 0; // set to one to optimize if call
// automatic set loadreg if no more stpopi or stpopd opcode is used
U1 call_set = 0;
U1 call_loadreg = 0;
// set all warnings as errors if 1
U1 warnings_as_errors = 0;
// don't use pull opcodes to optimize chained math expressions in "parse-rpolish.c" math expressions
U1 no_var_pull = 0;
void init_ast (void)
{
S4 i, j;
for (i = 0; i < MAXBRACKETLEVEL; i++)
{
for (j = 0; j < MAXEXPRESSION; j++)
{
ast[i].expr_reg[j] = -2;
ast[i].expr_type[j] = -2;
}
}
}
S2 check_brackets_match (U1 *line)
{
S4 slen;
S4 i;
S4 open_brackets = 0, close_brackets = 0;
U1 is_space = 0;
slen = strlen_safe ((const char *) line, MAXLINELEN);
for (i = 0; i < slen; i++)
{
if (line[i] == '{')
{
// RPN expression start: return OK
return (0);
}
if (line[i] == '(')
{
open_brackets++;
}
if (line[i] == ')')
{
close_brackets++;
// check if empty bracket
if (i > 0)
{
if (line[i - 1] == '(')
{
// empty bracket found: FATAL ERROR!!!
return (1);
}
}
if (is_space == 1)
{
// empty bracket found: FATAL ERROR!!!
return (1);
}
}
if (line[i] == ' ')
{
is_space = 1;
}
else
{
if (line[i] != '(' && line[i] != ')' && line[i] != ' ')
{
is_space = 0;
}
}
}
if (open_brackets != close_brackets)
{
return (1); // ERROR
}
else
{
return (0); // all ok
}
}
S2 get_ast (U1 *line, U1 *parse_cont)
{
S4 slen;
S4 pos = 0, argstart;
U1 ok = 0;
U1 arg = 0;
slen = strlen_safe ((const char *) line, MAXLINELEN);
S4 arg_ind = -1, arg_pos;
S4 ast_ind = -1;
ast_level = -1;
S4 exp_ind = 0;
// set to true if continous variable number sequence found:
// {x + y * z a =}
*parse_cont = 0;
S4 i;
for (i = 0; i < MAXBRACKETLEVEL; i++)
{
ast[i].expr_max = 0;
}
if (check_brackets_match (line) == 1)
{
// brackets don't match!
return (2);
}
// printf ("> '%s'\n", line);
while (! ok)
{
// printf ("top: %c\n", line[pos]);
if (line[pos] == ' ')
{
pos++;
if (pos >= slen)
{
ok = 1;
}
}
else
{
if (line[pos] == '(' || line[pos] == '{' )
{
// check if continous variable assign:
if (line[pos] == '{')
{
*parse_cont = 1;
// printf ("DEBUG get_ast: parse_cont return\n");
return (0);
}
ast_ind++;
if (ast_level < ast_ind)
{
ast_level = ast_ind;
}
#if DEBUG
printf ("AST bracket level: %lli\n", ast_level);
#endif
pos++;
if (pos >= slen)
{
ok = 1;
break;
}
}
else
{
argstart = pos;
arg_pos = 0;
arg_ind++;
arg = 0;
exp_ind = ast[ast_ind].expr_max;
while (! arg)
{
if (line[pos] == '"')
{
ast[ast_ind].expr[exp_ind][arg_ind][arg_pos] = line[pos];
arg_pos++;
if (arg_pos >= MAXLINELEN)
{
printf ("error: line %lli: argument too long!\n", linenum);
return (1);
}
pos++;
while (line[pos] != '"')
{
ast[ast_ind].expr[exp_ind][arg_ind][arg_pos] = line[pos];
arg_pos++;
if (arg_pos >= MAXLINELEN)
{
printf ("error: line %lli: argument too long!\n", linenum);
return (1);
}
pos++;
if (pos >= slen)
{
ok = 1;
break;
}
}
ast[ast_ind].expr[exp_ind][arg_ind][arg_pos] = '"';
arg_pos++;
if (arg_pos >= MAXLINELEN)
{
printf ("error: line %lli: argument too long!\n", linenum);
return (1);
}
pos++;
ast[ast_ind].expr[exp_ind][arg_ind][arg_pos] = '\0';
ast[ast_ind].expr_max = exp_ind;
arg = 1;
// printf ("get_ast: string: '%s'\n", ast[ast_ind].expr[exp_ind][arg_ind]);
break;
}
else
{
if (line[pos] != ' ' && line[pos] != ',' && line[pos] != '\n' && line[pos] != ')' && line[pos] != '}')
{
// BUGFIX -> avoid write byte error in valgrind.
// do check of all index variables!!!
if (ast_ind >= 0 && exp_ind >= 0 && arg_ind >= 0 && arg_pos >= 0)
{
ast[ast_ind].expr[exp_ind][arg_ind][arg_pos] = line[pos];
arg_pos++;
if (arg_pos >= MAXLINELEN)
{
printf ("error: line %lli: argument too long!\n", linenum);
return (1);
}
}
}
else
{
if (ast_ind == -1)
{
// brackets don't match!
return (2);
}
// printf ("ast_ind: %i, exp_ind: %i, arg_ind: %i, arg_pos: %i\n", ast_ind, exp_ind, arg_ind, arg_pos);
// BUGFIX -> avoid write byte error in valgrind.
// do check of all index variables!!!
if (ast_ind >= 0 && exp_ind >= 0 && arg_ind >= 0 && arg_pos >= 0)
{
ast[ast_ind].expr[exp_ind][arg_ind][arg_pos] = '\0';
// printf ("[ %s ]\n", ast[ast_ind].expr[exp_ind][arg_ind]);
}
if (line[pos] == ')' || line[pos] == '}')
{
// next char is open bracket, new expression next
// BUGFIX -> avoid write byte error in valgrind.
// do check of all index variables!!!
if (ast_ind >= 0 && exp_ind >= 0)
{
ast[ast_ind].expr_args[exp_ind] = arg_ind;
ast[ast_ind].expr_max = exp_ind;
exp_ind++; arg_ind = -1;
if (exp_ind >= MAXEXPRESSION)
{
printf ("error: line %lli: too much expressions!\n", linenum);
return (1);
}
ast[ast_ind].expr_max = exp_ind;
ast[ast_ind].expr_args[exp_ind] = arg_ind;
ast_ind--; arg_ind = -1;
}
}
arg = 1;
if (pos == slen - 2)
{
// end of line
ok = 1;
break;
}
}
}
pos++;
if (pos == slen)
{
ok = 1;
break;
}
}
if (arg_ind >= MAXARGS)
{
printf ("error: line %lli: too much arguments!\n", linenum);
return (1);
}
}
}
}
if (ast_ind != -1)
{
// brackets don't match!
return (2);
}
return (0);
}
S2 get_label_pos (U1 *labelname)
{
S8 i ALIGN;
S8 label_index ALIGN = -1;
for (i = 0; i < MAXLABELS; i++)
{
if (strcmp ((const char *) label[i].name, (const char *) labelname) == 0)
{
label_index = i;
break;
}
}
return (label_index);
}
U1 *get_variable_value (S8 maxind, U1 *name)
{
S8 i ALIGN;
for (i = 0; i < maxind; i++)
{
if (strcmp ((const char *) name, (const char *) data_info[i].name) == 0)
{
return (data_info[i].value_str);
}
}
return (NULL); // variable name not found, return empty string
}
S2 check_for_brackets (U1 *line)
{
S2 i, len;
S2 brackets_open = 0, brackets_close = 0;
S2 brackets_found = 0;
len = strlen_safe ((const char *) line, MAXLINELEN);
if (len > 0)
{
for (i = 0; i < len; i++)
{
if (line[i] == '(')
{
brackets_open++;
brackets_found = 1;
}
if (line[i] == ')')
{
brackets_close++;
}
}
}
// check if brackets counter is 0, all opening brackets are closed or not
if (brackets_open != brackets_close)
{
// printf ("error: line %lli: brackets don't match!\n", linenum);
// printf ("DEBUG check_for_brackets\n");
return (0);
}
return (brackets_found); // no brackets in line
}
S8 loadreg (void)
{
S8 e;
U1 str[MAXSTRLEN];
U1 code_temp[MAXSTRLEN];
// load double registers
for (e = MAXREG - 1; e >= 0; e--)
{
if (save_regd[e] == 1)
{
strcpy ((char *) code_temp, "stpopd ");
sprintf ((char *) str, "%lli", e);
strcat ((char *) code_temp, (const char*) str);
strcat ((char *) code_temp, "\n");
code_line++;
if (code_line >= line_len)
{
printf ("error: line %lli: code list full!\n", linenum);
return (1);
}
strcpy ((char *) code[code_line], (const char *) code_temp);
}
}
// load integer registers
for (e = MAXREG - 1; e >= 0; e--)
{
if (save_regi[e] == 1)
{
strcpy ((char *) code_temp, "stpopi ");
sprintf ((char *) str, "%lli", e);
strcat ((char *) code_temp, (const char*) str);
strcat ((char *) code_temp, "\n");
code_line++;
if (code_line >= line_len)
{
printf ("error: line %lli: code list full!\n", linenum);
return (1);
}
strcpy ((char *) code[code_line], (const char *) code_temp);
}
}
init_registers ();
return (0);
}
S2 check_for_whitespace (U1 *line)
{
S8 str_len ALIGN;
S8 i ALIGN;
U1 space = ' ';
U1 tab = 8;
U1 ret = 13;
U1 newline = 10;
str_len = strlen_safe ((const char *) line, MAXLINELEN);
if (str_len > 0)
{
for (i = 0; i < str_len; i++)
{
// printf ("check_for_whitespace: char num: %i, ascii code: %i\n", i, line[i]);
if (line[i] != space && line[i] != tab && line[i] != ret && line[i] != newline)
{
// ERROR no whitespace char found
return (1);
}
}
}
// all OK!
return (0);
}
S2 parse_line (U1 *line)
{
S4 level, j, last_arg, last_arg_2, t, v, reg, reg2, reg3, reg4, target, e, exp;
U1 ok;
S8 i ALIGN;
// for convert brackets expression to RPN
U1 conv[MAXSTRLEN];
U1 str[MAXSTRLEN];
U1 code_temp[MAXSTRLEN];
S8 if_pos ALIGN;
U1 if_label[MAXSTRLEN];
U1 else_label[MAXSTRLEN];
U1 endif_label[MAXSTRLEN];
S8 while_pos ALIGN;
U1 while_label[MAXSTRLEN];
S8 for_pos ALIGN;
U1 for_label[MAXSTRLEN];
S8 switch_pos ALIGN;
U1 set_loadreg = 0;
// "=" equal expression track vars:
S8 found_let ALIGN = 0;
S8 found_let_cont ALIGN = 0;
// returned by get_ast ()
// to parse: {a = x + y * z} like code stuff!
U1 parse_cont = 0;
S2 ret;
// multi line array assign
U1 array_multi = 0;
// for variable assign type checks
U1 target_var_type = UNKNOWN;
U1 expression_var_type_max = UNKNOWN;
S2 expression_if_op = -1; // set to 0 if math operation found. set to 1 if on if operator
ret = get_ast (line, &parse_cont);
if (ret == 1)
{
// parsing error
return (1);
}
if (ret == 2)
{
if (check_for_whitespace (line) == 1)
{
printf ("error: line %lli: brackets don't match!\n", linenum);
return (2);
}
else
{
// printf ("line %lli: blank line, ignored!\n", linenum);
return (0);
}
}
if (parse_cont)
{
// printf ("DEBUG parse_cont: '%s'\n", line);
if (check_for_brackets (line) == 1)
{
// found brackets in math expression, convert to RPN
if (convert (line, conv) == 1)
{
printf ("error: line: %lli can't convert infix to RPN!\n", linenum);
return (1);
}
// printf ("DEBUG: parse line: exp: '%s'\n", line);
// printf ("DEBUG: parse line: RPN: '%s'\n", conv);
if (parse_rpolish (conv) != 0)
{
printf ("error: line: %lli can't parse part in { }\n", linenum);
return (1);
}
return (0);
}
else
{
if (parse_rpolish (line) != 0)
{
printf ("error: line: %lli can't parse part in { }\n", linenum);
return (1);
}
return (0);
}
}
// walking the AST
for (level = ast_level; level >= 0; level--)
{
#if DEBUG
printf ("level: %i, expr max: %i\n", level, ast[level].expr_max);
#endif
if (ast[level].expr_max > -1)
{
for (j = 0; j <= ast[level].expr_max; j++)
{
#if DEBUG
{
printf ("ast level %i: j: %i, expression args: %i\n", level, j, ast[level].expr_args[j]);
S8 exp_ind ALIGN;
for (exp_ind = 0; exp_ind <= ast[level].expr_args[j]; exp_ind++)
{
printf ("exp: ind: %lli, string: '%s'\n", exp_ind, ast[level].expr[j][exp_ind]);
}
printf ("\n");
}
#endif
if (ast[level].expr_args[j] > -1 )
{
if (strcmp ((const char *) ast[level].expr[j][0], "set") == 0)
{
// set data type of variable
// get variable type, size, name and value
if (ast[level].expr_args[j] < 3)
{
printf ("error: line %lli: wrong data definition!\n", linenum);
return (1);
}
ok = 0;
data_ind++;
if (data_ind == MAXDATAINFO)
{
printf ("error: line %lli: data list full!\n", linenum);
return (1);
}
// normal variable definition ===========================================
if (strcmp ((const char *) ast[level].expr[j][1], "byte") == 0)
{
data_info[data_ind].type = BYTE;
data_info[data_ind].type_size = sizeof (U1);
strcpy ((char *) data_info[data_ind].type_str, "B");
data_info[data_ind].constant = 0;
ok = 1;
}
if (strcmp ((const char *) ast[level].expr[j][1], "int16") == 0)
{
data_info[data_ind].type = WORD;
data_info[data_ind].type_size = sizeof (S2);
strcpy ((char *) data_info[data_ind].type_str, "W");
data_info[data_ind].constant = 0;
ok = 1;
}
if (strcmp ((const char *) ast[level].expr[j][1], "int32") == 0)
{
data_info[data_ind].type = DOUBLEWORD;
data_info[data_ind].type_size = sizeof (S4);
strcpy ((char *) data_info[data_ind].type_str, "D");
data_info[data_ind].constant = 0;
ok = 1;
}
if (strcmp ((const char *) ast[level].expr[j][1], "int64") == 0)
{
data_info[data_ind].type = QUADWORD;
data_info[data_ind].type_size = sizeof (S8);
strcpy ((char *) data_info[data_ind].type_str, "Q");
data_info[data_ind].constant = 0;
ok = 1;
}
if (strcmp ((const char *) ast[level].expr[j][1], "double") == 0)
{
data_info[data_ind].type = DOUBLEFLOAT;
data_info[data_ind].type_size = sizeof (F8);
strcpy ((char *) data_info[data_ind].type_str, "F");
data_info[data_ind].constant = 0;
ok = 1;
}
if (strcmp ((const char *) ast[level].expr[j][1], "string") == 0)
{
data_info[data_ind].type = STRING;
data_info[data_ind].type_size = sizeof (U1);
strcpy ((char *) data_info[data_ind].type_str, "B");
data_info[data_ind].constant = 0;
ok = 1;
}
// const, constant variable definition ==================================
if (strcmp ((const char *) ast[level].expr[j][1], "const-byte") == 0)
{
data_info[data_ind].type = BYTE;
data_info[data_ind].type_size = sizeof (U1);
strcpy ((char *) data_info[data_ind].type_str, "B");
data_info[data_ind].constant = 1;
ok = 1;
}
if (strcmp ((const char *) ast[level].expr[j][1], "const-int16") == 0)
{
data_info[data_ind].type = WORD;
data_info[data_ind].type_size = sizeof (S2);
strcpy ((char *) data_info[data_ind].type_str, "W");
data_info[data_ind].constant = 1;
ok = 1;
}
if (strcmp ((const char *) ast[level].expr[j][1], "const-int32") == 0)
{
data_info[data_ind].type = DOUBLEWORD;
data_info[data_ind].type_size = sizeof (S4);
strcpy ((char *) data_info[data_ind].type_str, "D");
data_info[data_ind].constant = 1;
ok = 1;
}
if (strcmp ((const char *) ast[level].expr[j][1], "const-int64") == 0)
{
data_info[data_ind].type = QUADWORD;
data_info[data_ind].type_size = sizeof (S8);
strcpy ((char *) data_info[data_ind].type_str, "Q");
data_info[data_ind].constant = 1;
ok = 1;
}
if (strcmp ((const char *) ast[level].expr[j][1], "const-double") == 0)
{
data_info[data_ind].type = DOUBLEFLOAT;
data_info[data_ind].type_size = sizeof (F8);
strcpy ((char *) data_info[data_ind].type_str, "F");
data_info[data_ind].constant = 1;
ok = 1;
}
if (strcmp ((const char *) ast[level].expr[j][1], "const-string") == 0)
{
data_info[data_ind].type = STRING;
data_info[data_ind].type_size = sizeof (U1);
strcpy ((char *) data_info[data_ind].type_str, "B");
data_info[data_ind].constant = 1;
ok = 1;
}
if (ok == 0)
{
printf ("error: line %lli: unknown data type!\n", linenum);
return (1);
}
// name
strcpy ((char *) data_info[data_ind].name, (const char *) ast[level].expr[j][3]);
// size
if (checkdigit (ast[level].expr[j][2]) != 1)
{
if (data_info[data_ind].type != STRING)
{
printf ("error: line %lli: data size not a number!\n", linenum);
return (1);
}
}
data_info[data_ind].size = get_temp_int ();
// Set real string size, if 0 length was set or "s" was set.
if ((data_info[data_ind].size == 0 || ast[level].expr[j][2][0] == 's') && data_info[data_ind].type == STRING)
{
// set string size automatically:
data_info[data_ind].size = strlen_safe ((char *) ast[level].expr[j][4], MAXLINELEN) - 1;
// set size string:
sprintf ((char *) ast[level].expr[j][2], "%lli", data_info[data_ind].size);
}
// value
if (ast[level].expr_args[j] >= 4)
{
if ((data_info[data_ind].type >= BYTE && data_info[data_ind].type <= QUADWORD) || data_info[data_ind].type == DOUBLEFLOAT)
{
if (checkdigit (ast[level].expr[j][4]) != 1)
{
if (get_variable_value (data_ind, ast[level].expr[j][4]) != 0)
{
strcpy ((char *) data_info[data_ind].value_str, (const char *) get_variable_value (data_ind, ast[level].expr[j][4]));
}
else
{
if (ast[level].expr[j][4][0] == '$')
{
// path for data filename
strcpy ((char *) data_info[data_ind].value_str, (const char *) ast[level].expr[j][4]);
}
else
{
printf ("error: line %lli: value not a number and not a variable!\n", linenum);
return (1);
}
}
}
else
{
{
// check if variable value is in legal range
S8 value ALIGN;
U1 *ptr;
value = strtoll((const char *) ast[level].expr[j][4], (char **) &ptr, 10);
switch (data_info[data_ind].type)
{
case BYTE:
if (value < 0 || value > UCHAR_MAX)
{
printf ("error: line %lli: byte value out of range!\n", linenum);
return (1);
}
break;
case WORD:
if (value < SHRT_MIN || value > SHRT_MAX)
{
printf ("error: line %lli: int16 value out of range!\n", linenum);
return (1);
}
break;
case DOUBLEWORD:
if (value < -2147483648 || value > 2147483647)
{
printf ("error: line %lli: int32 value out of range!\n", linenum);
return (1);
}
break;
case QUADWORD:
if (value < LLONG_MIN || value > LLONG_MAX)
{
printf ("error: line %lli: int64 value out of range!\n", linenum);
return (1);
}
break;
}
strcpy ((char *) data_info[data_ind].value_str, (const char *) ast[level].expr[j][4]);
}
}
}
if (data_info[data_ind].type == STRING)
{
if ((S8) (strlen_safe ((const char *) ast[level].expr[j][4], MAXLINELEN) - 1) > data_info[data_ind].size)
{
printf ("error: line %lli: string variable: '%s' too small for string!\n", linenum, ast[level].expr[j][4]);
return (1);
}
strcpy ((char *) data_info[data_ind].value_str, (const char *) ast[level].expr[j][4]);
}
}
if (data_line == 0)
{
// first data, set ".data" and start offset
strcpy ((char *) data[data_line], ".data\n");
data_line++;
if (data_line >= line_len)
{
printf ("error: line %lli: data list full!\n", linenum);
return (1);
}
data_info[data_ind].offset = 0;
data_info[data_ind].end = data_info[data_ind].offset + (data_info[data_ind].size * data_info[data_ind].type_size);
strcpy ((char *) data[data_line], (const char *) data_info[data_ind].type_str);
strcat ((char *) data[data_line], ", ");
strcat ((char *) data[data_line], (const char *) ast[level].expr[j][2]);
strcat ((char *) data[data_line], ", ");
strcat ((char *) data[data_line], (const char *) data_info[data_ind].name);
strcat ((char *) data[data_line], "\n");
data_line++;
if (data_line >= line_len)
{
printf ("error: line %lli: data list full!\n", linenum);
return (1);
}
strcpy ((char *) data[data_line], "@, ");
sprintf ((char *) str, "%lli", data_info[data_ind].offset);
strcat ((char *) data[data_line], (const char *) str);
strcat ((char *) data[data_line], "Q, ");
strcat ((char *) data[data_line], (const char *) data_info[data_ind].value_str);
if (ast[level].expr_args[j] > 4)
{
S8 array_index ALIGN = 0;
for (i = 5; i <= ast[level].expr_args[j]; i++)
{
if ((data_info[data_ind].type >= BYTE && data_info[data_ind].type <= QUADWORD) || data_info[data_ind].type == DOUBLEFLOAT)
{
array_index++; // count assigned array variables
if (ast[level].expr[j][i][0] == '/')
{
// found end of line mark of multi line array data
array_multi = 1;
continue;
}
if (checkdigit (ast[level].expr[j][i]) != 1)
{
if (get_variable_value (data_ind, ast[level].expr[j][i]) != 0)
{
strcpy ((char *) data_info[data_ind].value_str, (const char *) get_variable_value (data_ind, ast[level].expr[j][i]));
}
else
{
printf ("error: line %lli: value not a number and not a variable!\n", linenum);
return (1);
}
}
else
{
strcpy ((char *) data_info[data_ind].value_str, (const char *) ast[level].expr[j][i]);
}
}
strcat ((char *) data[data_line], ", ");
strcat ((char *) data[data_line], (const char *) data_info[data_ind].value_str);
}
if (array_multi == 0)
{
strcat ((char *) data[data_line], ", ;");
}
if (array_index >= atoi ((const char *) ast[level].expr[j][2]))
{
// error: array variable overflow!
printf ("error: line %lli: array variable overflow!\n", linenum);
return (1);
}
}
strcat ((char *) data[data_line], "\n");
}
else
{
// use offset from last data end
data_info[data_ind].offset = data_info[data_ind - 1].end;
data_info[data_ind].end = data_info[data_ind].offset + (data_info[data_ind].size * data_info[data_ind].type_size);
data_line++;
if (data_line >= line_len)
{
printf ("error: line %lli: data list full!\n", linenum);
return (1);
}
strcpy ((char *) data[data_line], (const char *) data_info[data_ind].type_str);
strcat ((char *) data[data_line], ", ");
strcat ((char *) data[data_line], (const char *) ast[level].expr[j][2]);
strcat ((char *) data[data_line], ", ");
strcat ((char *) data[data_line], (const char *) data_info[data_ind].name);
strcat ((char *) data[data_line], "\n");
data_line++;
if (data_line >= line_len)
{
printf ("error: line %lli: data list full!\n", linenum);
return (1);
}
strcpy ((char *) data[data_line], "@, ");
sprintf ((char *) str, "%lli", data_info[data_ind].offset);
strcat ((char *) data[data_line], (const char *) str);
strcat ((char *) data[data_line], "Q, ");
strcat ((char *) data[data_line], (const char *) data_info[data_ind].value_str);
if (ast[level].expr_args[j] > 4 && data_info[data_ind].type != STRING)
{
S8 array_index ALIGN = 0;
for (i = 5; i <= ast[level].expr_args[j]; i++)
{
if ((data_info[data_ind].type >= BYTE && data_info[data_ind].type <= QUADWORD) || data_info[data_ind].type == DOUBLEFLOAT)
{
array_index++; // count assigned array variables
if (ast[level].expr[j][i][0] == '/')