-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler.c
1663 lines (1522 loc) · 57.8 KB
/
assembler.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 is part of Minimalist Casio Assembler (MCA).
MCA 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.
MCA 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 MCA. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
#include "assembler.h"
#include "assembler_private.h"
#include "opcodes.h"
#ifndef TESTING_PC
#include <fxstdio.h>
#else
#include <stdio.h>
#endif
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define COUNT_STEP 0
#define SYMBOLS_STEP 1
#define OPCODE_STEP 2
#define FIND_ABS_STEP 3
#define STEP_NUMBER 4
// Flag used in the symbols_flags array
#define SYMFLAG_NOTHING 0x00
#define SYMFLAG_EXTERN 0x01
#define SYMFLAG_GLOBAL 0x02
#define SYMFLAG_LABEL 0x04
#define SYMFLAG_CONSTVAL 0x08
#define SYMFLAG_EXT_USED 0x10
#define MAX_SECTIONS 20
#define SIZEOF_SECTIONDATA_4 (sizeof(int))
struct _SectionData {
char *name;
int data_size;
int data_pos; // in bytes, *not* in instruction number (words)
unsigned char *data;
// To store all the 'absolute' address positions (the object file need that) :
int abs_size;
int abs_pos; // current position in abs_address array
unsigned int *abs_address; // address in the section where a .long data must be converted to absolute address
unsigned char *abs_section_id; // the section associated with the address
// to store external symbols informations of this section
int ext_size;
int ext_pos;
unsigned int *ext_address;
int *ext_symbols_id; // the symbol ID of each external symbol
};
#define SIZEOF_SYMBOLDATA_1 (sizeof(unsigned char) + sizeof(unsigned char))
#define SIZEOF_SYMBOLDATA_4 (sizeof(char*)+sizeof(unsigned int))
// Idée : pour DO_STEP, faire comme si on ne connaissait aucun symbol pour les opcodes
// (les remplacer par 0), et enregistrer une table de correspondance label->position
// pour SYMBOL_STEP, parser le fichier à la recherche d'opcodes contenant un Label,
// et réécrire chacun d'eux, tout en notant les symboles non résolus
// WARNING : this is a spaghetti function, so don't worry if you see a lot of strange
// branching, as 'continue', and please don't blame me : write a parser in C with these
// constraints isn't a good way to follow 'good programing' conventions ;)
int asm2objfile(FONTCHARACTER *in, FONTCHARACTER *out) {
FILE *infile = NULL;
char str[40];
char linebuf[200];
char *line;
char *tmpline;
int i;
for(i=0; (i<40) && ((str[i]=(char)in[i]) != 0); i++);
infile = fopen(str, "r");
if(infile == NULL) return -1;
int error = 0;
int parse_step = 0;
int current_line = 0;
// For symbol (label or constant value) computing
int symbols_number = 0;
int current_symbol = 0;
char **symbols_name = NULL;
unsigned int *symbols_value = NULL;
// To indicate misc symbols informations (as "external" or "to export") :
unsigned char *symbols_flags = NULL;
unsigned char *symbols_section_id = NULL; // only used if the symbol isn't a constant nor an exported symbol
// For sections informations :
struct _SectionData *sections[MAX_SECTIONS];
// nothing ?
sections[0] = calloc(1, sizeof(struct _SectionData));
sections[0]->name = ".text";
// 0
int sections_number = 1;
int current_section = 0;
// Internal memory pool system :
int mempool_pos_1 = 0;
int mempool_size_1 = 0;
void *mempool_1 = NULL; // memory pool for '.align 1' data (as char)
int mempool_pos_4 = 0;
int mempool_size_4 = 0;
void *mempool_4 = NULL; // memory pool for '.align 4' data (as int or pointer)
void *mempool_absdata = NULL; // special memory pool for absolute address informations
void *mempool_extdata = NULL; // special memory pool for external address informations
int line_number;
while(parse_step<STEP_NUMBER && !error) {
line_number = 0;
while(fgets(linebuf, 200, infile)!=NULL && !error) {
int tmp;
line_number++;
line = linebuf;
SKIP_BLANK(line);
// If the line is a comment or is empty, go next line
if(line[0]=='!' || line[0]==';' || line[0]=='\n' || line[0]==0) continue;
// Is the line a label?
for(i=0; i<40 && isalnum(line[i]) || line[i]=='_'; i++);
if(i<40 && line[i]==':') {
// A label name can't start by a digit (0~9)
if(isdigit(line[0])) {
error = -100;
continue;
}
if(sections_number <= 0) {
error = -106;
continue;
}
tmpline = (char*)((int)line+i+1);
SKIP_BLANK(tmpline);
// Nothing else a end of line/string or a comment after a label
if(tmpline[0]!='!' && tmpline[0]!=';' && tmpline[0]!='\n' && tmpline[0]!=0) {
error = -109;
continue;
}
if(parse_step == COUNT_STEP) {
mempool_size_1 += i+1+SIZEOF_SYMBOLDATA_1;
mempool_size_4 += SIZEOF_SYMBOLDATA_4;
symbols_number++;
}
else if(parse_step == SYMBOLS_STEP) {
memcpy(str, line, i);
str[i]=0;
int endstr=i;
// Check if the symbol doesn't exist yet
for(i=0; i<current_symbol && !(strcmp(str, symbols_name[i])==0); i++);
if(i<current_symbol) {
error = -202;
continue;
}
symbols_name[current_symbol] = (char*)((int)mempool_1+mempool_pos_1);
symbols_value[current_symbol] = sections[current_section]->data_pos;
symbols_flags[current_symbol] = SYMFLAG_LABEL;
symbols_section_id[current_symbol] = current_section;
memcpy((char*)((int)mempool_1+mempool_pos_1), line, endstr);
mempool_pos_1 += endstr+1; // don't forget the '\0' !
current_symbol++;
}
continue;
}
// Is the line an assembler directive?
if(line[0]=='.') {
for(i=0; i<40 && !IS_SEP(line[i], ','); i++) str[i]=line[i];
if (i>=40) {
error = -103;
continue;
}
str[i] = 0;
char *tmpstr = (char*)((int)line + i);
SKIP_BLANK(tmpstr);
int numbersize = 0;
if(strcmp(str, ".byte")==0) numbersize = 8;
else if(strcmp(str, ".word")==0) numbersize = 16;
else if(strcmp(str, ".long")==0) numbersize = 32;
else if(strcmp(str, ".align")==0) {
int alignsize;
if(sections_number <= 0) {
error = -106;
continue;
}
if(tmpstr[0]=='1') continue; // ".align 1" does nothing...
else if(tmpstr[0]=='2') alignsize = 2;
else if(tmpstr[0]=='4') alignsize = 4;
else {
error = -104;
}
int aligndelta;
if(parse_step <= COUNT_STEP) aligndelta = alignsize-(sections[current_section]->data_size % alignsize);
else aligndelta = alignsize-(sections[current_section]->data_pos % alignsize);
if(aligndelta == alignsize) aligndelta=0;
if(parse_step==COUNT_STEP) {
sections[current_section]->data_size += aligndelta;
mempool_size_1 += aligndelta;
}
else if(parse_step==SYMBOLS_STEP || parse_step==FIND_ABS_STEP) sections[current_section]->data_pos += aligndelta;
else if(parse_step==OPCODE_STEP) {
unsigned char *tmpdata = (unsigned char*)((int)(sections[current_section]->data) + sections[current_section]->data_pos);
for(i=0; i<aligndelta; i++) tmpdata[i]=0x00;
sections[current_section]->data_pos += aligndelta;
}
continue;
}
else if(strcmp(str, ".section")==0) {
if(tmpstr[0]!='"') {
error = -107;
continue;
}
for(i=0; i<40 && tmpstr[i+1]!='"' && tmpstr[i+1]!=0 && tmpstr[i+1]!='\n'; i++) str[i]=tmpstr[i+1];
int endstr=i;
if(i>=40 || tmpstr[i+1]!='"') {
error = -107;
continue;
}
str[i]=0;
int namesize = i;
// Check if the section doesn't exist yet
for(i=0; i<sections_number && !(strcmp(sections[i]->name, str)==0); i++);
if(i >= sections_number) {
// New section
if(parse_step != COUNT_STEP) {
error = -104;
continue;
}
if(sections_number >= MAX_SECTIONS) {
error = -108;
continue;
}
sections[sections_number] = calloc(sizeof(struct _SectionData)+namesize+1, sizeof(char));
if(sections[sections_number] == NULL) {
error = -2;
continue;
}
char *nameptr = (char*)((int)(sections[sections_number])+sizeof(struct _SectionData));
memcpy(nameptr, str, namesize+1);
sections[sections_number]->name = nameptr;
current_section = sections_number;
sections_number++;
}
else {
// A section with the same name already exist
current_section = i;
}
continue;
}
else if(strcmp(str, ".global")==0) {
if(parse_step == OPCODE_STEP) {
line = tmpstr;
for(i=0; i<40 && isalnum(line[i]) || line[i]=='_'; i++);
if(i>=40 || !IS_SEP(line[i], ',')) {
error = -110;
continue;
}
memcpy(str, line, i);
str[i]=0;
for(i=0; i<symbols_number && !(strcmp(str, symbols_name[i])==0); i++);
if(i<symbols_number) {
if(symbols_flags[i] & SYMFLAG_EXTERN) {
error = -204;
continue;
}
symbols_flags[i] |= SYMFLAG_GLOBAL;
continue;
}
else {
error = -114;
continue;
}
}
else continue;
}
else if(strcmp(str, ".extern")==0) {
line = tmpstr;
for(i=0; i<40 && isalnum(line[i]) || line[i]=='_'; i++);
if(i>=40 || !IS_SEP(line[i], ',')) {
error = -110;
continue;
}
// A label name can't start by a digit (0~9)
if(isdigit(line[0])) {
error = -100;
continue;
}
if(parse_step == COUNT_STEP) {
mempool_size_1 += i+1+SIZEOF_SYMBOLDATA_1;
mempool_size_4 += SIZEOF_SYMBOLDATA_4;
symbols_number++;
}
else if(parse_step == SYMBOLS_STEP) {
int endstr = i;
memcpy(str, line, endstr);
str[endstr]=0;
for(i=0; i<current_symbol && !(strcmp(str, symbols_name[i])==0); i++);
if(i<current_symbol) {
error = -202;
continue;
}
else {
symbols_name[current_symbol] = (char*)((int)mempool_1+mempool_pos_1);
symbols_value[current_symbol] = 0;
symbols_flags[current_symbol] = SYMFLAG_EXTERN;
symbols_section_id[current_symbol] = 0xFF;
memcpy((char*)((int)mempool_1+mempool_pos_1), line, endstr);
mempool_pos_1 += endstr+1; // don't forget the '\0' !
current_symbol++;
}
}
continue;
}
else if(strcmp(str, ".equ")==0 || strcmp(str, ".set")==0) {
// TODO for now, .equ and .set can't modifiy an existing symbol (only can created new one)
// because of the memory pool system... So find a solution?
line = tmpstr;
for(i=0; i<40 && isalnum(line[i]) || line[i]=='_'; i++);
if(i>=40 || !IS_SEP(line[i], ',')) {
error = -110;
continue;
}
// A label name can't start by a digit (0~9)
if(isdigit(line[0])) {
error = -100;
continue;
}
if(parse_step == COUNT_STEP) {
mempool_size_1 += i+1+SIZEOF_SYMBOLDATA_1;
mempool_size_4 += SIZEOF_SYMBOLDATA_4;
symbols_number++;
}
else if(parse_step == SYMBOLS_STEP || parse_step == OPCODE_STEP) {
int endstr=i;
// Get the new symbol's value :
tmpline = (char*)((int)line+i);
SKIP_BLANK(tmpline);
if(tmpline[0] != ',') {
error = -111;
continue;
}
tmpline = (char*)((int)tmpline+1);
SKIP_BLANK(tmpline);
for(i=0; i<40 && !IS_SEP(tmpline[i], ','); i++) str[i]=tmpline[i];
if (i>=40) {
error = -105;
continue;
}
str[i] = 0;
tmpline = (char*)((int)tmpline + i);
SKIP_BLANK(tmpline);
if(tmpline[0]!='\n' && tmpline[0]!='\n' && tmpline[0]!=';' && tmpline[0]!='!') {
error = -109;
continue;
}
int value = 0;
int stnret = stringToNumber(str, &value);
if(stnret < 0) {
if(stnret==-2) error = -112;
else error = -113;
continue;
}
// now, look for an existing symbol with the same name
memcpy(str, line, endstr);
str[endstr]=0;
int maxnumber = (parse_step==SYMBOLS_STEP ? current_symbol : symbols_number);
for(i=0; i<maxnumber && !(strcmp(str, symbols_name[i])==0); i++);
if(i<maxnumber) {
// For now, no overwriting value, in this case return error...
//symbols_value[i] = value;
if(parse_step == SYMBOLS_STEP) error = -202;
continue;
}
else {
if(parse_step == OPCODE_STEP) {
error = -999;
continue;
}
symbols_name[current_symbol] = (char*)((int)mempool_1+mempool_pos_1);
symbols_value[current_symbol] = value;
symbols_flags[current_symbol] = SYMFLAG_CONSTVAL;
symbols_section_id[current_symbol] = 0xFF;
memcpy((char*)((int)mempool_1+mempool_pos_1), line, endstr);
mempool_pos_1 += endstr+1; // don't forget the '\0' !
current_symbol++;
}
}
continue;
}
// If the directive is .byte, .word or .long :
if(numbersize > 0) {
if(sections_number <= 0) {
error = -106;
continue;
}
if(parse_step==COUNT_STEP) {
sections[current_section]->data_size += numbersize>>3;
mempool_size_1 += numbersize>>3;
}
else if(parse_step==SYMBOLS_STEP) sections[current_section]->data_pos += numbersize>>3;
else if(parse_step==OPCODE_STEP || parse_step==FIND_ABS_STEP) {
int value;
int value2;
for(i=0; i<40 && !IS_SEP(tmpstr[i], ','); i++) str[i]=tmpstr[i];
if (i>=40) {
error = -103;
continue;
}
str[i] = 0;
tmpstr = (char*)((int)tmpstr + i);
SKIP_BLANK(tmpstr);
if(tmpstr[0]!='\n' && tmpstr[0]!='\n' && tmpstr[0]!=';' && tmpstr[0]!='!') {
error = -104;
continue;
}
int stnret = stringToNumber(str, &value);
if(stnret < 0) {
// Maybe it's a symbol address?
for(i=0; i<symbols_number && !(strcmp(symbols_name[i], str)==0); i++);
if(i >= symbols_number) {
error = -104;
continue;
}
if(numbersize == 32) { // Only for .long
if(!(symbols_flags[i] & SYMFLAG_EXTERN)) {
value = (int)symbols_value[i];
if(symbols_flags[i] & SYMFLAG_LABEL) {
// If the symbol is a non extern label, need to add this address to the
// positions of the section where a .long have to be "absolute extended"
if(parse_step==OPCODE_STEP) sections[current_section]->abs_size++;
else if(parse_step==FIND_ABS_STEP) {
int abspos = sections[current_section]->abs_pos;
sections[current_section]->abs_address[abspos] = sections[current_section]->data_pos;
sections[current_section]->abs_section_id[abspos] = symbols_section_id[i];
sections[current_section]->abs_pos++;
}
}
}
else {
// If the symbol is external :
if(parse_step==OPCODE_STEP) sections[current_section]->ext_size++;
else if(parse_step==FIND_ABS_STEP) {
int extpos = sections[current_section]->ext_pos;
sections[current_section]->ext_address[extpos] = sections[current_section]->data_pos;
sections[current_section]->ext_symbols_id[extpos] = i;
sections[current_section]->ext_pos++;
}
value = 0;
}
}
else {
if(!(symbols_flags[i] & SYMFLAG_CONSTVAL) || (symbols_flags[i] & SYMFLAG_EXTERN)) {
error = -203;
continue;
}
value = (int)symbols_value[i];
}
}
// if it's the FIND_ABS_STEP step, don't copy to data...
if(parse_step == FIND_ABS_STEP) {
sections[current_section]->data_pos += numbersize>>3;
continue;
}
// TODO warning system
/*else if(stnret == 1) {
if(numbersize == 8) value2 = (char)value;
else if(numbersize == 8) value2 = (short)value;
else value2 = value;
}
else {
if(numbersize == 8) value2 = (unsigned char)value;
else if(numbersize == 8) value2 = (unsigned short)value;
else value2 = value;
}
if(value2 != value) {
// Put a warning : the number is too large for this usage
}
*/
unsigned char *tmpdata = (unsigned char*)((int)(sections[current_section]->data) + sections[current_section]->data_pos);
// byte
if(numbersize == 8) {
tmpdata[0] = (char)value;
sections[current_section]->data_pos++;
}
//word (*big* endian)
else if (numbersize == 16) {
tmpdata[0] = (char)(value >> 8);
tmpdata[1] = (char)value;
sections[current_section]->data_pos += 2;
}
//long word (*big* endian)
else {
tmpdata[0] = (char)(value >> 24);
tmpdata[1] = (char)(value >> 16);
tmpdata[2] = (char)(value >> 8);
tmpdata[3] = (char)value;
sections[current_section]->data_pos += 4;
}
}
continue;
}
error = -103;
continue;
}
// Now the line is inevitably an opcode :
if(sections_number <= 0) {
error = -106;
continue;
}
// Here, considerate all these lines are valid opcodes (2 bytes)
// If there is any error, the OPCODE_STEP step will return an error code later.
if(parse_step == SYMBOLS_STEP || parse_step == FIND_ABS_STEP) {
sections[current_section]->data_pos += 2;
continue;
}
else if(parse_step == COUNT_STEP) {
sections[current_section]->data_size += 2;
mempool_size_1 += 2;
continue;
}
else if(parse_step == OPCODE_STEP) {
struct ParsedOpcode opcode;
int opret;
opret = parseOpcodeLine(line, &opcode);
if(opret<0) {
if(opret >= -5) error = -300 + opret;
else error = -300;
continue;
}
// transform arguments of type "Label" into a PC-displacement argument
if(opcode.args[0].type == ARG_TYPE_LABEL) {
for(i=0; i<symbols_number; i++) {
if(strcmp((char*)(opcode.args[0].data), symbols_name[i])==0) break;
}
if((i >= symbols_number) || (symbols_section_id[i] != current_section)) {
error = -200;
continue;
}
int disp;
if(tolower(opcode.mnemonic[0])=='m' && tolower(opcode.mnemonic[1])=='o' && tolower(opcode.mnemonic[2])=='v'
&& opcode.mnemonic[3]=='.' && tolower(opcode.mnemonic[4])=='l' && opcode.mnemonic[5]==0 ) {
disp = symbols_value[i] - (sections[current_section]->data_pos & 0xFFFFFFFC) -4;
}
else disp = symbols_value[i] - sections[current_section]->data_pos -4;
// TODO check if the label isn't too far (here or not?)
opcode.args[0].type = ARG_TYPE_DISP_PC;
opcode.args[0].data = disp;
}
unsigned short opvalue;
opret = opcodeValue(&opcode, &opvalue);
if(opret<0) {
error = -320;
continue;
}
sections[current_section]->data[sections[current_section]->data_pos] = (char)(opvalue >> 8);
sections[current_section]->data[sections[current_section]->data_pos+1] = (char)opvalue;
sections[current_section]->data_pos += 2;
continue;
}
else continue;
error=-101;
}
if(!error) if(sections_number > MAX_SECTIONS) error = -3;
if(!error) {
current_line = 0;
current_symbol = 0;
current_section = 0;
if(parse_step == COUNT_STEP) {
mempool_1 = calloc(mempool_size_1, sizeof(char));
mempool_4 = calloc(mempool_size_4, sizeof(char));
if((mempool_size_1!=0 && mempool_1==NULL) || (mempool_size_4!=0 && mempool_4==NULL)) {
error = -2;
break;
}
// Initialize array
symbols_name = (char**)((int)mempool_4+mempool_pos_4);
mempool_pos_4 += symbols_number * sizeof(char*);
symbols_value = (unsigned int*)((int)mempool_4+mempool_pos_4);
mempool_pos_4 += symbols_number * sizeof(unsigned int);
symbols_flags = (unsigned char*)((int)mempool_1+mempool_pos_1);
mempool_pos_1 += symbols_number * sizeof(unsigned char);
symbols_section_id = (unsigned char*)((int)mempool_1+mempool_pos_1);
mempool_pos_1 += symbols_number * sizeof(unsigned char);
//sections = (struct _SectionData*)((int)mempool_4+mempool_pos_4);
//mempool_pos_4 += sections_number * sizeof(struct _SectionData);
for(i=0; i<sections_number; i++) {
sections[i]->data = (unsigned char*)((int)mempool_1+mempool_pos_1);
mempool_pos_1 += sections[i]->data_size * sizeof(unsigned char);
//sections[i]->abs_address = (unsigned int*)((int)mempool_4+mempool_pos_4);
//mempool_pos_4 += sections[i]->abs_size * sizeof(unsigned int);
}
}
else if(parse_step == OPCODE_STEP) {
int total_abs_size = 0;
int total_ext_size = 0;
for(i=0; i<sections_number; i++) {
total_abs_size += sections[i]->abs_size;
total_ext_size += sections[i]->ext_size;
}
mempool_absdata = calloc(total_abs_size, sizeof(unsigned char) + sizeof(unsigned int));
mempool_extdata = calloc(total_ext_size, sizeof(int) + sizeof(unsigned int));
if(((total_abs_size > 0) && (mempool_absdata==NULL)) || ((total_ext_size > 0) && (mempool_extdata==NULL))) {
error = -2;
break;
}
int abs_pos = 0;
int ext_pos = 0;
for(i=0; i<sections_number; i++) {
sections[i]->abs_address = (unsigned int*)((int)mempool_absdata + abs_pos*4);
sections[i]->abs_section_id = (unsigned char*)((int)mempool_absdata + abs_pos + total_abs_size*4);
sections[i]->ext_address = (unsigned int*)((int)mempool_extdata + ext_pos*4);
sections[i]->ext_symbols_id = (int*)((int)mempool_extdata + ext_pos*4 + total_ext_size*4);
abs_pos += sections[i]->abs_size;
ext_pos += sections[i]->ext_size;
}
}
for(i=0; i<sections_number; i++) {
sections[i]->data_pos = 0;
sections[i]->abs_pos = 0;
sections[i]->ext_pos = 0;
}
parse_step++;
fseek(infile, 0, SEEK_SET);
}
}
// File parsed, now write the object file!
// Because of the fucking buggy Casio API, we have to write the full file in *one* call
if(!error) {
// Firstly compute the total size needed :
int size = 0;
int header_size = 7 + 4 + 4 + 4;
size += header_size;
int compiled_size = 4;
for(i=0; i<sections_number; i++)
compiled_size += strlen(sections[i]->name) + 1 + 4 + (sections[i]->abs_size*(4+1)) + 4 + sections[i]->data_size;
size += compiled_size;
int imported_size = 4;
int imported_number = 0;
for(i=0; i<sections_number; i++) {
int j;
imported_size += (1 + 4) * sections[i]->ext_size; // occurence_section_id + occurence_position
for(j=0; j<sections[i]->ext_size; j++) {
int symid = sections[i]->ext_symbols_id[j];
if((symbols_flags[symid] & SYMFLAG_EXTERN) && !(symbols_flags[symid] & SYMFLAG_EXT_USED)) {
imported_number++;
imported_size += strlen(symbols_name[symid]) + 1 + 4; // name + name_size + occurence_number
symbols_flags[symid] |= SYMFLAG_EXT_USED;
}
}
}
size += imported_size;
int exported_size = 4;
int exported_number = 0;
for(i=0; i<symbols_number; i++) {
if(symbols_flags[i] & SYMFLAG_GLOBAL) {
exported_size += 1 + strlen(symbols_name[i]) + 1 + 4;
exported_number++;
}
}
size += exported_size;
// then create and fill the buffer :
char *buffer = calloc(size, sizeof(char));
if(buffer == NULL) {
error = -2;
}
else {
int pos = 0;
// Write header
buffer[0]='M'; buffer[1]='O'; buffer[2]='F'; buffer[3]='-';
buffer[4]=MOF_MAJOR_VERSION+'0';
buffer[5]='.';
buffer[6]=MOF_MINOR_VERSION+'0';
WRITE_INTEGER(compiled_size, buffer, 7);
WRITE_INTEGER(imported_size, buffer, 11);
WRITE_INTEGER(exported_size, buffer, 15);
pos += 19;
// Write compiled data
WRITE_INTEGER(sections_number, buffer, pos);
pos += 4;
for(i=0; i<sections_number; i++) {
buffer[pos] = strlen(sections[i]->name);
memcpy((char*)((int)buffer + pos+1), sections[i]->name, (unsigned char)(buffer[pos]));
pos += (unsigned char)(buffer[pos]) + 1;
WRITE_INTEGER(sections[i]->abs_size, buffer, pos);
pos += 4;
int j;
for(j=0; j<sections[i]->abs_size; j++) {
buffer[pos] = sections[i]->abs_section_id[j];
WRITE_INTEGER(sections[i]->abs_address[j], buffer, pos+1);
pos += 1 + 4;
}
WRITE_INTEGER(sections[i]->data_size, buffer, pos);
memcpy((char*)((int)buffer + pos+4), sections[i]->data, sections[i]->data_size);
pos += 4 + sections[i]->data_size;
}
// Write imported symbols (maked with .extern)
WRITE_INTEGER(imported_number, buffer, pos);
pos += 4;
for(i=0; i<symbols_number; i++) symbols_flags[i] &= ~SYMFLAG_EXT_USED; // Clear the EXT_USED flag
for(i=0; i<sections_number; i++) {
int j;
for(j=0; j<sections[i]->ext_size; j++) {
int symid = sections[i]->ext_symbols_id[j];
unsigned char flags = symbols_flags[symid];
if((flags & SYMFLAG_EXTERN) && !(flags & SYMFLAG_EXT_USED)) {
buffer[pos] = strlen(symbols_name[symid]);
memcpy((char*)((int)buffer + pos+1), symbols_name[symid], (unsigned char)(buffer[pos]));
pos += (unsigned char)(buffer[pos]) + 1;
int tmp_pos = pos;
pos += 4; // the occurence number isn't know at this time
int occurence_num = 0;
int k;
for(k=i; k<sections_number; k++) {
int l;
for(l=0; l<sections[k]->ext_size; l++) {
if(sections[k]->ext_symbols_id[l] == symid) {
occurence_num++;
buffer[pos] = k;
WRITE_INTEGER(sections[k]->ext_address[l], buffer, pos+1);
pos += 1+4;
}
}
}
// Back-write the occurence number
WRITE_INTEGER(occurence_num, buffer, tmp_pos);
symbols_flags[symid] |= SYMFLAG_EXT_USED;
}
}
}
// Write exported symbols (marked with .global)
WRITE_INTEGER(exported_number, buffer, pos);
pos += 4;
for(i=0; i<symbols_number; i++) {
if(symbols_flags[i] & SYMFLAG_GLOBAL) {
buffer[pos] = strlen(symbols_name[i]);
memcpy((char*)((int)buffer + pos+1), symbols_name[i], (unsigned char)(buffer[pos]));
pos += (unsigned char)(buffer[pos]) + 1;
unsigned char section_id;
if(symbols_flags[i] & SYMFLAG_CONSTVAL) section_id = 0xFF;
else section_id = symbols_section_id[i];
buffer[pos] = section_id;
WRITE_INTEGER(symbols_value[i], buffer, pos+1);
pos += 1+4;
}
}
#ifndef TESTING_PC
// Buffer filled, write the real file!
// Delete and/or create the out file and open it in write mode
Bfile_DeleteFile(out);
Bfile_CreateFile(out, size);
int outputHandle = Bfile_OpenFile(out, _OPENMODE_WRITE);
if(outputHandle < 0) return -4;
if(Bfile_WriteFile(outputHandle, buffer, size) < 0) return -5;
Bfile_CloseFile(outputHandle);
#else
FILE *outfile = NULL;
outfile = fopen(out, "wb+");
if(outfile == NULL) return -4;
fwrite(buffer, sizeof(unsigned char), size, outfile);
fflush(outfile);
fclose(outfile);
#endif
}
/*FILE *outfile = NULL;
outfile = fopen(out, "wb+");
if(outfile == NULL) return -4;
fwrite(sections[0]->data, sizeof(unsigned char), sections[0]->data_size, outfile);*/
}
// else printf("Error line %d\n", line_number);
// Now, free the mallocated pointers
if(mempool_1 != NULL) free(mempool_1);
if(mempool_4 != NULL) free(mempool_4);
if(mempool_absdata != NULL) free(mempool_absdata);
if(mempool_extdata != NULL) free(mempool_extdata);
for(i=0; i<sections_number; i++) if(sections[i] != NULL) free(sections[i]);
if(infile != NULL) fclose(infile);
return error;
}
// This static array is requiered by parseOpcodeLine function to store a label argument
// (I considerate there is no opcode with 2 label argument)
static char g_pol_labelname[40];
int parseOpcodeLine(const char *line, struct ParsedOpcode *opstruct) {
int i=0;
char tmpstr[50];
opstruct->args[0].type=ARG_TYPE_NOTHING;
opstruct->args[1].type=ARG_TYPE_NOTHING;
opstruct->args[0].data=0;
opstruct->args[1].data=0;
// Don't forget that an opcode may contain '.' or '/'
for(i=0; (isalnum(opstruct->mnemonic[i]=tolower(line[i])) || line[i]=='/' || line[i]=='.')
&& (i<sizeof(opstruct->mnemonic)-1); i++);
// If the next character isn't a space or a tab, it's an illegal character
if(line[i]!=' ' && line[i]!='\t' && line[i]!='\n') return -1;
opstruct->mnemonic[i]=0;
line = (char*)((int)line + i);
SKIP_BLANK(line);
i=0;
// Get the opcode arguments
while(line[0]!='\n' && line[0]!=0 && line[0]!='!' && line[0]!=';') {
if(i>=2) return -2;
// Don't forget that the && operator will not evaluate the right part if the left is false!
// Direct Register :
if(toupper(line[0])=='R' && isdigit(line[1]) && (IS_SEP(line[2],',')
|| (line[1]=='1' && line[2]>='0' && line[2]<='5' && IS_SEP(line[3],',')) ))
{
opstruct->args[i].type = ARG_TYPE_D_REG;
int reg;
if(isdigit(line[2])) reg = line[2]-'0' + 10;
else reg = line[1]-'0';
opstruct->args[i].data = reg;
line = (char*)((int)line + (reg<10 ? 2 : 3));
}
// Indirect Register :
else if(line[0]=='@' && toupper(line[1])=='R' && isdigit(line[2]) && (IS_SEP(line[3],',')
|| (line[2]=='1' && line[3]>='0' && line[3]<='5' && IS_SEP(line[4],',')) ))
{
opstruct->args[i].type = ARG_TYPE_I_REG;
int reg;
if(isdigit(line[3])) reg = line[3]-'0' + 10;
else reg = line[2]-'0';
opstruct->args[i].data = reg;
line = (char*)((int)line + (reg<10 ? 3 : 4));
}
// Indirect Increment Register :
else if(line[0]=='@' && toupper(line[1])=='R' && isdigit(line[2]) && ((line[3]=='+' && IS_SEP(line[4],','))
|| (line[2]=='1' && line[3]>='0' && line[3]<='5' && line[4]=='+' && IS_SEP(line[5],',')) ))
{
opstruct->args[i].type = ARG_TYPE_INC_REG;
int reg;
if(isdigit(line[3])) reg = line[3]-'0' + 10;
else reg = line[2]-'0';
opstruct->args[i].data = reg;
line = (char*)((int)line + (reg<10 ? 4 : 5));
}
// Indirect Decrement Register :
else if(line[0]=='@' && line[1]=='-' && toupper(line[2])=='R' && isdigit(line[3]) && (IS_SEP(line[4],',')
|| (line[3]=='1' && line[4]>='0' && line[4]<='5' && IS_SEP(line[5],',')) ))
{
opstruct->args[i].type = ARG_TYPE_DEC_REG;
int reg;
if(isdigit(line[4])) reg = line[4]-'0' + 10;
else reg = line[3]-'0';
opstruct->args[i].data = reg;
line = (char*)((int)line + (reg<10 ? 4 : 5));
}
else if(line[0]=='@' && line[1]=='(' ) {
if(toupper(line[2])=='R' && line[3]=='0' && line[4]==',') {
// Indirect Indexed GBR :
if(toupper(line[5])=='G' && toupper(line[6])=='B' && toupper(line[7])=='R' && line[8]==')'
&& IS_SEP(line[9], ',') )
{
opstruct->args[i].type = ARG_TYPE_INDEX_GBR;
line = (char*)((int)line + 9);
}
// Indirect Indexed Register :
else if(toupper(line[5])=='R' && isdigit(line[6]) && (line[7]==')' && IS_SEP(line[8],',')
|| (line[6]=='1' && line[7]>='0' && line[7]<='5' && line[8]==')' && IS_SEP(line[9],',')) ))
{
opstruct->args[i].type = ARG_TYPE_INDEX_REG;
int reg;
if(isdigit(line[7])) reg = line[7]-'0' + 10;
else reg = line[6]-'0';
opstruct->args[i].data = reg;
line = (char*)((int)line + (reg<10 ? 8 : 9));
}
else return -3;
}
else {
int j, ret, val;
for(j=0; j<50 && !IS_SEP(line[j+2], ','); j++) tmpstr[j]=line[j+2];
if(j>=50) return -3;
tmpstr[j]=0;
ret = stringToNumber(tmpstr, &val);
if(ret == -2) return -4;
else if(ret < 0) return -3;
char *line2 = (char*)((int)line + 2+j);
// Indirect PC Displacement
if(line2[0]==',' && toupper(line2[1])=='P' && toupper(line2[2])=='C' && line2[3]==')' && IS_SEP(line2[4], ',')) {
opstruct->args[i].type = ARG_TYPE_DISP_PC;
opstruct->args[i].data = val;
line = (char*)((int)line2 + 4);
}
// Indirect PC Displacement
else if(line2[0]==',' && toupper(line2[1])=='G' && toupper(line2[2])=='B' && toupper(line2[3])=='R'
&& line2[4]==')' && IS_SEP(line2[5], ','))
{
opstruct->args[i].type = ARG_TYPE_DISP_GBR;
opstruct->args[i].data = val;
line = (char*)((int)line2 + 5);
}
// Indirect Register Displacement
else if(line2[0]==',' && toupper(line2[1])=='R' && isdigit(line2[2]) && ((line2[3]==')' && IS_SEP(line2[4],','))
|| (line2[2]=='1' && line2[3]>='0' && line2[3]<='5' && line2[4]==')' && IS_SEP(line2[5],',')) ))
{
opstruct->args[i].type = ARG_TYPE_DISP_REG;
int reg;
if(isdigit(line2[3])) reg = line2[3]-'0' + 10;
else reg = line2[2]-'0';
// WARNING : in this case (and ONLY this), data contain 2 informations : the 4 LSB
// contain the register ID, and the 28 MSB are the disp value << 4.
// For now, there is no error/warning when the value overflow
opstruct->args[i].data = reg | (val<<4);
line = (char*)((int)line2 + (reg<10 ? 4 : 5));
}
else return -3;