Some I/O Hack
The right way to support I/O functionality would require function calls. In the meantime we provide some limited I/O support through two operatos. The input operator '$>' can be used to read-in an unsigned integer into a l-value expression. And the output operator '$<' can be used to print an assignment expression.
In the grammar we integrate this hack such that it easily can be removed once we have proper function calls:
\[\begin{array}{rcl}\text{input-sequence} & = & \{\; $> \; \text{assignment-expr}\; \texttt{;}\; |\; $< \; \text{assignment-expr}\; \texttt{;}\; |\; \text{expr-statement}\; \}\; \\\text{expr-statement} & = & \text{assignment-expr}\; \texttt{;}\; \\\text{assignment-expr} & = & \text{expr}\; [\; \texttt{=}\; \text{assignment-expr}\; ]\; \\\text{expr} & = & \text{term}\; \{\; (\; \texttt{"+"}\; |\; \texttt{"-"}\; )\; \text{term} \} \\\text{term} & = & \text{unary-expr}\; \{\; (\; \texttt{"*"}\; |\; \texttt{"/"}\; |\; \texttt{"%"}\; )\; \text{unary-expr} \} \\\text{unary-expr} & = & \text{factor}\; |\; (\; \texttt{"+"}\; |\; \texttt{"-"}\;)\; \text{unary-expr} \\\text{factor} & = & \text{identifier}\; \\ & | & \text{dec-literal}\; \\ & | & \text{hex-literal}\; \\ & | & \text{oct-literal}\; \\ & | & \texttt{"("}\; \text{assignment-expr}\; \texttt{")"}\; \\\end{array}\]Assembly Functions for I/O
Below you find assembly code for funtion get_uint64() that reads-in and returns an unsigned value:
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 | .equ FP, 1
.equ SP, 2
.equ RET_ADDR, 3
.equ ret, 0
.equ fp, ret + 8
.equ rval, fp + 8
.equ fparam0, rval + 8
// function: %RET_VAL get_uint64()
.text
get_uint64:
// prologue
movq %RET_ADDR, ret(%SP)
movq %FP, fp(%SP)
addq 0, %SP, %FP
subq 8 * 2, %SP, %SP
.equ dest, 6
.equ ch, dest + 1
movq %0, %dest
.get_uint64.read:
getc %ch
# if %ch < '0' then we are done
subq '0', %ch, %0
jb .get_uint64.ret
# if %ch > '9' then we are done
subq '9', %ch, %0
ja .get_uint64.ret
subq '0', %ch, %ch
imulq 10, %dest, %dest
addq %ch, %dest, %dest
jmp .get_uint64.read
.get_uint64.ret:
movq %dest, rval(%FP)
// epilogue
movq %FP, %SP
movq fp(%SP), %FP
movq ret(%SP), %RET_ADDR
jmp %RET_ADDR, %0
|
And function print_uint64() that prints an unsigned integer passed as parameter:
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 | .equ FP, 1
.equ SP, 2
.equ RET_ADDR, 3
.equ ret, 0
.equ fp, ret + 8
.equ rval, fp + 8
.equ fparam0, rval + 8
// function: void print_uint64(val)
// static char[20] buf;
.bss
.print_uint64.buf:
.space 20
.text
print_uint64:
// prologue
movq %RET_ADDR, ret(%SP)
movq %FP, fp(%SP)
addq 0, %SP, %FP
subq 8 * 2, %SP, %SP
.equ buf, 6
.equ val, buf + 1
.equ digit, val + 1
.equ p, digit + 1
ldpa .print_uint64.pool, %buf
ldfp (%buf), %buf
movq %buf, %p
movq fparam0(%FP), %val
.print_uint64.get_digit:
divq 10, %val, %val
addq '0', %digit, %digit
movb %digit, (%p)
addq 1, %p, %p
subq 0, %val, %0
jnz .print_uint64.get_digit
.print_uint64.print_digit:
subq 1, %p, %p
movzbq (%p), %digit
putc %digit
subq %buf, %p, %0
jnz .print_uint64.print_digit
// epilogue
movq %FP, %SP
movq fp(%SP), %FP
movq ret(%SP), %RET_ADDR
jmp %RET_ADDR, %0
.align 8
.print_uint64.pool:
.quad .print_uint64.buf
|
Of course this code needs to be “available” for the assembly code that our compiler produces. We have several choices:
-
The complete source code of both function could be generated by genFooter().
-
We can generate object files getuint64.o and printuint64.o``. The object files generated from our assembler output can be linked against these.
-
The ULM assembler can process a sequence of assembly files and treats this input sequence as-if it was one large source file.
For now the last option listed above is the simplest. If the compiler generated foo.s then we invoke the assembler with
1 | path-to-ulm/ulmas foo.s getuint64.s printuint64.s
|
In a subsequent section you will find a makefile that can be used in a separate directory for testing our compiler. In the makefile we can hide such nasty details.
Changes for the Lexer
We at least need three more token kinds for '$', '<' and '>'. But now is a good opportunity to added even a few more:
end of input |
EOI |
bad token |
BAD_TOKEN |
[1-9][0-9]* |
DEC_LITERAL |
0x[0-9a-fA-F]+ |
HEX_LITERAL |
0[0-7]* |
OCT_LITERAL |
& |
AMPERSAND |
&& |
AMPERSAND2 |
* |
ASTERISK |
^ |
CARET |
$ |
DOLLAR |
= |
EQUAL |
== |
EQUAL2 |
! |
NOT |
!= |
NOT_EQUAL |
> |
GREATER |
>= |
GREATER_EQUAL |
< |
LESS |
<= |
LESS_EQUAL |
( |
LPAREN |
- |
MINUS |
% |
PERCENT |
+ |
PLUS |
) |
RPAREN |
; |
SEMICOLON |
/ |
SLASH |
~ |
TILDE |
| |
VBAR |
|| |
VBAR2 |
[a-zA-Z_][a-zA-Z_0-9]* |
IDENTIFIER |
Here the applied changes to tokenkind.txt and lexer.c:
EOI
BAD_TOKEN
DEC_LITERAL
HEX_LITERAL
OCT_LITERAL
AMPERSAND
AMPERSAND2
ASTERISK
CARET
DOLLAR
EQUAL
EQUAL2
NOT
NOT_EQUAL
GREATER
GREATER_EQUAL
LESS
LESS_EQUAL
LPAREN
MINUS
PERCENT
PLUS
RPAREN
SEMICOLON
SLASH
TILDE
VBAR
VBAR2
IDENTIFIER
#include <stdbool.h>
#include <stdio.h>
#include "finalize.h"
#include "lexer.h"
struct Token token;
static void
cleanup(void)
{
releaseStr(&token.val);
}
//------------------------------------------------------------------------------
// position of current character ch
static struct TokenPos curr = {
1,
0,
};
static int ch;
static int
nextCh(void)
{
++curr.col;
ch = getchar();
if (ch == '\n') {
++curr.line;
curr.col = 0;
}
return ch;
}
static bool
isWhiteSpace(int ch)
{
return ch == ' ' || ch == '\t';
}
static bool
isDecDigit(int ch)
{
return ch >= '0' && ch <= '9';
}
static bool
isOctDigit(int ch)
{
return ch >= '0' && ch <= '7';
}
static bool
isHexDigit(int ch)
{
return isDecDigit(ch) || (ch >= 'a' && ch <= 'f') ||
(ch >= 'A' && ch <= 'F');
}
static bool
isLetter(int ch)
{
return ((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A' && ch <= 'Z')) ||
ch == '_';
}
enum TokenKind
getToken(void)
{
static bool first = true;
if (first) {
first = false;
finalizeRegister(cleanup);
}
// init ch, skip white spaces and newlines
while (ch == 0 || isWhiteSpace(ch) || ch == '\n') {
nextCh();
}
token.pos.line = curr.line;
token.pos.col = curr.col;
clearStr(&token.val);
if (ch == EOF) {
return token.kind = EOI;
} else if (isDecDigit(ch)) {
// parse literal
if (ch == '0') {
appendCharToStr(&token.val, ch);
nextCh();
if (ch == 'x') {
appendCharToStr(&token.val, ch);
nextCh();
if (isHexDigit(ch)) {
while (isHexDigit(ch)) {
appendCharToStr(&token.val, ch);
nextCh();
}
return token.kind = HEX_LITERAL;
}
return token.kind = BAD_TOKEN;
}
while (isOctDigit(ch)) {
appendCharToStr(&token.val, ch);
nextCh();
}
return token.kind = OCT_LITERAL;
} else if (isDecDigit(ch)) {
while (isDecDigit(ch)) {
appendCharToStr(&token.val, ch);
nextCh();
}
return token.kind = DEC_LITERAL;
}
} else if (ch == '&') {
appendCharToStr(&token.val, ch);
nextCh();
if (ch == '&') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = AMPERSAND2;
}
return token.kind = AMPERSAND;
} else if (ch == '*') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = ASTERISK;
} else if (ch == '^') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = CARET;
} else if (ch == '$') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = DOLLAR;
} else if (ch == '=') {
appendCharToStr(&token.val, ch);
nextCh();
if (ch == '=') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = EQUAL2;
}
return token.kind = EQUAL;
} else if (ch == '!') {
appendCharToStr(&token.val, ch);
nextCh();
if (ch == '=') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = NOT_EQUAL;
}
return token.kind = NOT;
} else if (ch == '>') {
appendCharToStr(&token.val, ch);
nextCh();
if (ch == '=') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = GREATER_EQUAL;
}
return token.kind = GREATER;
} else if (ch == '<') {
appendCharToStr(&token.val, ch);
nextCh();
if (ch == '=') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = LESS_EQUAL;
}
return token.kind = LESS;
} else if (ch == '(') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = LPAREN;
} else if (ch == '-') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = MINUS;
} else if (ch == '%') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = PERCENT;
} else if (ch == '+') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = PLUS;
} else if (ch == ')') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = RPAREN;
} else if (ch == ';') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = SEMICOLON;
} else if (ch == '/') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = SLASH;
} else if (ch == '~') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = TILDE;
} else if (ch == '|') {
appendCharToStr(&token.val, ch);
nextCh();
if (ch == '|') {
appendCharToStr(&token.val, ch);
nextCh();
return token.kind = VBAR2;
}
return token.kind = VBAR;
} else if (isLetter(ch)) {
do {
appendCharToStr(&token.val, ch);
nextCh();
} while (isLetter(ch) || isDecDigit(ch));
return token.kind = IDENTIFIER;
}
nextCh();
return token.kind = BAD_TOKEN;
}
The test for the lexer was changed so that a bad token triggers an assertion failure. Here a minimalistic test run for the lexer:
theon$ make xtest_lexer gcc -o xgen_tokenkind xgen_tokenkind.c -lm ./xgen_tokenkind tokenkind.txt gen_tokenkind.h gen_strtokenkind.c gcc -c -Wall -Wcast-qual -MT xtest_lexer.o -MMD -MP -MF xtest_lexer.c.d xtest_lexer.c gcc -c -Wall -Wcast-qual -MT expr.o -MMD -MP -MF expr.c.d expr.c gcc -c -Wall -Wcast-qual -MT finalize.o -MMD -MP -MF finalize.c.d finalize.c gcc -c -Wall -Wcast-qual -MT gen.o -MMD -MP -MF gen.c.d gen.c gcc -c -Wall -Wcast-qual -MT lexer.o -MMD -MP -MF lexer.c.d lexer.c gcc -c -Wall -Wcast-qual -MT memregion.o -MMD -MP -MF memregion.c.d memregion.c gcc -c -Wall -Wcast-qual -MT parser.o -MMD -MP -MF parser.c.d parser.c gcc -c -Wall -Wcast-qual -MT str.o -MMD -MP -MF str.c.d str.c gcc -c -Wall -Wcast-qual -MT sym.o -MMD -MP -MF sym.c.d sym.c gcc -c -Wall -Wcast-qual -MT tokenkind.o -MMD -MP -MF tokenkind.c.d tokenkind.c gcc -c -Wall -Wcast-qual -MT ustr.o -MMD -MP -MF ustr.c.d ustr.c gcc -o xtest_lexer xtest_lexer.o expr.o finalize.o gen.o lexer.o memregion.o parser.o str.o sym.o tokenkind.o ustr.o -lm theon$ cat test_lexer.in | ./xtest_lexer 1.1: IDENTIFIER 'a' 1.3: EQUAL '=' 1.5: DEC_LITERAL '5' 1.6: SEMICOLON ';' 2.1: IDENTIFIER 'b' 2.3: EQUAL '=' 2.5: DEC_LITERAL '42' 2.7: SEMICOLON ';' 3.1: IDENTIFIER 'c' 3.3: EQUAL '=' 3.5: LPAREN '(' 3.6: IDENTIFIER 'a' 3.8: PLUS '+' 3.10: IDENTIFIER 'b' 3.11: RPAREN ')' 3.13: ASTERISK '*' 3.14: DEC_LITERAL '2' 3.15: SEMICOLON ';' 4.1: DEC_LITERAL '123' 4.5: OCT_LITERAL '0123' 4.10: HEX_LITERAL '0xaB12' 4.17: IDENTIFIER 'abc' 4.21: PLUS '+' 4.22: MINUS '-' 4.23: SLASH '/' 4.24: ASTERISK '*' 4.25: PERCENT '%' 4.26: CARET '^' 4.27: LPAREN '(' 4.28: RPAREN ')' 5.1: DOLLAR '$' 5.3: EQUAL '=' 5.5: EQUAL2 '==' 5.8: NOT '!' 5.9: TILDE '~' 5.10: NOT_EQUAL '!=' 5.12: GREATER '>' 5.13: GREATER_EQUAL '>=' 5.15: LESS '<' 5.16: LESS_EQUAL '<=' 5.18: LPAREN '(' 5.19: RPAREN ')' 5.20: VBAR '|' 5.22: VBAR2 '||' 5.25: TILDE '~' theon$
The test input for the lexer was changed to
a = 5;
b = 42;
c = (a + b) *2;
123 0123 0xaB12 abc +-/*%^()
$ = == !~!=>>=<<=()| || ~
Changes in the Code Generation Interface
The code generation interface now declares four more functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #ifndef ABC_GEN_H
#define ABC_GEN_H
// ...
// fetch / store quad word (8 bytes)
void genFetch(GenReg addr, GenReg dest);
void genFetchDispl(int64_t displ, GenReg addr, GenReg dest); // new
void genStore(GenReg src, GenReg addr);
void genStoreDispl(GenReg src, int64_t displ, GenReg addr); // new
// ...
// IO hack
void genOutHack(GenReg src); // new
void genInHack(GenReg dest); // new
#endif // ABC_GEN_H
|
Functions genFetchDispl() and genStoreDispl() can be used for fetch and store instructions where a displacement is needed. For example, genFetchDispl(16, 8, 9) generates
1 | movq 16(%8), %9
|
If the displacement can not be encoded code get generates to compute the displaced address and for fetching data from there. For example, genFetchDispl(256, 8, 9) might acquire %6 as temporary register and generate
1 2 3 | ldzwq 0x100, %6
addq %6, %8, %6
movq (%6), %9
|
Function genHackOut() generates code to call a function for printing the unsigned integer in the register specified by src.
Function genInHack() generates code to call a function for reading-in an unsigned integer and for storing it in the register specified by dest.
Here the complete header and source file:
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 | #ifndef ABC_GEN_H
#define ABC_GEN_H
#include <stdint.h>
#include <stdio.h>
// register type
typedef uint8_t GenReg;
// output needs to be set before using the rest of the interface
void genSetOutput(FILE *out);
// generate data
void genLabeledUInt(const char *label, uint64_t val);
// header / footer
void genHeader(void);
void genFooter(void);
// set active segment
void genText(void);
void genData(void);
void genBSS(void);
// acquire / release register
GenReg genGetReg(void);
void genUngetReg(GenReg reg);
// load literal into register
void genLoadUInt(uint64_t val, GenReg reg);
void genLoadLabel(const char *label, GenReg reg);
// fetch / store quad word (8 bytes)
void genFetch(GenReg addr, GenReg dest);
void genFetchDispl(int64_t displ, GenReg addr, GenReg dest);
void genStore(GenReg src, GenReg addr);
void genStoreDispl(GenReg src, int64_t displ, GenReg addr);
// supported instruction
enum GenOp
{
GEN_OP2R_BEGIN,
GEN_UNARYMINUS_R,
GEN_OP2R_END,
GEN_OP3R_BEGIN = GEN_OP2R_END,
GEN_ADD_R = GEN_OP3R_BEGIN, // addition
GEN_SUB_R, // subtraction
GEN_IMUL_R, // multiplication
GEN_DIV_R, // division
GEN_MOD_R, // modulo
GEN_OP3R_END,
GEN_OP3I_BEGIN = GEN_OP3R_END,
GEN_ADD_I = GEN_OP3I_BEGIN,
GEN_SUB_I,
GEN_IMUL_I,
GEN_DIV_I,
GEN_MOD_I,
GEN_OP3I_END,
};
// 2 address instructions
void genOp2r(enum GenOp op, GenReg reg0, GenReg reg1);
// 3 address instructions
void genOp3r(enum GenOp op, GenReg reg0, GenReg reg1, GenReg reg2);
void genOp3i(enum GenOp op, uint64_t val, GenReg reg1, GenReg reg2);
// IO hack
void genOutHack(GenReg src);
void genInHack(GenReg dest);
#endif // ABC_GEN_H
|
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 | #include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include "finalize.h"
#include "gen.h"
enum {
ZERO = 0,
FP = 1,
SP = 2,
RET_ADDR = 3,
FUNC_ADDR = 4,
};
// output needs to be set before using the rest of the interface
static FILE *out;
void
genSetOutput(FILE *out_)
{
out = out_;
}
// acquire / release register
static bool usedReg[256];
static void
check(void)
{
GenReg reg = 0;
do {
if (usedReg[reg]) {
fprintf(stderr, "warning: register %" PRIu8 " not released\n", reg);
}
++reg;
} while (reg != 255);
}
GenReg
genGetReg(void)
{
static bool first = true;
if (first) {
first = false;
finalizeRegister(check);
}
GenReg reg = 5;
while (usedReg[++reg]) {
}
if (reg < 6) {
fprintf(stderr, "genGetReg: out of registers\n");
finalizeExit(1);
}
usedReg[reg] = true;
return reg;
}
void
genUngetReg(GenReg reg)
{
assert(usedReg[reg]);
usedReg[reg] = false;
}
// internal print stuff
static void
printInstr(const char *instr)
{
assert(out);
fprintf(out, "\t%s ", instr);
}
static void
printUInt_(uint64_t val, const char *prefix, const char *suffix)
{
assert(out);
if (prefix) {
fprintf(out, "%s", prefix);
}
fprintf(out, "0x%" PRIx64, val);
if (suffix) {
fprintf(out, "%s", suffix);
}
}
static void
printUInt(uint64_t val)
{
printUInt_(val, 0, 0);
}
static void
printReg(GenReg reg)
{
assert(out);
fprintf(out, "%%%" PRIu8, reg);
}
static void
printMem(GenReg reg)
{
assert(out);
fprintf(out, "(%%%" PRIu8 ")", reg);
}
static void
printDisplMem(int64_t displ, GenReg reg)
{
assert(out);
fprintf(out, "%" PRId64 "(%%%" PRIu8 ")", displ, reg);
}
static void
printLabel(const char *label, const char *prefix, const char *suffix)
{
assert(out);
if (prefix) {
fprintf(out, "%s", prefix);
}
fprintf(out, "%s", label);
if (suffix) {
fprintf(out, "%s", suffix);
}
}
static void
printLabelDef(const char *label)
{
assert(out);
fprintf(out, "%s:\n", label);
}
static void
printComma(void)
{
assert(out);
fprintf(out, ", ");
}
static void
printNl(void)
{
assert(out);
fprintf(out, "\n");
}
// header / footer
void
genHeader(void)
{
printInstr("ldzwq");
printUInt(0);
printComma();
printReg(SP);
printNl();
}
void
genFooter(void)
{
printInstr("halt");
printReg(0);
printNl();
}
// set active segment
void
genText(void)
{
printInstr(".text");
printNl();
}
void
genData(void)
{
printInstr(".data");
printNl();
}
void
genBSS(void)
{
printInstr(".bss");
printNl();
}
// generate data
void
genLabeledUInt(const char *label, uint64_t val)
{
printInstr(".align");
printUInt(8);
printNl();
printLabelDef(label);
printInstr(".quad");
printUInt(val);
printNl();
}
// load literal into register
void
genLoadUInt(uint64_t val, GenReg reg)
{
if (val <= 0xFFFFu) {
printInstr("ldzwq");
printUInt(val);
printComma();
printReg(reg);
printNl();
} else if (val <= 0xFFFFFFFFu) {
printInstr("ldzwq");
printUInt_(val, "@w1(", ")");
printComma();
printReg(reg);
printNl();
printInstr("shldwq");
printUInt_(val, "@w0(", ")");
printComma();
printReg(reg);
printNl();
} else {
printInstr("ldzwq");
printUInt_(val, "@w3(", ")");
printComma();
printReg(reg);
printNl();
printInstr("shldwq");
printUInt_(val, "@w2(", ")");
printComma();
printReg(reg);
printNl();
printInstr("shldwq");
printUInt_(val, "@w1(", ")");
printComma();
printReg(reg);
printNl();
printInstr("shldwq");
printUInt_(val, "@w0(", ")");
printComma();
printReg(reg);
printNl();
}
}
void
genLoadLabel(const char *label, GenReg reg)
{
printInstr("ldzwq");
printLabel(label, "@w3(", ")");
printComma();
printReg(reg);
printNl();
printInstr("shldwq");
printLabel(label, "@w2(", ")");
printComma();
printReg(reg);
printNl();
printInstr("shldwq");
printLabel(label, "@w1(", ")");
printComma();
printReg(reg);
printNl();
printInstr("shldwq");
printLabel(label, "@w0(", ")");
printComma();
printReg(reg);
printNl();
}
// load / fetch quad word (8 bytes)
void
genFetch(GenReg addr, GenReg dest)
{
printInstr("movq");
printMem(addr);
printComma();
printReg(dest);
printNl();
}
void
genFetchDispl(int64_t displ, GenReg addr, GenReg dest)
{
if (displ >= -128 && displ < 128) {
printInstr("movq");
printDisplMem(displ, addr);
printComma();
printReg(dest);
printNl();
} else {
// acquire new register and use it to store displ + addr
GenReg displAddr = genGetReg();
genLoadUInt(displ, displAddr); // ok, we use two's complenent for signed
genOp3r(GEN_ADD_R, displAddr, addr, displAddr);
genFetch(displAddr, dest);
genUngetReg(displAddr);
}
}
void
genStore(GenReg src, GenReg addr)
{
printInstr("movq");
printReg(src);
printComma();
printMem(addr);
printNl();
}
void
genStoreDispl(GenReg src, int64_t displ, GenReg addr)
{
if (displ >= -128 && displ < 128) {
printInstr("movq");
printReg(src);
printComma();
printDisplMem(displ, addr);
printNl();
} else {
// acquire new register and use it to store displ + addr
GenReg displAddr = genGetReg();
genLoadUInt(displ, displAddr); // ok, we use two's complenent for signed
genOp3r(GEN_ADD_R, displAddr, addr, displAddr);
genStore(src, displAddr);
genUngetReg(displAddr);
}
}
// 2 address instructions
void
genOp2r(enum GenOp op, GenReg reg0, GenReg reg1)
{
assert(op >= GEN_OP2R_BEGIN && op < GEN_OP2R_END);
if (op == GEN_UNARYMINUS_R) {
printInstr("subq");
printReg(reg0);
printComma();
printReg(ZERO);
printComma();
printReg(reg1);
printNl();
return;
} else {
assert(0);
}
}
// 3 address instructions
void
genOp3r(enum GenOp op, GenReg reg0, GenReg reg1, GenReg reg2)
{
assert(op >= GEN_OP3R_BEGIN && op < GEN_OP3R_END);
GenReg reg2_ = reg2;
if (op == GEN_ADD_R) {
printInstr("addq");
} else if (op == GEN_SUB_R) {
printInstr("subq");
} else if (op == GEN_IMUL_R) {
printInstr("imulq");
} else if (op == GEN_DIV_R || op == GEN_MOD_R) {
printInstr("divq");
reg2 = 4;
} else {
assert(0);
}
printReg(reg0);
printComma();
printReg(reg1);
printComma();
printReg(reg2);
printNl();
if (op == GEN_DIV_R || op == GEN_MOD_R) {
printInstr("movq");
printReg(op == GEN_DIV_R ? 4 : 5);
printComma();
printReg(reg2_);
printNl();
}
}
void
genOp3i(enum GenOp op, uint64_t val, GenReg reg1, GenReg reg2)
{
assert(op >= GEN_OP3I_BEGIN && op < GEN_OP3I_END);
if (val > 255) {
GenReg tmp = genGetReg();
genLoadUInt(val, tmp);
genOp3r(op - GEN_OP3I_BEGIN + GEN_OP3R_BEGIN, tmp, reg1, reg2);
genUngetReg(tmp);
return;
}
GenReg reg2_ = reg2;
if (op == GEN_ADD_I) {
printInstr("addq");
} else if (op == GEN_SUB_I) {
printInstr("subq");
} else if (op == GEN_IMUL_I) {
printInstr("imulq");
} else if (op == GEN_DIV_I || op == GEN_MOD_I) {
printInstr("divq");
reg2 = 4;
} else {
assert(0);
}
printUInt(val);
printComma();
printReg(reg1);
printComma();
printReg(reg2);
printNl();
if (op == GEN_DIV_I || op == GEN_MOD_I) {
printInstr("movq");
printReg(op == GEN_DIV_I ? 4 : 5);
printComma();
printReg(reg2_);
printNl();
}
}
// IO hack
void
genOutHack(GenReg src)
{
printInstr("subq");
printUInt(8 * (3 + 1));
printComma();
printReg(SP);
printComma();
printReg(SP);
printNl();
// store first function parameter
genStoreDispl(src, 24, SP);
// call function
genLoadLabel("print_uint64", FUNC_ADDR);
printInstr("call");
printReg(FUNC_ADDR);
printComma();
printReg(RET_ADDR);
printNl();
printInstr("addq");
printUInt(8 * (3 + 1));
printComma();
printReg(SP);
printComma();
printReg(SP);
printNl();
// print an additional newline
printInstr("putc");
printUInt('\n');
printNl();
}
void
genInHack(GenReg dest)
{
printInstr("subq");
printUInt(8 * (3 + 0));
printComma();
printReg(SP);
printComma();
printReg(SP);
printNl();
genLoadLabel("get_uint64", FUNC_ADDR);
printInstr("call");
printReg(FUNC_ADDR);
printComma();
printReg(RET_ADDR);
printNl();
// fetch return value
genFetchDispl(16, SP, dest);
printInstr("addq");
printUInt(8 * (3 + 0));
printComma();
printReg(SP);
printComma();
printReg(SP);
printNl();
}
|
Changes in the Parser
In the parser only function parse() needs to be changed:
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 | void
parse(void)
{
while (token.kind != EOI) {
if (token.kind == DOLLAR) {
getToken();
if (token.kind == GREATER) {
getToken();
// read unsigned integer
GenReg dest = genGetReg(), val = genGetReg();
struct TokenPos pos = token.pos;
const struct Expr *expr = parseExpr();
if (!isLValueExpr(expr)) {
errorAtPos(pos, "L-value expected");
}
genInHack(val);
loadExprAddr(expr, dest);
genStore(val, dest);
genUngetReg(dest);
genUngetReg(val);
} else if (token.kind == LESS) {
getToken();
// print unsigned integer
GenReg src = genGetReg();
const struct Expr *expr = parseAssignmentExpr();
loadExpr(expr, src);
genOutHack(src);
genUngetReg(src);
} else {
expectedError("'>' or '<'");
}
expected(SEMICOLON);
getToken();
} else {
parseExprStatement();
}
}
}
|
Testing the Compiler
Run make xtest_abc to build the compiler:
theon$ make xtest_abc gcc -c -Wall -Wcast-qual -MT xtest_abc.o -MMD -MP -MF xtest_abc.c.d xtest_abc.c gcc -o xtest_abc xtest_abc.o expr.o finalize.o gen.o lexer.o memregion.o parser.o str.o sym.o tokenkind.o ustr.o -lm theon$
Now a simple program like
1 2 3 | x = 21;
x = x * 2;
$< x;
|
can be used to print out the value of a variable. With the compiler we first generate the assembly code. Currently the compiler reads the source code from stdin so we have to redirect the source file with '<':
theon$ ./xtest_abc print_42.s < print_42.abc theon$
Here the generated assembly code:
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 | ldzwq 0x0, %2
ldzwq 0x15, %6
ldzwq @w3(x), %7
shldwq @w2(x), %7
shldwq @w1(x), %7
shldwq @w0(x), %7
movq %6, (%7)
ldzwq @w3(x), %6
shldwq @w2(x), %6
shldwq @w1(x), %6
shldwq @w0(x), %6
movq (%6), %6
imulq 0x2, %6, %6
ldzwq @w3(x), %7
shldwq @w2(x), %7
shldwq @w1(x), %7
shldwq @w0(x), %7
movq %6, (%7)
ldzwq @w3(x), %6
shldwq @w2(x), %6
shldwq @w1(x), %6
shldwq @w0(x), %6
movq (%6), %6
subq 0x20, %2, %2
movq %6, 24(%2)
ldzwq @w3(print_uint64), %4
shldwq @w2(print_uint64), %4
shldwq @w1(print_uint64), %4
shldwq @w0(print_uint64), %4
call %4, %3
addq 0x20, %2, %2
putc 0xa
halt %0
.data
.bss
.align 0x8
x:
.quad 0x0
|
Next we can use the ULM assembler to generate an executable. In addition to the generated assembly code print_42.s we also pass the source files getuint64.s and printuint64.s:
theon$ ../ulm-generator/1_ulm_build/stack/ulmas print_42.s getuint64.s printuint64.s theon$
This produced a.out
#TEXT 8
0x0000000000000000: 08 00 00 02 # ldzwq 0, %2
0x0000000000000004: 08 00 15 06 # ldzwq 21, %6
0x0000000000000008: 08 00 00 07 # ldzwq @w3(x), %7
0x000000000000000C: 15 00 00 07 # shldwq @w2(x), %7
0x0000000000000010: 15 00 00 07 # shldwq @w1(x), %7
0x0000000000000014: 15 01 38 07 # shldwq @w0(x), %7
0x0000000000000018: 19 06 00 07 # movq %6, (%7)
0x000000000000001C: 08 00 00 06 # ldzwq @w3(x), %6
0x0000000000000020: 15 00 00 06 # shldwq @w2(x), %6
0x0000000000000024: 15 00 00 06 # shldwq @w1(x), %6
0x0000000000000028: 15 01 38 06 # shldwq @w0(x), %6
0x000000000000002C: 12 06 00 06 # movq (%6), %6
0x0000000000000030: 0F 02 06 06 # imulq 2, %6, %6
0x0000000000000034: 08 00 00 07 # ldzwq @w3(x), %7
0x0000000000000038: 15 00 00 07 # shldwq @w2(x), %7
0x000000000000003C: 15 00 00 07 # shldwq @w1(x), %7
0x0000000000000040: 15 01 38 07 # shldwq @w0(x), %7
0x0000000000000044: 19 06 00 07 # movq %6, (%7)
0x0000000000000048: 08 00 00 06 # ldzwq @w3(x), %6
0x000000000000004C: 15 00 00 06 # shldwq @w2(x), %6
0x0000000000000050: 15 00 00 06 # shldwq @w1(x), %6
0x0000000000000054: 15 01 38 06 # shldwq @w0(x), %6
0x0000000000000058: 12 06 00 06 # movq (%6), %6
0x000000000000005C: 05 20 02 02 # subq 32, %2, %2
0x0000000000000060: 19 06 18 02 # movq %6, 24(%2)
0x0000000000000064: 08 00 00 04 # ldzwq @w3(print_uint64), %4
0x0000000000000068: 15 00 00 04 # shldwq @w2(print_uint64), %4
0x000000000000006C: 15 00 00 04 # shldwq @w1(print_uint64), %4
0x0000000000000070: 15 00 D0 04 # shldwq @w0(print_uint64), %4
0x0000000000000074: 14 04 03 00 # call %4, %3
0x0000000000000078: 0A 20 02 02 # addq 32, %2, %2
0x000000000000007C: 13 0A 00 00 # putc 10
0x0000000000000080: 01 00 00 00 # halt %0
0x0000000000000084: 19 03 00 02 # movq %RET_ADDR, ret(%SP)
0x0000000000000088: 19 01 08 02 # movq %FP, fp(%SP)
0x000000000000008C: 0A 00 02 01 # addq 0, %SP, %FP
0x0000000000000090: 05 10 02 02 # subq 8*2, %SP, %SP
0x0000000000000094: 0E 00 00 06 # movq %0, %dest
0x0000000000000098: 02 07 00 00 # getc %ch
0x000000000000009C: 05 30 07 00 # subq 48, %ch, %0
0x00000000000000A0: 0D 00 00 07 # jb .get_uint64.ret
0x00000000000000A4: 05 39 07 00 # subq 57, %ch, %0
0x00000000000000A8: 0C 00 00 05 # ja .get_uint64.ret
0x00000000000000AC: 05 30 07 07 # subq 48, %ch, %ch
0x00000000000000B0: 0F 0A 06 06 # imulq 10, %dest, %dest
0x00000000000000B4: 0E 07 06 06 # addq %ch, %dest, %dest
0x00000000000000B8: 04 FF FF F8 # jmp .get_uint64.read
0x00000000000000BC: 19 06 10 01 # movq %dest, rval(%FP)
0x00000000000000C0: 0E 01 00 02 # movq %FP, %SP
0x00000000000000C4: 12 02 08 01 # movq fp(%SP), %FP
0x00000000000000C8: 12 02 00 03 # movq ret(%SP), %RET_ADDR
0x00000000000000CC: 14 03 00 00 # jmp %RET_ADDR, %0
0x00000000000000D0: 19 03 00 02 # movq %RET_ADDR, ret(%SP)
0x00000000000000D4: 19 01 08 02 # movq %FP, fp(%SP)
0x00000000000000D8: 0A 00 02 01 # addq 0, %SP, %FP
0x00000000000000DC: 05 10 02 02 # subq 8*2, %SP, %SP
0x00000000000000E0: 16 00 14 06 # ldpa .print_uint64.pool, %buf
0x00000000000000E4: 17 06 00 06 # ldfp (%buf), %buf
0x00000000000000E8: 0E 06 00 09 # movq %buf, %p
0x00000000000000EC: 12 01 18 07 # movq fparam0(%FP), %val
0x00000000000000F0: 10 0A 07 07 # divq 10, %val, %val
0x00000000000000F4: 0A 30 08 08 # addq 48, %digit, %digit
0x00000000000000F8: 11 08 00 09 # movb %digit, (%p)
0x00000000000000FC: 0A 01 09 09 # addq 1, %p, %p
0x0000000000000100: 05 00 07 00 # subq 0, %val, %0
0x0000000000000104: 06 FF FF FB # jnz .print_uint64.get_digit
0x0000000000000108: 05 01 09 09 # subq 1, %p, %p
0x000000000000010C: 09 09 00 08 # movzbq (%p), %digit
0x0000000000000110: 03 08 00 00 # putc %digit
0x0000000000000114: 18 06 09 00 # subq %buf, %p, %0
0x0000000000000118: 06 FF FF FC # jnz .print_uint64.print_digit
0x000000000000011C: 0E 01 00 02 # movq %FP, %SP
0x0000000000000120: 12 02 08 01 # movq fp(%SP), %FP
0x0000000000000124: 12 02 00 03 # movq ret(%SP), %RET_ADDR
0x0000000000000128: 14 03 00 00 # jmp %RET_ADDR, %0
0x000000000000012C: 00 00 00 00 # for alignment
0x0000000000000130: 00 00 00 00
0x0000000000000134: 00 00 00 08 # .quad .print_uint64.buf
#DATA 1
#BSS 8 28
#SYMTAB
b x 0x0000000000000000
t print_uint64 0x00000000000000D0
a FP 0x0000000000000001
a SP 0x0000000000000002
a RET_ADDR 0x0000000000000003
a ret 0x0000000000000000
a fp 0x0000000000000008
a rval 0x0000000000000010
a fparam0 0x0000000000000018
t get_uint64 0x0000000000000084
a dest 0x0000000000000006
a ch 0x0000000000000007
t .get_uint64.read 0x0000000000000098
t .get_uint64.ret 0x00000000000000BC
b .print_uint64.buf 0x0000000000000008
a buf 0x0000000000000006
a val 0x0000000000000007
a digit 0x0000000000000008
a p 0x0000000000000009
t .print_uint64.pool 0x0000000000000130
t .print_uint64.get_digit 0x00000000000000F0
t .print_uint64.print_digit 0x0000000000000108
#FIXUPS
text 0x0000000000000130 0 64 absolute [bss]+8
text 0x0000000000000070 8 16 absolute @w0([text]+208)
text 0x000000000000006C 8 16 absolute @w1([text]+208)
text 0x0000000000000068 8 16 absolute @w2([text]+208)
text 0x0000000000000064 8 16 absolute @w3([text]+208)
text 0x0000000000000054 8 16 absolute @w0([bss]+312)
text 0x0000000000000050 8 16 absolute @w1([bss]+312)
text 0x000000000000004C 8 16 absolute @w2([bss]+312)
text 0x0000000000000048 8 16 absolute @w3([bss]+312)
text 0x0000000000000040 8 16 absolute @w0([bss]+312)
text 0x000000000000003C 8 16 absolute @w1([bss]+312)
text 0x0000000000000038 8 16 absolute @w2([bss]+312)
text 0x0000000000000034 8 16 absolute @w3([bss]+312)
text 0x0000000000000028 8 16 absolute @w0([bss]+312)
text 0x0000000000000024 8 16 absolute @w1([bss]+312)
text 0x0000000000000020 8 16 absolute @w2([bss]+312)
text 0x000000000000001C 8 16 absolute @w3([bss]+312)
text 0x0000000000000014 8 16 absolute @w0([bss]+312)
text 0x0000000000000010 8 16 absolute @w1([bss]+312)
text 0x000000000000000C 8 16 absolute @w2([bss]+312)
text 0x0000000000000008 8 16 absolute @w3([bss]+312)
Which now can be executed:
theon$ ../ulm-generator/1_ulm_build/stack/ulm a.out 42 theon$
If you find it inconvenient to specify the path to ULM for executing the program you can turn it into an executable shell script. The first line of an executable shell script contains after the Shebang the path to a program which receives the rest of the file as argument. So in the case above the first line should be
1 | #! ../ulm-generator/1_ulm_build/stack/ulm
|
With the two Unix commands
theon$ (echo '#! ../ulm-generator/1_ulm_build/stack/ulm'; cat a.out) > print_42 theon$ chmod +x print_42 theon$
a new file print_42 was created with the Shebang for the ULM in the first line followed by the content of a.out.
#! ../ulm-generator/1_ulm_build/stack/ulm
#TEXT 8
0x0000000000000000: 08 00 00 02 # ldzwq 0, %2
0x0000000000000004: 08 00 15 06 # ldzwq 21, %6
0x0000000000000008: 08 00 00 07 # ldzwq @w3(x), %7
0x000000000000000C: 15 00 00 07 # shldwq @w2(x), %7
0x0000000000000010: 15 00 00 07 # shldwq @w1(x), %7
0x0000000000000014: 15 01 38 07 # shldwq @w0(x), %7
0x0000000000000018: 19 06 00 07 # movq %6, (%7)
0x000000000000001C: 08 00 00 06 # ldzwq @w3(x), %6
0x0000000000000020: 15 00 00 06 # shldwq @w2(x), %6
0x0000000000000024: 15 00 00 06 # shldwq @w1(x), %6
0x0000000000000028: 15 01 38 06 # shldwq @w0(x), %6
0x000000000000002C: 12 06 00 06 # movq (%6), %6
0x0000000000000030: 0F 02 06 06 # imulq 2, %6, %6
0x0000000000000034: 08 00 00 07 # ldzwq @w3(x), %7
0x0000000000000038: 15 00 00 07 # shldwq @w2(x), %7
0x000000000000003C: 15 00 00 07 # shldwq @w1(x), %7
0x0000000000000040: 15 01 38 07 # shldwq @w0(x), %7
0x0000000000000044: 19 06 00 07 # movq %6, (%7)
0x0000000000000048: 08 00 00 06 # ldzwq @w3(x), %6
0x000000000000004C: 15 00 00 06 # shldwq @w2(x), %6
0x0000000000000050: 15 00 00 06 # shldwq @w1(x), %6
0x0000000000000054: 15 01 38 06 # shldwq @w0(x), %6
0x0000000000000058: 12 06 00 06 # movq (%6), %6
0x000000000000005C: 05 20 02 02 # subq 32, %2, %2
0x0000000000000060: 19 06 18 02 # movq %6, 24(%2)
0x0000000000000064: 08 00 00 04 # ldzwq @w3(print_uint64), %4
0x0000000000000068: 15 00 00 04 # shldwq @w2(print_uint64), %4
0x000000000000006C: 15 00 00 04 # shldwq @w1(print_uint64), %4
0x0000000000000070: 15 00 D0 04 # shldwq @w0(print_uint64), %4
0x0000000000000074: 14 04 03 00 # call %4, %3
0x0000000000000078: 0A 20 02 02 # addq 32, %2, %2
0x000000000000007C: 13 0A 00 00 # putc 10
0x0000000000000080: 01 00 00 00 # halt %0
0x0000000000000084: 19 03 00 02 # movq %RET_ADDR, ret(%SP)
0x0000000000000088: 19 01 08 02 # movq %FP, fp(%SP)
0x000000000000008C: 0A 00 02 01 # addq 0, %SP, %FP
0x0000000000000090: 05 10 02 02 # subq 8*2, %SP, %SP
0x0000000000000094: 0E 00 00 06 # movq %0, %dest
0x0000000000000098: 02 07 00 00 # getc %ch
0x000000000000009C: 05 30 07 00 # subq 48, %ch, %0
0x00000000000000A0: 0D 00 00 07 # jb .get_uint64.ret
0x00000000000000A4: 05 39 07 00 # subq 57, %ch, %0
0x00000000000000A8: 0C 00 00 05 # ja .get_uint64.ret
0x00000000000000AC: 05 30 07 07 # subq 48, %ch, %ch
0x00000000000000B0: 0F 0A 06 06 # imulq 10, %dest, %dest
0x00000000000000B4: 0E 07 06 06 # addq %ch, %dest, %dest
0x00000000000000B8: 04 FF FF F8 # jmp .get_uint64.read
0x00000000000000BC: 19 06 10 01 # movq %dest, rval(%FP)
0x00000000000000C0: 0E 01 00 02 # movq %FP, %SP
0x00000000000000C4: 12 02 08 01 # movq fp(%SP), %FP
0x00000000000000C8: 12 02 00 03 # movq ret(%SP), %RET_ADDR
0x00000000000000CC: 14 03 00 00 # jmp %RET_ADDR, %0
0x00000000000000D0: 19 03 00 02 # movq %RET_ADDR, ret(%SP)
0x00000000000000D4: 19 01 08 02 # movq %FP, fp(%SP)
0x00000000000000D8: 0A 00 02 01 # addq 0, %SP, %FP
0x00000000000000DC: 05 10 02 02 # subq 8*2, %SP, %SP
0x00000000000000E0: 16 00 14 06 # ldpa .print_uint64.pool, %buf
0x00000000000000E4: 17 06 00 06 # ldfp (%buf), %buf
0x00000000000000E8: 0E 06 00 09 # movq %buf, %p
0x00000000000000EC: 12 01 18 07 # movq fparam0(%FP), %val
0x00000000000000F0: 10 0A 07 07 # divq 10, %val, %val
0x00000000000000F4: 0A 30 08 08 # addq 48, %digit, %digit
0x00000000000000F8: 11 08 00 09 # movb %digit, (%p)
0x00000000000000FC: 0A 01 09 09 # addq 1, %p, %p
0x0000000000000100: 05 00 07 00 # subq 0, %val, %0
0x0000000000000104: 06 FF FF FB # jnz .print_uint64.get_digit
0x0000000000000108: 05 01 09 09 # subq 1, %p, %p
0x000000000000010C: 09 09 00 08 # movzbq (%p), %digit
0x0000000000000110: 03 08 00 00 # putc %digit
0x0000000000000114: 18 06 09 00 # subq %buf, %p, %0
0x0000000000000118: 06 FF FF FC # jnz .print_uint64.print_digit
0x000000000000011C: 0E 01 00 02 # movq %FP, %SP
0x0000000000000120: 12 02 08 01 # movq fp(%SP), %FP
0x0000000000000124: 12 02 00 03 # movq ret(%SP), %RET_ADDR
0x0000000000000128: 14 03 00 00 # jmp %RET_ADDR, %0
0x000000000000012C: 00 00 00 00 # for alignment
0x0000000000000130: 00 00 00 00
0x0000000000000134: 00 00 00 08 # .quad .print_uint64.buf
#DATA 1
#BSS 8 28
#SYMTAB
b x 0x0000000000000000
t print_uint64 0x00000000000000D0
a FP 0x0000000000000001
a SP 0x0000000000000002
a RET_ADDR 0x0000000000000003
a ret 0x0000000000000000
a fp 0x0000000000000008
a rval 0x0000000000000010
a fparam0 0x0000000000000018
t get_uint64 0x0000000000000084
a dest 0x0000000000000006
a ch 0x0000000000000007
t .get_uint64.read 0x0000000000000098
t .get_uint64.ret 0x00000000000000BC
b .print_uint64.buf 0x0000000000000008
a buf 0x0000000000000006
a val 0x0000000000000007
a digit 0x0000000000000008
a p 0x0000000000000009
t .print_uint64.pool 0x0000000000000130
t .print_uint64.get_digit 0x00000000000000F0
t .print_uint64.print_digit 0x0000000000000108
#FIXUPS
text 0x0000000000000130 0 64 absolute [bss]+8
text 0x0000000000000070 8 16 absolute @w0([text]+208)
text 0x000000000000006C 8 16 absolute @w1([text]+208)
text 0x0000000000000068 8 16 absolute @w2([text]+208)
text 0x0000000000000064 8 16 absolute @w3([text]+208)
text 0x0000000000000054 8 16 absolute @w0([bss]+312)
text 0x0000000000000050 8 16 absolute @w1([bss]+312)
text 0x000000000000004C 8 16 absolute @w2([bss]+312)
text 0x0000000000000048 8 16 absolute @w3([bss]+312)
text 0x0000000000000040 8 16 absolute @w0([bss]+312)
text 0x000000000000003C 8 16 absolute @w1([bss]+312)
text 0x0000000000000038 8 16 absolute @w2([bss]+312)
text 0x0000000000000034 8 16 absolute @w3([bss]+312)
text 0x0000000000000028 8 16 absolute @w0([bss]+312)
text 0x0000000000000024 8 16 absolute @w1([bss]+312)
text 0x0000000000000020 8 16 absolute @w2([bss]+312)
text 0x000000000000001C 8 16 absolute @w3([bss]+312)
text 0x0000000000000014 8 16 absolute @w0([bss]+312)
text 0x0000000000000010 8 16 absolute @w1([bss]+312)
text 0x000000000000000C 8 16 absolute @w2([bss]+312)
text 0x0000000000000008 8 16 absolute @w3([bss]+312)
With chmod this file was made executable:
theon$ ./print_42 42 theon$
Of course the creation of an executable can be automatized with a makefile.
Example Directory and Makefile
Create a subdirectory and save there following files (make sure the makefile has poper tabs):
ABC := ../xtest_abc
ulm.path := $(patsubst %/,%,$(shell cat "path-to-ulm"))
AS := $(ulm.path)/ulmas
ULM := $(ulm.path)/ulm
io.hack.s := getuint64.s printuint64.s
example.src := $(wildcard *.abc)
example.s := $(patsubst %.abc,%.s,$(example.src))
example := $(patsubst %.abc,%,$(example.src))
all: $(example)
%.s : %.abc
$(ABC) $@ < $^
% : %.s
$(AS) $^ $(io.hack.s)
(echo "#! $(ULM)"; cat a.out) > $@
$(RM) a.out
chmod +x $@
clean:
$(RM) $(example)
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 | .equ FP, 1
.equ SP, 2
.equ RET_ADDR, 3
.equ ret, 0
.equ fp, ret + 8
.equ rval, fp + 8
.equ fparam0, rval + 8
// function: %RET_VAL get_uint64()
.text
get_uint64:
// prologue
movq %RET_ADDR, ret(%SP)
movq %FP, fp(%SP)
addq 0, %SP, %FP
subq 8 * 2, %SP, %SP
.equ dest, 6
.equ ch, dest + 1
movq %0, %dest
.get_uint64.read:
getc %ch
# if %ch < '0' then we are done
subq '0', %ch, %0
jb .get_uint64.ret
# if %ch > '9' then we are done
subq '9', %ch, %0
ja .get_uint64.ret
subq '0', %ch, %ch
imulq 10, %dest, %dest
addq %ch, %dest, %dest
jmp .get_uint64.read
.get_uint64.ret:
movq %dest, rval(%FP)
// epilogue
movq %FP, %SP
movq fp(%SP), %FP
movq ret(%SP), %RET_ADDR
jmp %RET_ADDR, %0
|
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 | .equ FP, 1
.equ SP, 2
.equ RET_ADDR, 3
.equ ret, 0
.equ fp, ret + 8
.equ rval, fp + 8
.equ fparam0, rval + 8
// function: void print_uint64(val)
// static char[20] buf;
.bss
.print_uint64.buf:
.space 20
.text
print_uint64:
// prologue
movq %RET_ADDR, ret(%SP)
movq %FP, fp(%SP)
addq 0, %SP, %FP
subq 8 * 2, %SP, %SP
.equ buf, 6
.equ val, buf + 1
.equ digit, val + 1
.equ p, digit + 1
ldpa .print_uint64.pool, %buf
ldfp (%buf), %buf
movq %buf, %p
movq fparam0(%FP), %val
.print_uint64.get_digit:
divq 10, %val, %val
addq '0', %digit, %digit
movb %digit, (%p)
addq 1, %p, %p
subq 0, %val, %0
jnz .print_uint64.get_digit
.print_uint64.print_digit:
subq 1, %p, %p
movzbq (%p), %digit
putc %digit
subq %buf, %p, %0
jnz .print_uint64.print_digit
// epilogue
movq %FP, %SP
movq fp(%SP), %FP
movq ret(%SP), %RET_ADDR
jmp %RET_ADDR, %0
.align 8
.print_uint64.pool:
.quad .print_uint64.buf
|
$> x;
$< x * x;
x = x + 42;
$< x;
$< y;
Create an addition file “path-to-ulm” that contains the path to your ULM assembler and the virtual machine. For example
1 | $HOME/tmp/ulm-generator/1_ulm_build/stack/
|
or
1 | ../../ulm-generator/1_ulm_build/stack/
|
Then with make all files with the extension '.abc' will be translated into executables:
theon$ make ../xtest_abc fold_example.s < fold_example.abc ../../ulm-generator/1_ulm_build/stack/ulmas fold_example.s getuint64.s printuint64.s (echo "#! ../../ulm-generator/1_ulm_build/stack/ulm"; cat a.out) > fold_example rm -f a.out chmod +x fold_example ../xtest_abc test.s < test.abc ../../ulm-generator/1_ulm_build/stack/ulmas test.s getuint64.s printuint64.s (echo "#! ../../ulm-generator/1_ulm_build/stack/ulm"; cat a.out) > test rm -f a.out chmod +x test rm fold_example.s test.s theon$ echo 4 | ./test 16 46 0 theon$