forked from rui314/chibicc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
as.c
74 lines (64 loc) · 1.48 KB
/
as.c
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
#include <cbm.h>
#include <elf.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "as-tokenize.h"
#include "std.h"
static Elf32_Ehdr elf = {
{ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, ELFCLASS32, ELFDATA2LSB, EV_CURRENT,
ELFOSABI_NONE, /*EI_ABIVERSION=*/0},
ET_REL,
EM_MOS,
EV_CURRENT,
/*e_entry=*/0,
/*e_phoff=*/0,
/*e_shoff=*/0,
EF_MOS_ARCH_6502 | EF_MOS_ARCH_6502_BCD | EF_MOS_ARCH_65C02 |
EF_MOS_ARCH_R65C02 | EF_MOS_ARCH_W65C02,
/*e_ehsize=*/sizeof(Elf32_Ehdr),
/*e_phentsize=*/0,
/*e_phnum=*/0,
/*e_shentsize=*/0,
/*e_shnum=*/0,
/*e_shstrndx=*/SHN_UNDEF,
};
static void error(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "error: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
static bool read_stmt(char *stmt, size_t max, FILE* f) {
if (feof(f))
return false;
while (max--) {
int c = fgetc(f);
if (c == EOF)
error("file did not end with newline");
*stmt++ = c;
if (c == '\n') {
if (!max)
break;
*stmt = '\0';
return true;
}
}
error("line too long");
}
int main(void) {
putchar(0x0f); // Enable ISO mode
FILE *input_file = fopen("a.s", "r");
char stmt[160];
while (read_stmt(stmt, sizeof(stmt), input_file)) {
Token *tok = tokenize(stmt);
}
FILE *elf_file = fopen("a.out", "w");
fwrite(&elf, sizeof(elf), 1, elf_file);
fclose(elf_file);
cbm_k_clrch();
return 0;
}