-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathy64asm.c
executable file
·1143 lines (1036 loc) · 30.7 KB
/
y64asm.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 <stdint.h>
#include "y64asm.h"
line_t *line_head = NULL;
line_t *line_tail = NULL;
int lineno = 0;
#define err_print(_s, _a ...) do { \
if (lineno < 0) \
fprintf(stderr, "[--]: "_s"\n", ## _a); \
else \
fprintf(stderr, "[L%d]: "_s"\n", lineno, ## _a); \
} while (0);
int64_t vmaddr = 0; /* vm addr */
//dec to hex
static void hexstuff(char *dest, int value, int len)
{
int i;
for (i = 0; i < len; i++) {
char c;
int h = (value >> 4*i) & 0xF;
c = h < 10 ? h + '0' : h - 10 + 'a';
dest[len-i-1] = c;
}
}
/* register table */
const reg_t reg_table[REG_NONE] = {
{"%rax", REG_RAX, 4},
{"%rcx", REG_RCX, 4},
{"%rdx", REG_RDX, 4},
{"%rbx", REG_RBX, 4},
{"%rsp", REG_RSP, 4},
{"%rbp", REG_RBP, 4},
{"%rsi", REG_RSI, 4},
{"%rdi", REG_RDI, 4},
{"%r8", REG_R8, 3},
{"%r9", REG_R9, 3},
{"%r10", REG_R10, 4},
{"%r11", REG_R11, 4},
{"%r12", REG_R12, 4},
{"%r13", REG_R13, 4},
{"%r14", REG_R14, 4}
};
const reg_t* find_register(char *name)
{
int i;
for (i = 0; i < REG_NONE; i++)
if (!strncmp(name, reg_table[i].name, reg_table[i].namelen))
return ®_table[i];
return NULL;
}
/* instruction set */
instr_t instr_set[] = {
{"nop", 3, HPACK(I_NOP, F_NONE), 1 },
{"halt", 4, HPACK(I_HALT, F_NONE), 1 },
{"rrmovq", 6,HPACK(I_RRMOVQ, F_NONE), 2 },
{"cmovle", 6,HPACK(I_RRMOVQ, C_LE), 2 },
{"cmovl", 5, HPACK(I_RRMOVQ, C_L), 2 },
{"cmove", 5, HPACK(I_RRMOVQ, C_E), 2 },
{"cmovne", 6,HPACK(I_RRMOVQ, C_NE), 2 },
{"cmovge", 6,HPACK(I_RRMOVQ, C_GE), 2 },
{"cmovg", 5, HPACK(I_RRMOVQ, C_G), 2 },
{"irmovq", 6,HPACK(I_IRMOVQ, F_NONE), 10 },
{"rmmovq", 6,HPACK(I_RMMOVQ, F_NONE), 10 },
{"mrmovq", 6,HPACK(I_MRMOVQ, F_NONE), 10 },
{"addq", 4, HPACK(I_ALU, A_ADD), 2 },
{"subq", 4, HPACK(I_ALU, A_SUB), 2 },
{"andq", 4, HPACK(I_ALU, A_AND), 2 },
{"xorq", 4, HPACK(I_ALU, A_XOR), 2 },
{"jmp", 3, HPACK(I_JMP, C_YES), 9 },
{"jle", 3, HPACK(I_JMP, C_LE), 9 },
{"jl", 2, HPACK(I_JMP, C_L), 9 },
{"je", 2, HPACK(I_JMP, C_E), 9 },
{"jne", 3, HPACK(I_JMP, C_NE), 9 },
{"jge", 3, HPACK(I_JMP, C_GE), 9 },
{"jg", 2, HPACK(I_JMP, C_G), 9 },
{"call", 4, HPACK(I_CALL, F_NONE), 9 },
{"ret", 3, HPACK(I_RET, F_NONE), 1 },
{"pushq", 5, HPACK(I_PUSHQ, F_NONE), 2 },
{"popq", 4, HPACK(I_POPQ, 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 },
{".quad", 5, HPACK(I_DIRECTIVE, D_DATA), 8 },
{".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)
{
if(!symtab) return NULL;
symbol_t* cur = symtab;
while(cur && cur->next){
if(strcmp(cur->name, name) == 0){
return cur;
}else{
cur = cur->next;
}
}
return NULL;
}
/*
* 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)
{
/* check duplicate */
if(find_symbol(name)){
err_print("Dup symbol:%s", name);
return -1;
}
/* create new symbol_t (don't forget to free it)*/
symbol_t* new_symbol = malloc(sizeof(symbol_t));
new_symbol->name = name;
new_symbol->next = symtab;
new_symbol->addr = vmaddr;
//err_print("add symbol name = %s, addr = %x", new_symbol->name, new_symbol->addr);
symtab = new_symbol;
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
*/
void add_reloc(char *name, bin_t *bin)
{
/* create new reloc_t (don't forget to free it)*/
reloc_t* reloc = malloc(sizeof(reloc_t));
reloc->name = name;
reloc->y64bin = bin;
reloc->next = reltab;
reltab = reloc;
return;
}
/* macro for parsing y64 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., 'rrmovq')
* 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)
{
/* skip the blank */
SKIP_BLANK(*(ptr));
/* find_instr and check end */
instr_t* cur_instr = find_instr(*(ptr));
/* set 'ptr' and 'inst' */
if(cur_instr){
*(ptr) += cur_instr->len;
*(inst) = cur_instr;
return PARSE_INSTR;
}
return PARSE_ERR;
}
/*
* 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)
{
/* skip the blank and check */
SKIP_BLANK(*(ptr));
if(IS_END(*(ptr))) return PARSE_ERR;
char to_check = *(*(ptr));
if(to_check != delim) return PARSE_ERR;
/* set 'ptr' */
*(ptr) += 1;
return PARSE_DELIM;
}
/*
* parse_reg: parse an expected register token (e.g., '%rax')
* 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)
{
/* skip the blank and check */
SKIP_BLANK(*(ptr));
if(IS_END(*(ptr))) return PARSE_ERR;
/* find register */
reg_t* reg = find_register(*(ptr));
if(!reg){
err_print("Invalid REG");
return PARSE_ERR;
}
/* set 'ptr' and 'regid' */
*(ptr) += reg->namelen;
*regid = reg->id;
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)
{
SKIP_BLANK(*(ptr));
if(IS_END(*(ptr))) return PARSE_ERR;
/* allocate name and copy to it */
int str_p = 0;
char* symbol_name = NULL;
for(str_p = 0; str_p < strlen(*(ptr)); ++str_p){
char ch = *(*(ptr) + str_p);
if(ch == ',' || IS_BLANK(*ptr + str_p)){
// char symbol_name[str_p];
// for(int i = 0; i < str_p; ++i){
// symbol_name[i] = *(*(ptr) + i);
// }
*(name) = malloc(str_p + 1);
strncpy(*name, *ptr, str_p);
(*name)[str_p] = '\0';
*(ptr) += str_p;
return PARSE_SYMBOL;
}else if(str_p == strlen(*(ptr)) - 1){
*(name) = *(ptr);
*(ptr) += str_p;
return PARSE_SYMBOL;
}
}
/* set 'ptr' and 'name' */
return PARSE_ERR;
}
/*
* 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 long int *value)
{
/* skip the blank and check */
SKIP_BLANK(*(ptr));
if(IS_END(*(ptr))) return PARSE_ERR;
/* calculate the digit, (NOTE: see strtoll()) */
char* end_ptr;
long res = strtoull(*(ptr), &end_ptr, 0);
/* set 'ptr' and 'value' */
if(*(ptr) == end_ptr){
err_print("Invalid Immediate");
return PARSE_ERR;
}
*(ptr) = end_ptr;
*(value) = res;
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)
{
/* skip the blank and check */
SKIP_BLANK(*(ptr));
if(IS_END(*(ptr))) return PARSE_ERR;
/* if IS_IMM, then parse the digit */
if(IS_IMM(*(ptr))){
*(ptr) += 1;
return parse_digit(ptr, value);
}
/* if IS_LETTER, then parse the symbol */
if(IS_LETTER(*(ptr))){
return parse_symbol(ptr, name);
}
/* set 'ptr' and 'name' or 'value' */
return PARSE_ERR;
}
/*
* parse_mem: parse an expected memory token (e.g., '8(%rbp)')
* 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)
{
/* skip the blank and check */
SKIP_BLANK(*(ptr));
if(IS_END(*(ptr))) return PARSE_ERR;
/* calculate the digit and register, (ex: (%rbp) or 8(%rbp)) */
if(*(*(ptr)) == '('){
*value = 0;
}else{
parse_digit(ptr, value);
}
if(*(*(ptr)) != '(') return PARSE_ERR;
*(ptr) += 1;
if(parse_reg(ptr, regid) == PARSE_ERR) return PARSE_ERR;
/* set 'ptr', 'value' and 'regid' */
if(*(*(ptr)) != ')'){
err_print("Invalid MEM");
return PARSE_ERR;
}
*(ptr) += 1;
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)
{
/* skip the blank and check */
SKIP_BLANK(*ptr);
if(IS_END(*ptr)) return PARSE_ERR;
/* if IS_DIGIT, then parse the digit */
if(IS_DIGIT(*ptr)){
return parse_digit(ptr, value);
}
/* if IS_LETTER, then parse the symbol */
if(IS_LETTER(*ptr)){
return parse_symbol(ptr, name);
}
/* set 'ptr', 'name' and 'value' */
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)
{
/* skip the blank and check */
SKIP_BLANK(*(ptr));
if(IS_END(*(ptr))) return PARSE_ERR;
/* allocate name and copy to it */
int str_p = 0;
//char* symbol_name = NULL;
for(str_p = 0; str_p < strlen(*(ptr)); ++str_p){
char ch = *(*(ptr) + str_p);
if(ch == ':'){
*name = malloc(str_p + 1);
strncpy(*name, *ptr, str_p);
(*name)[str_p] = '\0';
*ptr += (str_p + 1);
return PARSE_LABEL;
}
}
/* set 'ptr' and 'name' */
return PARSE_ERR;
}
/*
* parse_line: parse a line of y64 code (e.g., 'Loop: mrmovq (%rcx), %rsi')
* (you could combine above parse_xxx functions to do it)
* args
* line: point to a line_t data with a line of y64 assembly code
*
* return
* TYPE_XXX: success, fill line_t with assembled y64 code
* TYPE_ERR: error, try to print err information
*/
type_t parse_line(line_t *line)
{
/* when finish parse an instruction or lable, we still need to continue check
* e.g.,
* Loop: mrmovl (%rbp), %rcx
* call SUM #invoke SUM function */
char* y64asm = (line->y64asm);
regid_t regA, regB;
long long int value;
char* name = NULL;
instr_t* instr;
/* skip blank and check IS_END */
SKIP_BLANK(y64asm);
if(IS_END(y64asm)){
return line->type;
}
bool_t flag = FALSE;
/* is a comment ? */
if(IS_COMMENT(y64asm)){
flag = TRUE;
line->type = TYPE_COMM;
return line->type;
}
/* is a label ? */
if(parse_label(&y64asm, &name) == PARSE_LABEL){
line->type = TYPE_INS;
line->y64bin.addr = vmaddr;
//err_print("name = %s, addr = %x", name, line->y64bin.addr);
line->y64bin.bytes = 0;
int res = add_symbol(name);
if(res == -1){
line->type = TYPE_ERR;
return line->type;
}
}
SKIP_BLANK(y64asm);
if(IS_END(y64asm)){
return line->type;
}
/* is an instruction ? */
if(parse_instr(&y64asm, &instr) != -1){
line->type = TYPE_INS;
// else{
// line->type = TYPE_ERR;
// return line->type;
// }
line->y64bin.bytes = instr->bytes;
if(HIGH(instr->code) < 12){
line->y64bin.codes[0] = instr->code;
}
line->y64bin.addr = vmaddr;
vmaddr += instr->bytes;
itype_t instr_type = HIGH(instr->code);
switch (instr_type)
{
case I_HALT:{
break;
}
case I_NOP:{
break;
}
case I_RRMOVQ:{ //rrmovq regA, regB
//parse register A
if(parse_reg(&y64asm, ®A) == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
//parse delim
if(parse_delim(&y64asm, ',') == PARSE_ERR){
line->type = TYPE_ERR;
err_print("Invalid \'%c\'", ',');
return line->type;
}
//parse register B
if(parse_reg(&y64asm, ®B) == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
line->y64bin.codes[1] = HPACK(regA, regB);
break;
}
case I_IRMOVQ: { //irmovq imm/symbol, reg
//parse imm/symbol
parse_t res = parse_imm(&y64asm, &name, &value);
if(res == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
//parse delim
if(parse_delim(&y64asm, ',') == PARSE_ERR){
line->type = TYPE_ERR;
err_print("Invalid \'%c\'", ',');
return line->type;
}
//parse reg
if(parse_reg(&y64asm, ®B) == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
line->y64bin.codes[1] = HPACK(REG_NONE, regB);
if(res == PARSE_SYMBOL){
add_reloc(name, &line->y64bin);
}else if(res == PARSE_DIGIT){
for(int i = 0; i < 8; ++i){
int low = (value >> (8 * i)) & 0xF;
int high = (value >> (8 * i + 4)) & 0xF;
line->y64bin.codes[i + 2] = HPACK(high, low);
}
}
break;
}
case I_RMMOVQ: { //rmmovq reg, mm
//parse reg
if(parse_reg(&y64asm, ®A) == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
//parse delim
if(parse_delim(&y64asm, ',') == PARSE_ERR){
line->type = TYPE_ERR;
err_print("Invalid \'%c\'", ',');
return line->type;
}
//parse memory
if(parse_mem(&y64asm, &value, ®B) == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
//err_print("test1");
line->y64bin.codes[1] = HPACK(regA, regB);
for(int i = 0; i < 8; ++i){
int low = (value >> (8 * i)) & 0xF;
int high = (value >> (8 * i + 4)) & 0xF;
line->y64bin.codes[i + 2] = HPACK(high, low);
}
break;
}
case I_MRMOVQ:{ //mrmovq D(rB), rA
//parse mm
if(parse_mem(&y64asm, &value, ®B) == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
//parse delim
if(parse_delim(&y64asm, ',') == PARSE_ERR){
line->type = TYPE_ERR;
err_print("Invalid \'%c\'", ',');
return line->type;
}
//parse regA
if(parse_reg(&y64asm, ®A) == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
line->y64bin.codes[1] = HPACK(regA, regB);
for(int i = 0; i < 8; ++i){
int low = (value >> (8 * i)) & 0xF;
int high = (value >> (8 * i + 4)) & 0xF;
line->y64bin.codes[i + 2] = HPACK(high, low);
}
break;
}
case I_ALU: { //OPq rA, rB
//parse regA
parse_t res = parse_reg(&y64asm, ®A);
if(res == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
//parse delim
if(parse_delim(&y64asm, ',') == PARSE_ERR){
line->type = TYPE_ERR;
err_print("Invalid \'%c\'", ',');
return line->type;
}
//parse regB
res = parse_reg(&y64asm, ®B);
if(res == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
line->y64bin.codes[1] = HPACK(regA, regB);
break;
}
case I_JMP:{ //jxx Dest(digit or symbol)
parse_t res = parse_imm(&y64asm, &name, &value);
if(res == PARSE_ERR){
line->type = TYPE_ERR;
err_print("Invalid DEST");
return line->type;
}else if(res == PARSE_SYMBOL){
add_reloc(name, &line->y64bin);
}else if(res == PARSE_DIGIT){
for(int i = 0; i < 8; ++i){
int low = (value >> (8 * i)) & 0xF;
int high = (value >> (8 * i + 4)) & 0xF;
line->y64bin.codes[i + 1] = HPACK(high, low);
}
}
break;
}
case I_CALL: { //call Dest(digit or symbol)
parse_t res = parse_imm(&y64asm, &name, &value);
if(res == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}else if(res == PARSE_SYMBOL){
add_reloc(name, &line->y64bin);
}else if(res == PARSE_DIGIT){
for(int i = 0; i < 8; ++i){
int low = (value >> (8 * i)) & 0xF;
int high = (value >> (8 * i + 4)) & 0xF;
line->y64bin.codes[i + 1] = HPACK(high, low);
}
}
break;
}
case I_RET: {
break;
}
case I_PUSHQ: { //pushq rA
//parse regA
if(parse_reg(&y64asm, ®A) == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
line->y64bin.codes[1] = HPACK(regA, REG_NONE);
break;
}
case I_POPQ: { //popq rA
if(parse_reg(&y64asm, ®A) == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
line->y64bin.codes[1] = HPACK(regA, REG_NONE);
break;
}
case I_DIRECTIVE: {
dtv_t direc_code = LOW(instr->code);
parse_t res;
switch (direc_code)
{
case D_POS:{
res = parse_digit(&y64asm, &value);
if(res == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
vmaddr = value;
line->y64bin.addr = vmaddr;
break;
}
case D_ALIGN:{
res = parse_digit(&y64asm, &value);
if(res == PARSE_ERR){
line->type = TYPE_ERR;
return line->type;
}
vmaddr = ((vmaddr + value - 1) & (~(value - 1)));
line->y64bin.addr = vmaddr;
break;
}
case D_DATA:{
SKIP_BLANK(y64asm);
res = parse_data(&y64asm, &name, &value);
if(res == PARSE_DIGIT){
for(int i = 0; i < instr->bytes; ++i){
int low = (value >> (8 * i)) & 0xF;
int high = (value >> (8 * i + 4)) & 0xF;
line->y64bin.codes[i] = HPACK(high, low);
}
}else if(res == PARSE_SYMBOL){
line->y64bin.codes[0] = instr->code;
add_reloc(name, &line->y64bin);
}else if(res == PARSE_ERR){
line->type = PARSE_ERR;
return line->type;
}
break;
}
default:
break;
}
break;
}
default:
break;
}
}
return line->type;
}
/*
* assemble: assemble an y64 file (e.g., 'asum.ys')
* args
* in: point to input file (an y64 assembly file)
*
* return
* 0: success, assmble the y64 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 *y64asm;
/* read y64 code line-by-line, and parse them to generate raw y64 binary code list */
while (fgets(asm_buf, MAX_INSLEN, in) != NULL) {
slen = strlen(asm_buf);
while ((asm_buf[slen-1] == '\n') || (asm_buf[slen-1] == '\r')) {
asm_buf[--slen] = '\0'; /* replace terminator */
}
/* store y64 assembly code */
y64asm = (char *)malloc(sizeof(char) * (slen + 1)); // free in finit
strcpy(y64asm, asm_buf);
line = (line_t *)malloc(sizeof(line_t)); // free in finit
memset(line, '\0', sizeof(line_t));
line->type = TYPE_COMM;
line->y64asm = y64asm;
line->next = NULL;
line_tail->next = line;
line_tail = line;
lineno ++;
if (parse_line(line) == TYPE_ERR) {
return -1;
}
// if(lineno >= 5){
// err_print("reltab in assemble = %x", reltab->y64bin->codes[0]);
// }
//err_print("y64asm = %s", line_tail->y64asm);
}
lineno = -1;
//err_print("assemble");
return 0;
}
/*
* relocate: relocate the raw y64 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 = reltab;
while (rtmp) {
char* name = rtmp->name;
if(!name){
rtmp = rtmp->next;
continue;
}
symbol_t* symb = find_symbol(name);
if(symb == NULL) {
err_print("Unknown symbol:\'%s\'", name);
return -1;
}
//err_print("reloc name = %s, addr = %x", rtmp->name, symb->addr);
/* relocate y64bin according itype */
//rtmp->y64bin->addr = symb->addr;
itype_t type = HIGH(rtmp->y64bin->codes[0]);
if(type == I_IRMOVQ){
for(int i = 0; i < 8; ++i){
int low = (symb->addr >> (8 * i)) & 0xF;
int high = (symb->addr >> (8 * i + 4)) & 0xF;
rtmp->y64bin->codes[i + 2] = HPACK(high, low);
}
}else if(type == I_CALL || type == I_JMP){
for(int i = 0; i < 8; ++i){
int low = ((symb->addr) >> (8 * i)) & 0xF;
int high = ((symb->addr) >> (8 * i + 4)) & 0xF;
rtmp->y64bin->codes[i + 1] = HPACK(high, low);
}
}
else if(type == I_DIRECTIVE){
//err_print("test");
for(int i = 0; i < 8; ++i){
int low = ((symb->addr) >> (8 * i)) & 0xF;
int high = ((symb->addr) >> (8 * i + 4)) & 0xF;
rtmp->y64bin->codes[i] = HPACK(high, low);
}
}
/* next */
rtmp = rtmp->next;
}
return 0;
}
/*
* binfile: generate the y64 binary file
* args
* out: point to output file (an y64 binary file)
*
* return
* 0: success
* -1: error
*/
int binfile(FILE *out)
{
/* prepare image with y64 binary code */
/* binary write y64 code to output file (NOTE: see fwrite()) */
line_t* cur = line_head->next;
while(cur){
switch (cur->type)
{
case TYPE_COMM:{
break;
}
case TYPE_INS:{
if(fseek(out, cur->y64bin.addr, SEEK_SET) != 0){
return -1;
}
fwrite(cur->y64bin.codes, 1, cur->y64bin.bytes, out);
break;
}
case TYPE_ERR:{
break;
}
default:
break;
}
cur = cur->next;
}
return 0;
}
/* whether print the readable output to screen or not ? */
bool_t screen = FALSE;
void print_line(line_t *line)
{
char buf[64];
/* line format: 0xHHH: cccccccccccc | <line> */
if (line->type == TYPE_INS) {
bin_t *y64bin = &line->y64bin;
int i;
strcpy(buf, " 0x000: | ");
hexstuff(buf+4, y64bin->addr, 3);
if (y64bin->bytes > 0)
for (i = 0; i < y64bin->bytes; i++)
hexstuff(buf+9+2*i, y64bin->codes[i]&0xFF, 2);
} else {
strcpy(buf, " | ");
}
printf("%s%s\n", buf, line->y64asm);
}
/*
* print_screen: dump readable binary and assembly code to screen
* (e.g., Figure 4.8 in ICS book)
*/
void print_screen(void)
{
line_t *tmp = line_head->next;
while (tmp != NULL) {
print_line(tmp);
tmp = tmp->next;
}
}
/* init and finit */
void init(void)
{
reltab = (reloc_t *)malloc(sizeof(reloc_t)); // free in finit
memset(reltab, 0, sizeof(reloc_t));