/*
* Copyright (C) 2023 Johnny Richard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include
#include
#include
#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 \n", stderr);
}
void
print_tokens(lexer_t *lexer) {
token_t token;
for (lexer_next_token(lexer, &token); token.kind != TOKEN_EOF; lexer_next_token(lexer, &token)) {
printf("%s:%d:%d: [kind=%d, value='%s']\n", lexer->filepath, token.row + 1, token.col + 1, token.kind, token.value);
}
}
int
main(int argc, char **argv)
{
if (argc < 2) {
print_usage();
return EXIT_FAILURE;
}
char *filepath = argv[1];
lexer_t lexer;
lexer_init(&lexer, filepath);
parser_t parser;
parser_init(&parser, &lexer);
ast_function_t func = parser_parse_function(&parser);
generate_gas_x86_64_linux(&func);
return EXIT_SUCCESS;
}