52 lines
1.4 KiB
C
52 lines
1.4 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include "a64instruction/a64instruction.h"
|
|
#include "parser.h"
|
|
#include "fileio.h"
|
|
#include "parser.h"
|
|
#include "encode.c"
|
|
|
|
static symbol_table *firstPass(a64inst_instruction *instructions, int lineCount);
|
|
|
|
int main(int argc, char **argv) {
|
|
// Check the arguments
|
|
if (argc < 3) {
|
|
fprintf(stderr, "Error: A source file and an object output file are required. Syntax: ./assemble <file_in> <file_out>");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
// Load the source file into memory
|
|
int lineCount = countLines(argv[1]);
|
|
char **source = readAssemblyFile(argv[1], lineCount);
|
|
|
|
// Parse the source file
|
|
a64inst_instruction *instructions = parse(source, lineCount);
|
|
|
|
// First Pass: Create the symbol table
|
|
symbol_table *table = firstPass(instructions, lineCount);
|
|
|
|
// Second Pass: Encode the instructions into binary
|
|
word *binary = encode(instructions, lineCount, table);
|
|
|
|
// Write the binary to the output file
|
|
writeBinaryFile(binary, argv[2], lineCount);
|
|
|
|
/* TODO: FREE MEMORY!! */
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
static symbol_table *firstPass(a64inst_instruction *instructions, int lineCount) {
|
|
symbol_table *table = st_init();
|
|
int labelCount = 0;
|
|
|
|
for (int i = 0; i < lineCount; i++) {
|
|
a64inst_instruction inst = instructions[i];
|
|
if (inst.type == a64inst_LABEL) {
|
|
st_insert(table, inst.data.LabelData.label, 4 * (i - (labelCount++)));
|
|
}
|
|
}
|
|
|
|
return table;
|
|
}
|