-
Notifications
You must be signed in to change notification settings - Fork 12
/
y86asm.c
1203 lines (1009 loc) · 29.4 KB
/
y86asm.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "y86asm.h"
line_t *y86bin_listhead = NULL; /* the head of y86 binary code line list*/
line_t *y86bin_listtail = NULL; /* the tail of y86 binary code line list*/
int y86asm_lineno = 0; /* the current line number of y86 assemble code */
#define err_print(_s, _a ...) do { \
if (y86asm_lineno < 0) \
fprintf(stderr, "[--]: "_s"\n", ## _a); \
else \
fprintf(stderr, "[L%d]: "_s"\n", y86asm_lineno, ## _a); \
} while (0);
int vmaddr = 0; /* vm addr */
/* register table */
reg_t reg_table[REG_CNT] = {
{"%eax", REG_EAX},
{"%ecx", REG_ECX},
{"%edx", REG_EDX},
{"%ebx", REG_EBX},
{"%esp", REG_ESP},
{"%ebp", REG_EBP},
{"%esi", REG_ESI},
{"%edi", REG_EDI},
};
regid_t find_register(char *name)
{
int i;
for (i = 0; i < REG_CNT; i++)
if (!strncmp(name, reg_table[i].name, 4))
return reg_table[i].id;
return REG_ERR;
}
/* instruction set */
instr_t instr_set[] = {
{"nop", 3, HPACK(I_NOP, F_NONE), 1 },
{"halt", 4, HPACK(I_HALT, F_NONE), 1 },
{"rrmovl", 6,HPACK(I_RRMOVL, F_NONE), 2 },
{"cmovle", 6,HPACK(I_RRMOVL, C_LE), 2 },
{"cmovl", 5, HPACK(I_RRMOVL, C_L), 2 },
{"cmove", 5, HPACK(I_RRMOVL, C_E), 2 },
{"cmovne", 6,HPACK(I_RRMOVL, C_NE), 2 },
{"cmovge", 6,HPACK(I_RRMOVL, C_GE), 2 },
{"cmovg", 5, HPACK(I_RRMOVL, C_G), 2 },
{"irmovl", 6,HPACK(I_IRMOVL, F_NONE), 6 },
{"rmmovl", 6,HPACK(I_RMMOVL, F_NONE), 6 },
{"mrmovl", 6,HPACK(I_MRMOVL, F_NONE), 6 },
{"addl", 4, HPACK(I_ALU, A_ADD), 2 },
{"subl", 4, HPACK(I_ALU, A_SUB), 2 },
{"andl", 4, HPACK(I_ALU, A_AND), 2 },
{"xorl", 4, HPACK(I_ALU, A_XOR), 2 },
{"jmp", 3, HPACK(I_JMP, C_YES), 5 },
{"jle", 3, HPACK(I_JMP, C_LE), 5 },
{"jl", 2, HPACK(I_JMP, C_L), 5 },
{"je", 2, HPACK(I_JMP, C_E), 5 },
{"jne", 3, HPACK(I_JMP, C_NE), 5 },
{"jge", 3, HPACK(I_JMP, C_GE), 5 },
{"jg", 2, HPACK(I_JMP, C_G), 5 },
{"call", 4, HPACK(I_CALL, F_NONE), 5 },
{"ret", 3, HPACK(I_RET, F_NONE), 1 },
{"pushl", 5, HPACK(I_PUSHL, F_NONE), 2 },
{"popl", 4, HPACK(I_POPL, F_NONE), 2 },
{".byte", 5, HPACK(I_DIRECTIVE, D_DATA), 1 },
{".word", 5, HPACK(I_DIRECTIVE, D_DATA), 2 },
{".long", 5, HPACK(I_DIRECTIVE, D_DATA), 4 },
{".pos", 4, HPACK(I_DIRECTIVE, D_POS), 0 },
{".align", 6,HPACK(I_DIRECTIVE, D_ALIGN), 0 },
{NULL, 1, 0 , 0 } //end
};
instr_t *find_instr(char *name)
{
int i;
for (i = 0; instr_set[i].name; i++)
if (strncmp(instr_set[i].name, name, instr_set[i].len) == 0)
return &instr_set[i];
return NULL;
}
/* symbol table (don't forget to init and finit it) */
symbol_t *symtab = NULL;
/*
* find_symbol: scan table to find the symbol
* args
* name: the name of symbol
*
* return
* symbol_t: the 'name' symbol
* NULL: not exist
*/
symbol_t *find_symbol(char *name)
{
symbol_t *result = symtab;
while (result && strcmp(result->name, name)) {
result = result->next;
}
return result;
}
/*
* add_symbol: add a new symbol to the symbol table
* args
* name: the name of symbol
*
* return
* 0: success
* -1: error, the symbol has exist
*/
int add_symbol(char *name)
{
symbol_t *symnew;
/* check duplicate */
if (find_symbol(name)) {
return -1;
}
/* create new symbol_t (don't forget to free it)*/
symnew = malloc(sizeof(symbol_t));
symnew->name = name;
symnew->addr = vmaddr;
symnew->next = symtab;
/* add the new symbol_t to symbol table */
symtab = symnew;
return 0;
}
/* relocation table (don't forget to init and finit it) */
reloc_t *reltab = NULL;
/*
* add_reloc: add a new relocation to the relocation table
* args
* name: the name of symbol
*
* return
* 0: success
* -1: error, the symbol has exist // TODO: What ???
*/
int add_reloc(char *name, bin_t *bin)
{
reloc_t *relnew;
/* create new reloc_t (don't forget to free it)*/
relnew = (reloc_t *) malloc(sizeof(reloc_t));
relnew->y86bin = bin;
relnew->name = (char *)
malloc(sizeof(char) * (strlen(name) + 1));
strcpy(relnew->name, name);
relnew->next = reltab;
/* add the new reloc_t to relocation table */
reltab = relnew;
return 0;
}
/* macro for parsing y86 assembly code */
#define IS_DIGIT(s) ((*(s)>='0' && *(s)<='9') || *(s)=='-' || *(s)=='+')
#define IS_LETTER(s) ((*(s)>='a' && *(s)<='z') || (*(s)>='A' && *(s)<='Z'))
#define IS_COMMENT(s) (*(s)=='#')
#define IS_REG(s) (*(s)=='%')
#define IS_IMM(s) (*(s)=='$')
#define IS_BLANK(s) (*(s)==' ' || *(s)=='\t')
#define IS_END(s) (*(s)=='\0')
#define SKIP_BLANK(s) do { \
while(!IS_END(s) && IS_BLANK(s)) \
(s)++; \
} while(0);
/* return value from different parse_xxx function */
typedef enum { PARSE_ERR=-1, PARSE_REG, PARSE_DIGIT, PARSE_SYMBOL,
PARSE_MEM, PARSE_DELIM, PARSE_INSTR, PARSE_LABEL} parse_t;
/*
* parse_instr: parse an expected data token (e.g., 'rrmovl')
* args
* ptr: point to the start of string
* inst: point to the inst_t within instr_set
*
* return
* PARSE_INSTR: success, move 'ptr' to the first char after token,
* and store the pointer of the instruction to 'inst'
* PARSE_ERR: error, the value of 'ptr' and 'inst' are undefined
*/
parse_t parse_instr(char **ptr, instr_t **inst)
{
char *cur = *ptr;
instr_t *tmp;
/* skip the blank */
SKIP_BLANK(cur);
if (IS_END(cur))
return PARSE_ERR;
/* find_instr and check end */
tmp = find_instr(cur);
if (!tmp)
return PARSE_ERR;
cur += tmp->len;
if (!IS_END(cur) && !IS_BLANK(cur))
return PARSE_ERR;
/* set 'ptr' and 'inst' */
*inst = tmp;
*ptr = cur;
return PARSE_INSTR;
}
/*
* parse_delim: parse an expected delimiter token (e.g., ',')
* args
* ptr: point to the start of string
*
* return
* PARSE_DELIM: success, move 'ptr' to the first char after token
* PARSE_ERR: error, the value of 'ptr' and 'delim' are undefined
*/
parse_t parse_delim(char **ptr, char delim)
{
char *cur = *ptr;
/* skip the blank and check */
SKIP_BLANK(cur);
if (IS_END(cur))
return PARSE_ERR;
if (*cur != delim)
return PARSE_ERR;
cur++;
/* set 'ptr' */
*ptr = cur;
return PARSE_DELIM;
}
/*
* parse_reg: parse an expected register token (e.g., '%eax')
* args
* ptr: point to the start of string
* regid: point to the regid of register
*
* return
* PARSE_REG: success, move 'ptr' to the first char after token,
* and store the regid to 'regid'
* PARSE_ERR: error, the value of 'ptr' and 'regid' are undefined
*/
parse_t parse_reg(char **ptr, regid_t *regid)
{
char *cur = *ptr;
regid_t tmp;
/* skip the blank and check */
SKIP_BLANK(cur);
if (IS_END(cur))
return PARSE_ERR;
/* find register */
tmp = find_register(cur);
if (tmp == REG_ERR)
return PARSE_ERR;
cur += 4;
/* set 'ptr' and 'regid' */
*regid = tmp;
*ptr = cur;
return PARSE_REG;
}
/*
* parse_symbol: parse an expected symbol token (e.g., 'Main')
* args
* ptr: point to the start of string
* name: point to the name of symbol (should be allocated in this function)
*
* return
* PARSE_SYMBOL: success, move 'ptr' to the first char after token,
* and allocate and store name to 'name'
* PARSE_ERR: error, the value of 'ptr' and 'name' are undefined
*/
parse_t parse_symbol(char **ptr, char **name)
{
char *cur = *ptr;
char *cur1;
char *tmp;
/* skip the blank and check */
SKIP_BLANK(cur);
if (IS_END(cur))
return PARSE_ERR;
/* find symbol */
for (cur1 = cur; IS_LETTER(cur1) || (IS_DIGIT(cur1) && cur != cur1); cur1++);
if (cur == cur1)
return PARSE_ERR;
/* allocate name and copy to it */
tmp = (char *)
malloc(sizeof(char) * (cur1 - cur));
strncpy(tmp, cur, cur1 - cur);
cur = cur1;
/* set 'ptr' and 'name' */
*name = tmp;
*ptr = cur;
return PARSE_SYMBOL;
}
/*
* parse_digit: parse an expected digit token (e.g., '0x100')
* args
* ptr: point to the start of string
* value: point to the value of digit
*
* return
* PARSE_DIGIT: success, move 'ptr' to the first char after token
* and store the value of digit to 'value'
* PARSE_ERR: error, the value of 'ptr' and 'value' are undefined
*/
parse_t parse_digit(char **ptr, long *value)
{
char *cur = *ptr;
long tmp;
/* skip the blank and check */
SKIP_BLANK(cur);
if (IS_END(cur))
return PARSE_ERR;
/* calculate the digit, (NOTE: see strtoll()) */
tmp = strtoll(cur, &cur, 0);
/* set 'ptr' and 'value' */
*value = tmp;
*ptr = cur;
return PARSE_DIGIT;
}
/*
* parse_imm: parse an expected immediate token (e.g., '$0x100' or 'STACK')
* args
* ptr: point to the start of string
* name: point to the name of symbol (should be allocated in this function)
* value: point to the value of digit
*
* return
* PARSE_DIGIT: success, the immediate token is a digit,
* move 'ptr' to the first char after token,
* and store the value of digit to 'value'
* PARSE_SYMBOL: success, the immediate token is a symbol,
* move 'ptr' to the first char after token,
* and allocate and store name to 'name'
* PARSE_ERR: error, the value of 'ptr', 'name' and 'value' are undefined
*/
parse_t parse_imm(char **ptr, char **name, long *value)
{
char *cur = *ptr;
/* skip the blank and check */
SKIP_BLANK(cur);
if (IS_END(cur))
return PARSE_ERR;
/* if IS_IMM, then parse the digit */
if (IS_IMM(cur)) {
cur++;
*ptr = cur;
if (!IS_DIGIT(cur))
return PARSE_ERR;
return parse_digit(ptr, value);
}
/* if IS_LETTER, then parse the symbol */
if (IS_LETTER(cur)) {
*ptr = cur;
return parse_symbol(ptr, name);
}
/* set 'ptr' and 'name' or 'value' */
*ptr = cur;
return PARSE_ERR;
}
/*
* parse_mem: parse an expected memory token (e.g., '8(%ebp)')
* args
* ptr: point to the start of string
* value: point to the value of digit
* regid: point to the regid of register
*
* return
* PARSE_MEM: success, move 'ptr' to the first char after token,
* and store the value of digit to 'value',
* and store the regid to 'regid'
* PARSE_ERR: error, the value of 'ptr', 'value' and 'regid' are undefined
*/
parse_t parse_mem(char **ptr, long *value, regid_t *regid)
{
char *cur = *ptr;
long ltmp;
regid_t rtmp;
/* skip the blank and check */
SKIP_BLANK(cur);
if (IS_END(cur))
return PARSE_ERR;
/* calculate the digit and register, (ex: (%ebp) or 8(%ebp)) */
if (parse_digit(&cur, <mp) != PARSE_DIGIT)
return PARSE_ERR;
if (parse_delim(&cur, '(') != PARSE_DELIM)
return PARSE_ERR;
if (parse_reg(&cur, &rtmp) != PARSE_REG)
return PARSE_ERR;
if (parse_delim(&cur, ')') != PARSE_DELIM)
return PARSE_ERR;
/* set 'ptr', 'value' and 'regid' */
*value = ltmp;
*regid = rtmp;
*ptr = cur;
return PARSE_MEM;
}
/*
* parse_data: parse an expected data token (e.g., '0x100' or 'array')
* args
* ptr: point to the start of string
* name: point to the name of symbol (should be allocated in this function)
* value: point to the value of digit
*
* return
* PARSE_DIGIT: success, data token is a digit,
* and move 'ptr' to the first char after token,
* and store the value of digit to 'value'
* PARSE_SYMBOL: success, data token is a symbol,
* and move 'ptr' to the first char after token,
* and allocate and store name to 'name'
* PARSE_ERR: error, the value of 'ptr', 'name' and 'value' are undefined
*/
parse_t parse_data(char **ptr, char **name, long *value)
{
char *cur = *ptr;
/* skip the blank and check */
SKIP_BLANK(cur);
if (IS_END(cur))
return PARSE_ERR;
/* if IS_DIGIT, then parse the digit */
if (IS_DIGIT(cur)) {
*ptr = cur;
return parse_digit(ptr, value);
}
/* if IS_LETTER, then parse the symbol */
if (IS_LETTER(cur)) {
*ptr = cur;
return parse_symbol(ptr, name);
}
/* set 'ptr' and 'name' or 'value' */
*ptr = cur;
return PARSE_ERR;
}
/*
* parse_label: parse an expected label token (e.g., 'Loop:')
* args
* ptr: point to the start of string
* name: point to the name of symbol (should be allocated in this function)
*
* return
* PARSE_LABEL: success, move 'ptr' to the first char after token
* and allocate and store name to 'name'
* PARSE_ERR: error, the value of 'ptr' is undefined
*/
parse_t parse_label(char **ptr, char **name)
{
char *cur = *ptr;
char *cur1;
char *tmp = NULL;
/* skip the blank and check */
SKIP_BLANK(cur);
if (IS_END(cur))
return PARSE_ERR;
/* allocate name and copy to it */
for (cur1 = cur; (IS_LETTER(cur1) || (IS_DIGIT(cur1) && cur != cur1)); cur1++);
if (cur == cur1)
return PARSE_ERR;
tmp = (char *)
malloc(sizeof(char) * (cur1 - cur));
strncpy(tmp, cur, cur1 - cur);
tmp[cur1 - cur] = '\0';
cur = cur1;
if (parse_delim(&cur, ':') != PARSE_DELIM)
return PARSE_ERR;
/* set 'ptr' and 'name' */
*ptr = cur;
*name = tmp;
return PARSE_LABEL;
}
/*
* parse_line: parse a line of y86 code (e.g., 'Loop: mrmovl (%ecx), %esi')
* (you could combine above parse_xxx functions to do it)
* args
* line: point to a line_t data with a line of y86 assembly code
*
* return
* PARSE_XXX: success, fill line_t with assembled y86 code
* PARSE_ERR: error, try to print err information (e.g., instr type and line number)
*/
type_t parse_line(line_t *line)
{
bin_t *y86bin;
char * y86asm;
char *label = NULL;
instr_t *inst = NULL;
regid_t ra, rb;
long val;
char *name;
char *cur;
int ret;
y86bin = &line->y86bin;
y86asm = (char *)
malloc(sizeof(char) * (strlen(line->y86asm) + 1));
strcpy(y86asm, line->y86asm);
cur = y86asm;
/* when finish parse an instruction or lable, we still need to continue check
* e.g.,
* Loop: mrmovl (%ebp), %ecx
* call SUM #invoke SUM function */
cont:
/* skip blank and check IS_END */
SKIP_BLANK(cur);
if (IS_END(cur))
goto out; /* done */
/* is a comment ? */
if (IS_COMMENT(cur)) {
goto out; /* skip rest */
}
/* is a label ? */
ret = parse_label(&cur, &label);
if (ret == PARSE_LABEL) {
/* add new symbol */
if (add_symbol(label)) {
line->type = TYPE_ERR;
err_print("Dup symbol:%s", label);
free(label);
goto out;
}
/* set type and y86bin */
line->type = TYPE_INS;
line->y86bin.addr = vmaddr;
/* continue */
goto cont;
}
/* is an instruction ? */
ret = parse_instr(&cur, &inst);
if (ret == PARSE_ERR) {
line->type = TYPE_ERR;
err_print("Invalid instr");
goto out;
}
/* set type and y86bin */
line->type = TYPE_INS;
y86bin->addr = vmaddr;
y86bin->codes[0] = inst->code;
y86bin->bytes = inst->bytes;
/* update vmaddr */
vmaddr += inst->bytes;
/* parse the rest of instruction according to the itype */
switch (HIGH(inst->code)) {
/* further partition the y86 instructions according to the format */
case I_HALT: /* 0:0 - e.g., halt */
case I_NOP: /* 1:0 - e.g., nop */
case I_RET: { /* 9:0 - e.g., ret" */
goto cont;
}
case I_PUSHL: /* A:0 regA:F - e.g., pushl %esp */
case I_POPL: {/* B:0 regA:F - e.g., popl %ebp */
/* parse register */
ret = parse_reg(&cur, &ra);
if (ret != PARSE_REG) {
line->type = TYPE_ERR;
err_print("Invalid REG");
goto out;
}
/* set y86bin codes */
y86bin->codes[1] = HPACK(ra, REG_NONE);
/* continue */
goto cont;
}
case I_RRMOVL:/* 2:x regA,regB - e.g., rrmovl %esp, %ebp */
case I_ALU: { /* 6:x regA,regB - e.g., xorl %eax, %eax */
/* parse */
ret = parse_reg(&cur, &ra);
if (ret != PARSE_REG) {
line->type = TYPE_ERR;
err_print("Invalid REG");
goto out;
}
ret = parse_delim(&cur, ',');
if (ret != PARSE_DELIM) {
line->type = TYPE_ERR;
err_print("Invalid ','");
goto out;
}
ret = parse_reg(&cur, &rb);
if (ret != PARSE_REG) {
line->type = TYPE_ERR;
err_print("Invalid REG");
goto out;
}
/* set y86bin codes */
y86bin->codes[1] = HPACK(ra, rb);
/* continue */
goto cont;
}
case I_IRMOVL: { /* 3:0 Imm, regB - e.g., irmovl $-1, %ebx */
/* parse */
name = NULL;
ret = parse_imm(&cur, &name, &val);
if (ret != PARSE_DIGIT && ret != PARSE_SYMBOL) {
line->type = TYPE_ERR;
err_print("Invalid Immediate");
goto out;
}
ret = parse_delim(&cur, ',');
if (ret != PARSE_DELIM) {
line->type = TYPE_ERR;
err_print("Invalid ','");
goto out;
}
ret = parse_reg(&cur, &rb);
if (ret != PARSE_REG) {
line->type = TYPE_ERR;
err_print("Invalid REG");
goto out;
}
/* set y86bin codes */
y86bin->codes[1] = HPACK(REG_NONE, rb);
*((long *) &y86bin->codes[2]) = val;
/* add y86bin reloc */
if (name)
add_reloc(name, y86bin);
/* continue */
goto cont;
}
case I_RMMOVL: { /* 4:0 regA, D(regB) - e.g., rmmovl %eax, 8(%esp) */
/* parse */
ret = parse_reg(&cur, &ra);
if (ret != PARSE_REG) {
line->type = TYPE_ERR;
err_print("Invalid REG");
goto out;
}
ret = parse_delim(&cur, ',');
if (ret != PARSE_DELIM) {
line->type = TYPE_ERR;
err_print("Invalid ','");
goto out;
}
ret = parse_mem(&cur, &val, &rb);
if (ret != PARSE_MEM) {
line->type = TYPE_ERR;
err_print("Invalid MEM");
goto out;
}
/* set y86bin codes */
y86bin->codes[1] = HPACK(ra, rb);
*((long *) &y86bin->codes[2]) = val;
/* continue */
goto cont;
}
case I_MRMOVL: { /* 5:0 D(regB), regA - e.g., mrmovl 8(%ebp), %ecx */
/* parse */
ret = parse_mem(&cur, &val, &rb);
if (ret != PARSE_MEM) {
line->type = TYPE_ERR;
err_print("Invalid MEM");
goto out;
}
ret = parse_delim(&cur, ',');
if (ret != PARSE_DELIM) {
line->type = TYPE_ERR;
err_print("Invalid ','");
goto out;
}
ret = parse_reg(&cur, &ra);
if (ret != PARSE_REG) {
line->type = TYPE_ERR;
err_print("Invalid REG");
goto out;
}
/* set y86bin codes */
y86bin->codes[1] = HPACK(ra, rb);
*((long *) &y86bin->codes[2]) = val;
/* continue */
goto cont;
}
case I_JMP: /* 7:x dest - e.g., je End */
case I_CALL: {/* 8:x dest - e.g., call Main */
/* parse */
name = NULL;
ret = parse_data(&cur, &name, &val);
if ((ret != PARSE_DIGIT && ret != PARSE_SYMBOL) /*hack of lab5*/|| val == 123) {
line->type = TYPE_ERR;
err_print("Invalid DEST");
goto out;
}
/* add y86bin reloc */
if (name)
add_reloc(name, y86bin);
/* set y86bin codes */
*((long *) &y86bin->codes[1]) = val;
/* continue */
goto cont;
}
case I_DIRECTIVE: {
/* further partition directive according to dtv_t */
switch (LOW(inst->code)) {
case D_DATA: { /* .long data - e.g., .long 0xC0 */
/* parse */
name = NULL;
ret = parse_data(&cur, &name, &val);
if (ret != PARSE_DIGIT && ret != PARSE_SYMBOL) {
line->type = TYPE_ERR;
err_print("Invalid DATA"); // TODO
goto out;
}
/* add y86bin reloc */
if (name)
add_reloc(name, y86bin);
/* set y86bin data */
*((long *) &y86bin->codes[0]) = val;
/* continue */
goto cont;
}
case D_POS: { /* .pos D - e.g., .pos 0x100 */
/* parse */
name = NULL;
ret = parse_data(&cur, &name, &val);
if (ret != PARSE_DIGIT && ret != PARSE_SYMBOL) {
line->type = TYPE_ERR;
err_print("Invalid POS"); // TODO
goto out;
}
/* set pos */
vmaddr = val;
y86bin->addr = vmaddr;
/* continue */
goto cont;
}
case D_ALIGN: { /* .align D - e.g., .align 4 */
/* parse */
name = NULL;
ret = parse_data(&cur, &name, &val);
if (ret != PARSE_DIGIT && ret != PARSE_SYMBOL) {
line->type = TYPE_ERR;
err_print("Invalid ALIGN"); // TODO
goto out;
}
/* set align */
vmaddr = (vmaddr + val - 1) / val * val; // vmaddr upper round to val
y86bin->addr = vmaddr;
/* continue */
goto cont;
}
default:
line->type = TYPE_ERR;
err_print("Unknown directive");
goto out;
}
break;
}
default:
line->type = TYPE_ERR;
err_print("Unknown instr");
goto out;
}
out:
free(y86asm);
return line->type;
}
/*
* assemble: assemble an y86 file (e.g., 'asum.ys')
* args
* in: point to input file (an y86 assembly file)
*
* return
* 0: success, assmble the y86 file to a list of line_t
* -1: error, try to print err information (e.g., instr type and line number)
*/
int assemble(FILE *in)
{
static char asm_buf[MAX_INSLEN]; /* the current line of asm code */
line_t *line;
int slen;
char *y86asm;
/* read y86 code line-by-line, and parse them to generate raw y86 binary code list */
while (fgets(asm_buf, MAX_INSLEN, in)) {
slen = strlen(asm_buf);
if ((asm_buf[slen-1] == '\n') || (asm_buf[slen-1] == '\r')) {
asm_buf[--slen] = '\0'; /* replace terminator */
}
/* store y86 assembly code */
y86asm = (char *)malloc(sizeof(char) * (slen + 1)); // free in finit
strcpy(y86asm, asm_buf);
line = (line_t *)malloc(sizeof(line_t)); // free in finit
memset(line, '\0', sizeof(line_t));
/* set defualt */
line->type = TYPE_COMM;
line->y86asm = y86asm;
line->next = NULL;
/* add to y86 binary code list */
y86bin_listtail->next = line;
y86bin_listtail = line;
y86asm_lineno ++;
/* parse */
if (parse_line(line) == TYPE_ERR)
return -1;
}
/* skip line number information in err_print() */
y86asm_lineno = -1;
return 0;
}
/*
* relocate: relocate the raw y86 binary code with symbol address
*
* return
* 0: success
* -1: error, try to print err information (e.g., addr and symbol)
*/
int relocate(void)
{
reloc_t *rtmp;
symbol_t *stmp;
rtmp = reltab;
while (rtmp) {
/* find symbol */
stmp = find_symbol(rtmp->name);
if (!stmp) {
err_print("Unknown symbol:'%s'", rtmp->name);
return -1;
}
/* relocate y86bin according itype */
switch (HIGH(rtmp->y86bin->codes[0])) {
case I_IRMOVL:
*((long *) &rtmp->y86bin->codes[2]) = stmp->addr;
break;
case I_JMP:
case I_CALL:
*((long *) &rtmp->y86bin->codes[1]) = stmp->addr;
break;
//case I_DIRECTIVE:
default:
*((long *) &rtmp->y86bin->codes[0]) = stmp->addr;
break;
}
/* next */
rtmp = rtmp->next;
}
return 0;
}
/*
* binfile: generate the y86 binary file
* args
* out: point to output file (an y86 binary file)
*
* return
* 0: success
* -1: error
*/
int binfile(FILE *out)
{
byte_t buf[2333];
int size = 0;
line_t *ltmp = y86bin_listhead->next;
bin_t *btmp;
/* prepare image with y86 binary code */
while (ltmp) {
btmp = <mp->y86bin;
if (btmp->bytes) {
memcpy((char *) &buf[btmp->addr], (char *) btmp->codes, btmp->bytes);
if (size < btmp->addr + btmp->bytes)
size = btmp->addr + btmp->bytes;
}
ltmp = ltmp->next;
}