remove errno

This commit is contained in:
Igor Malovitsa 2024-03-05 16:17:19 +02:00
parent 944c224d30
commit c70f2d9f2a
2 changed files with 49 additions and 47 deletions

View file

@ -4,7 +4,7 @@
; .origin 0x1000 ; .origin 0x1000
; .align 0x100 ; .align 0x100
; .db "hello" ; .db "hello"
; .stuct ; .struct
start: start:
jmp end jmp end
un un

94
hbas.c
View file

@ -23,7 +23,6 @@ SOFTWARE.
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
void hd(char *data, size_t len) { void hd(char *data, size_t len) {
for (size_t ii = 0; ii < len; ii += 1) { for (size_t ii = 0; ii < len; ii += 1) {
@ -304,23 +303,62 @@ size_t inst_lookup(InstHt ht, const char *s, size_t len) {
return INVALID; return INVALID;
} }
typedef enum AsmError_e {
ErrOk = 0,
ErrBadRegister,
ErrImmediateOverflow,
ErrInvalidToken,
ErrBadArgumentMeta,
ErrNeedCommaAfterArgument,
ErrLabelImmediate,
ErrNumberImmediate,
ErrBadNumOverflow,
ErrBadNumDigit,
ErrBadNumNoDigit,
ErrLabelAfterLabel,
ErrOutOfMemory,
ErrDuplicateLabel,
ErrTrailingLine,
ErrNeedDirectiveAfterDot,
ErrDirectiveNotImplemented,
} AsmError;
char *ERRORS[] = {
"Success",
"Bad register name",
"Immediate integer OR relative offset overflow",
"Invalid token",
"Bad argument char? (blame developer of this program)",
"Expected comma after the argument, got something else",
"Label immediate needs label or number",
"Immediate needs to be a number",
"Bad number: u64 overflow",
"Bad number: encountered bad gidit",
"Bad number: no digits presented after the suffix",
"Encountered label after label",
"Out of Memory",
"Duplicate label",
"Encountered trailing identifier after instruction",
"Expected directive after dot",
"Directive is not implemented",
};
typedef struct ByteVec_s { typedef struct ByteVec_s {
char *buf; char *buf;
size_t cap; size_t cap;
size_t len; size_t len;
} ByteVec; } ByteVec;
int ensure_push(ByteVec *vec, size_t el_size, size_t extra) { AsmError ensure_push(ByteVec *vec, size_t el_size, size_t extra) {
while (vec->len + extra > vec->cap) { while (vec->len + extra > vec->cap) {
vec->cap *= 2; vec->cap *= 2;
// multiply overflow // multiply overflow
if ((~(size_t)0) / el_size < vec->cap) { if ((~(size_t)0) / el_size < vec->cap) {
return ENOMEM; return ErrOutOfMemory;
} }
vec->buf = realloc(vec->buf, el_size * vec->cap); vec->buf = realloc(vec->buf, el_size * vec->cap);
if (vec->buf == NULL) { if (vec->buf == NULL) {
vec->cap = 0; vec->cap = 0;
return ENOMEM; return ErrOutOfMemory;
} }
} }
return 0; return 0;
@ -334,12 +372,12 @@ int slurp(FILE *fd, ByteVec *out) {
int err = 0; int err = 0;
if (rv.buf == NULL) { if (rv.buf == NULL) {
rv.cap = 0; rv.cap = 0;
err = ENOMEM; err = ErrOutOfMemory;
bread = 0; bread = 0;
} }
while (bread > 0) { while (bread > 0) {
if (ensure_push(&rv, 1, 1) != 0) { if (ensure_push(&rv, 1, 1) != 0) {
err = ENOMEM; err = ErrOutOfMemory;
break; break;
} }
bread = fread(&rv.buf[rv.len], 1, rv.cap - rv.len, fd); bread = fread(&rv.buf[rv.len], 1, rv.cap - rv.len, fd);
@ -369,42 +407,6 @@ typedef struct Token_s {
size_t len; size_t len;
uint64_t num; uint64_t num;
} Token; } Token;
typedef enum AsmError_e {
ErrOk = 0,
ErrBadRegister,
ErrImmediateOverflow,
ErrInvalidToken,
ErrBadArgumentMeta,
ErrNeedCommaAfterArgument,
ErrLabelImmediate,
ErrNumberImmediate,
ErrBadNumOverflow,
ErrBadNumDigit,
ErrBadNumNoDigit,
ErrLabelAfterLabel,
ErrDuplicateLabel,
ErrTrailingLine,
ErrNeedDirectiveAfterDot,
ErrDirectiveNotImplemented,
} AsmError;
char *ERRORS[] = {
"Success",
"Bad register name",
"Immediate integer OR relative offset overflow",
"Invalid token",
"Bad argument char? (blame developer of this program)",
"Expected comma after the argument, got something else",
"Label immediate needs label or number",
"Immediate needs to be a number",
"Bad number: u64 overflow",
"Bad number: encountered bad gidit",
"Bad number: no digits presented after the suffix",
"Encountered label after label",
"Duplicate label",
"Encountered trailing identifier after instruction",
"Expected directive after dot",
"Directive is not implemented",
};
Token token_ident(char *input, size_t len, size_t pos) { Token token_ident(char *input, size_t len, size_t pos) {
size_t start = pos; size_t start = pos;
@ -637,7 +639,7 @@ AsmError assemble_instr(
size += meta.size; size += meta.size;
} }
if (ensure_push(rv, 1, size) != 0) { if (ensure_push(rv, 1, size) != 0) {
return ENOMEM; return ErrOutOfMemory;
} }
rv->buf[rv->len] = inst->opcode; rv->buf[rv->len] = inst->opcode;
rv->len += 1; rv->len += 1;
@ -674,7 +676,7 @@ AsmError assemble_instr(
size_t idx = label_lookup(labels, &input[tok->start], tok->len); size_t idx = label_lookup(labels, &input[tok->start], tok->len);
if (idx == INVALID) { if (idx == INVALID) {
if (ensure_push((ByteVec*)holes, 1, sizeof(Hole)) != 0) { if (ensure_push((ByteVec*)holes, 1, sizeof(Hole)) != 0) {
return ENOMEM; return ErrOutOfMemory;
} }
holes->buf[holes->len] = (Hole) { holes->buf[holes->len] = (Hole) {
.location = rv->len, .location = rv->len,
@ -783,7 +785,7 @@ AsmError assemble(InstHt ht, char *input, size_t len, ByteVec *out, EInfo *einfo
} }
line_state = 1; line_state = 1;
if (ensure_push((ByteVec*)&labels, sizeof(Label), 1) != 0) { if (ensure_push((ByteVec*)&labels, sizeof(Label), 1) != 0) {
err = ENOMEM; err = ErrOutOfMemory;
goto end; goto end;
} }
size_t idx = label_lookup(&labels, &input[tok.start], tok.len); size_t idx = label_lookup(&labels, &input[tok.start], tok.len);
@ -860,7 +862,7 @@ int main(int argc, char **argv) {
} }
ht = build_lookup(); ht = build_lookup();
if (ht == NULL) { if (ht == NULL) {
err = ENOMEM; err = ErrOutOfMemory;
fprintf(stderr, "failed to init hash table: %d\n", err); fprintf(stderr, "failed to init hash table: %d\n", err);
goto done; goto done;
} }