diff options
author | Johnny Richard <johnny@johnnyrichard.com> | 2023-04-15 03:07:21 +0200 |
---|---|---|
committer | Johnny Richard <johnny@johnnyrichard.com> | 2023-04-15 03:07:21 +0200 |
commit | bf2c6f765db1b349d5bd5ce98631144f9cab654a (patch) | |
tree | f6c8e142f9324c2663e96cac469b9a1bc31621b2 /src | |
parent | 7720f00ffdb6fb23cec263d6aef67f54fd698d95 (diff) |
parser: Generate GAS 64-bit assembly for linux
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/pipac.c | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/src/pipac.c b/src/pipac.c index 29c21bd..993d56d 100644 --- a/src/pipac.c +++ b/src/pipac.c @@ -16,10 +16,28 @@ */ #include <stdio.h> #include <stdlib.h> +#include <string.h> + #include "lexer.h" #include "parser.h" void +generate_gas_x86_64_linux(ast_function_t *func) +{ + if (strcmp(func->name, "main") != 0) { + fprintf(stderr, "[ERROR]: no main function has been defined!\n"); + exit(EXIT_FAILURE); + } + + printf(".global _start\n"); + printf(".text\n"); + printf("_start:\n"); + printf(" mov $1, %%al\n"); + printf(" mov $%d, %%ebx\n", func->body.number); + printf(" int $0x80\n"); +} + +void print_usage() { fputs("pipac <filename.pipa>\n", stderr); @@ -50,7 +68,7 @@ main(int argc, char **argv) parser_init(&parser, &lexer); ast_function_t func = parser_parse_function(&parser); - printf("function name='%s' and return type '%d' with value %d\n", func.name, func.return_type, func.body.number); + generate_gas_x86_64_linux(&func); return EXIT_SUCCESS; } |