-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrap.c
188 lines (168 loc) · 5.14 KB
/
scrap.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
void parse_line2(char *line, int len)
{
struct inst inst = {};
struct as as = {};
// TODO: LABELS
// TODO: COMMENTS
// TODO: NO LIMITS, IF THE VALUE IS OUT OF BOUNDS EMIT MULTIPLE INSTRUCTIONS
char *tok;
int tok_len;
repeat:
while(len > 0 && isspace(*line)) {
line++; len--;
}
if(len == 0 || *line == '#')
goto exit;
printf("parse: %.*s\n", len - 1, line);
struct arg label = {};
int toklen = get_label(&label, line, len);
if(toklen && toklen < len && line[toklen] == ':') {
goto repeat;
}
char *opcode = gettok(&line, &len, 0);
int opcode_len = line - opcode;
//if(parse_label(opcode, opcode_len) == 0) {
// //current_label = &labels[labelidx - 1];
// goto repeat;
//}
struct inst_desc *desc = opcode_map;
for(; desc->opcode; ++desc) {
//printf("OPCODE: %.*s %s\n", opcode_len, opcode, desc->opcode);
if(opcode_len != desc->opcode_len)
continue;
if(memcmp(opcode, desc->opcode, desc->opcode_len))
continue;
for(int i = 0; i < MAX_ARG; ++i) {
if(getarg(&as.a[i], &line, &len))
break;
}
//printf("A0"); print_arg(as.a + 0); puts("");
//printf("A1"); print_arg(as.a + 1); puts("");
//printf("A2"); print_arg(as.a + 2); puts("");
as.desc = desc;
do_emit_inst(desc, as.a + 0, as.a + 1, as.a + 2);
goto exit;
#if 0
if(desc->type & INST_A) {
tok = gettok(&line, &len, ',');
as.a[0].type = ARG_REG;
as.a[0].value = parse_reg(tok, line - tok);
if(desc->type & INST_B) {
tok = gettok(&line, &len, 0);
if(line - tok != 1 || tok[0] != ',')
fail("Forgot a comma");
tok = gettok(&line, &len, ',');
as.a[1].type = ARG_REG;
as.a[1].value = parse_reg(tok, line - tok);
if(desc->type & INST_C) {
tok = gettok(&line, &len, 0);
as.a[2].type = ARG_REG;
if(line - tok != 1 || tok[0] != ',')
fail("Forgot a comma");
tok = gettok(&line, &len, ',');
as.a[2].value = parse_reg(tok, line - tok);
} else if(desc->type & INST_IMM) {
tok = gettok(&line, &len, 0);
as.a[2].type = ARG_IMM;
if(line - tok != 1 || tok[0] != ',') {
as.a[2].value = 0;
} else {
tok = gettok(&line, &len, '#');
as.a[2].value = parse_imm(tok, line - tok);
//printf("IMM %d\n", parse_imm(tok, line - tok));
}
}
} else if(desc->type & INST_IMM) {
tok = gettok(&line, &len, 0);
as.a[1].type = ARG_IMM;
if(line - tok != 1 || tok[0] != ',') {
as.a[1].value = 0;
} else {
tok = gettok(&line, &len, '#');
as.a[1].value = parse_imm(tok, line - tok);
//printf("IMM %d\n", parse_imm(tok, line - tok));
}
}
} else if (desc->type & INST_DATA) {
// TODO: allow .fill a, b syntax
tok = gettok(&line, &len, 0);
as.a[0].type = ARG_IMM;
if(desc->op == DOP_PAD) {
as.a[0].value = parse_imm(tok, line - tok);
} else if (desc->op == DOP_DW) {
as.a[0].value = parse_imm(tok, line - tok);
} else {
fail("");
}
}
// #else
switch(as->desc->type) {
case INST_A_IMM:
/* A */
tok = gettok(&line, &len, ',');
as.a[0].type = ARG_REG;
as.a[0].value = parse_reg(tok, line - tok);
/* COMMA */
tok = gettok(&line, &len, 0);
if(line - tok != 1 || tok[0] != ',')
fail("Forgot a comma");
/* IMMEDIATE */
tok = gettok(&line, &len, '#');
as.a[1].type = ARG_IMM;
as.a[1].value = parse_imm(tok, line - tok, IMM_MAX);
break;
case INST_AB_IMM:
/* A */
tok = gettok(&line, &len, ',');
as.a[0].type = ARG_REG;
as.a[0].value = parse_reg(tok, line - tok);
/* COMMA */
tok = gettok(&line, &len, 0);
if(line - tok != 1 || tok[0] != ',')
fail("Forgot a comma");
/* B */
tok = gettok(&line, &len, ',');
as.a[1].type = ARG_REG;
as.a[1].value = parse_reg(tok, line - tok);
/* COMMA */
tok = gettok(&line, &len, 0);
if(line - tok != 1 || tok[0] != ',')
fail("Forgot a comma");
/* IMMEDIATE */
tok = gettok(&line, &len, '#');
as.a[2].type = ARG_IMM;
as.a[2].value = parse_imm(tok, line - tok, IMM_MAX);
break;
case INST_ABC:
/* A */
tok = gettok(&line, &len, ',');
as.a[0].type = ARG_REG;
as.a[0].value = parse_reg(tok, line - tok);
/* COMMA */
tok = gettok(&line, &len, 0);
if(line - tok != 1 || tok[0] != ',')
fail("Forgot a comma");
/* B */
tok = gettok(&line, &len, ',');
as.a[1].type = ARG_REG;
as.a[1].value = parse_reg(tok, line - tok);
/* COMMA */
tok = gettok(&line, &len, 0);
if(line - tok != 1 || tok[0] != ',')
fail("Forgot a comma");
/* IMMEDIATE */
tok = gettok(&line, &len, '#');
as.a[2].type = ARG_IMM;
as.a[2].value = parse_imm(tok, line - tok, IMM_MAX);
break;
break;
case INST_DATA:
default:
fail("Unknown instruction type");
}
#endif
}
fail("Unknown opcode");
exit:
return;
}