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
|
/*
* Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Hyra nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/errno.h>
#include <string.h>
#include <oasm/state.h>
#include <oasm/lex.h>
#include <oasm/log.h>
#define is_num(c) ((c) >= '0' && (c) <= '9')
static char putback = '\0';
/* Instruction mnemonic strings */
#define S_IMN_MOV "mov"
#define S_IMN_ADD "add"
#define S_IMN_SUB "sub"
#define S_IMN_DIV "div"
#define S_IMN_INC "inc"
#define S_IMN_DEC "dec"
/*
* Returns 0 if a char is counted as a
* skippable token. Otherwise, -1
*/
static inline int
lex_skippable(struct oasm_state *state, char c)
{
switch (c) {
case ' ': return 0;
case '\f': return 0;
case '\t': return 0;
case '\r': return 0;
case '\n':
++state->line;
return 0;
}
return -1;
}
/*
* Put back a token to grab later
*
* @c: Character to put back
*/
static inline char
lex_putback(char c)
{
putback = c;
return c;
}
/*
* Grab a character from the input file
* descriptor.
*/
static char
lex_cin(struct oasm_state *state)
{
char retval;
if (putback != '\0') {
retval = putback;
putback = '\0';
return retval;
}
if (read(state->in_fd, &retval, 1) <= 0) {
return '\0';
}
return retval;
}
/*
* Nom an operation, directive or any kind
* of raw string (unquoted/builtin) and return
* memory allocated by strdup() pointing to the
* string.
*
* @state: OASM state pointer
* @res: Resulting string
*
* Returns 0 on success. Greater than zero
* value of the last character if a comma or
* space was not buffered.
*/
static int
lex_nomstr(struct oasm_state *state, char **res)
{
char buf[256];
int retval = 0, n = 0;
int tmp;
memset(buf, 0, sizeof(buf));
/*
* We are filling the buffer containing
* the operation or directive.
*
* Keep going until we hit a space or comman (^)
* Examples of such strings (everything in '[]'):
*
* [mov] [x0], [#1]
* ^ ^
*/
while ((tmp = lex_cin(state)) != 0) {
if (tmp == ' ' || tmp == ',') {
retval = tmp;
break;
}
if (tmp == '\n') {
++state->line;
retval = tmp;
break;
}
buf[n++] = tmp;
}
*res = strdup(buf);
return retval;
}
static tt_t
token_arith(char *p)
{
if (strcmp(p, S_IMN_MOV) == 0) {
return TT_MOV;
} else if (strcmp(p, S_IMN_INC) == 0) {
return TT_INC;
} else if (strcmp(p, S_IMN_DEC) == 0) {
return TT_DEC;
} else if (strcmp(p, S_IMN_ADD) == 0) {
return TT_ADD;
} else if (strcmp(p, S_IMN_SUB) == 0) {
return TT_SUB;
} else if (strcmp(p, S_IMN_DIV) == 0) {
return TT_DIV;
}
return TT_UNKNOWN;
}
static tt_t
token_xreg(char *p)
{
int num;
if (p[0] != 'x') {
return TT_UNKNOWN;
}
if (!is_num(p[1])) {
return TT_UNKNOWN;
}
num = atoi(&p[1]);
switch (num) {
case 0: return TT_X0;
case 1: return TT_X1;
case 2: return TT_X2;
case 3: return TT_X3;
case 4: return TT_X4;
case 5: return TT_X5;
case 6: return TT_X6;
case 7: return TT_X7;
case 8: return TT_X8;
case 9: return TT_X9;
case 10: return TT_X10;
case 11: return TT_X11;
case 12: return TT_X12;
case 13: return TT_X13;
case 14: return TT_X14;
case 15: return TT_X15;
}
return TT_UNKNOWN;
}
static tt_t
token_operand(char *p)
{
/* Is this a numeric constant? */
if (p[0] == '#') {
return TT_IMM;
}
return TT_UNKNOWN;
}
static tt_t
token_reg(char *p)
{
tt_t tok;
if ((tok = token_xreg(p)) != TT_UNKNOWN) {
return tok;
}
return TT_UNKNOWN;
}
int
lex_tok(struct oasm_state *state, struct oasm_token *ttp)
{
char *p;
char c = ' ';
int tmp;
tt_t tok;
if (state == NULL || ttp == NULL) {
return -EINVAL;
}
/*
* Grab characters. If they are skippable,
* don't use them.
*/
while (lex_skippable(state, c) == 0) {
if ((c = lex_cin(state)) == 0) {
return -1;
}
}
switch (c) {
case '\n':
++state->line;
return 0;
case '\0':
return -1;
case ',':
return TT_COMMA;
default:
ttp->type = TT_UNKNOWN;
ttp->raw = NULL;
lex_putback(c);
lex_nomstr(state, &p);
/* Arithmetic operation? */
if ((tok = token_arith(p)) != TT_UNKNOWN) {
ttp->type = tok;
ttp->raw = p;
return 0;
}
/* Register? */
if ((tok = token_reg(p)) != TT_UNKNOWN) {
ttp->is_reg = 1;
ttp->type = tok;
ttp->raw = p;
return 0;
}
/* Immediate operand? */
if ((tok = token_operand(p)) != TT_UNKNOWN) {
ttp->type = tok;
ttp->raw = p;
return 0;
}
oasm_err("bad token \"%s\"\n", p);
return -1;
}
return 0;
}
|