2024-03-04 14:40:50 -06:00
|
|
|
/*
|
|
|
|
Copyright (c) 2024 Igor null <m1el.2027@gmail.com>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2024-03-04 14:24:03 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-03-07 10:05:17 -06:00
|
|
|
#include <stdint.h>
|
2024-03-04 14:24:03 -06:00
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
#include "op.h"
|
2024-03-07 08:35:44 -06:00
|
|
|
#include "error.h"
|
2024-03-07 08:09:18 -06:00
|
|
|
#include "instructions.c"
|
|
|
|
#include "hash.c"
|
|
|
|
|
|
|
|
void hd(char *data, size_t len)
|
|
|
|
{
|
|
|
|
for (size_t ii = 0; ii < len; ii += 1)
|
|
|
|
{
|
|
|
|
if (ii > 0 && (ii & 15) == 0)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
printf("%02x", (uint8_t)data[ii]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
typedef struct ArgMeta_s
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
char chr;
|
|
|
|
uint8_t size;
|
|
|
|
// This is a bitset of acceptable overflow states,
|
|
|
|
// where accept signed = 1, accept unsigned = 2.
|
|
|
|
// 1 -> signed, 2 -> unsigned, 3 -> whatever
|
|
|
|
uint8_t sign;
|
|
|
|
uint8_t rel;
|
|
|
|
} ArgMeta;
|
|
|
|
const ArgMeta ARGS[] = {
|
2024-03-07 08:09:18 -06:00
|
|
|
{'R', 1, 2, 0},
|
|
|
|
{'1', 1, 3, 0},
|
|
|
|
{'b', 1, 1, 0},
|
|
|
|
{'B', 1, 2, 0},
|
|
|
|
{'2', 2, 3, 0},
|
|
|
|
{'o', 2, 1, 1},
|
|
|
|
{'h', 2, 1, 0},
|
|
|
|
{'H', 2, 2, 0},
|
|
|
|
{'4', 4, 3, 0},
|
|
|
|
{'w', 4, 1, 0},
|
|
|
|
{'O', 4, 1, 1},
|
|
|
|
{'W', 4, 2, 0},
|
|
|
|
{'8', 8, 3, 0},
|
|
|
|
{'d', 8, 1, 0},
|
|
|
|
{'D', 8, 2, 0},
|
|
|
|
{0},
|
2024-03-04 14:24:03 -06:00
|
|
|
};
|
|
|
|
const size_t NARGS = sizeof(ARGS) / sizeof(ARGS[0]);
|
2024-03-07 08:09:18 -06:00
|
|
|
ArgMeta arg_meta(char arg)
|
|
|
|
{
|
|
|
|
for (size_t ii = 0; ii < NARGS; ii += 1)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
ArgMeta meta = ARGS[ii];
|
2024-03-07 08:09:18 -06:00
|
|
|
if (meta.chr == arg)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
return ARGS[NARGS - 1];
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
typedef struct ByteVec_s
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
char *buf;
|
|
|
|
size_t cap;
|
|
|
|
size_t len;
|
|
|
|
} ByteVec;
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
AsmError ensure_push(ByteVec *vec, size_t el_size, size_t extra)
|
|
|
|
{
|
|
|
|
if (vec->len + extra < vec->len)
|
|
|
|
{
|
2024-03-06 13:05:21 -06:00
|
|
|
return ErrOutOfMemory;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
while (vec->len + extra > vec->cap)
|
|
|
|
{
|
|
|
|
if ((~(size_t)0) / 2 < vec->cap)
|
|
|
|
{
|
2024-03-06 13:05:21 -06:00
|
|
|
return ErrOutOfMemory;
|
|
|
|
}
|
2024-03-04 14:24:03 -06:00
|
|
|
vec->cap *= 2;
|
|
|
|
// multiply overflow
|
2024-03-07 08:09:18 -06:00
|
|
|
if ((~(size_t)0) / el_size < vec->cap)
|
|
|
|
{
|
2024-03-05 08:17:19 -06:00
|
|
|
return ErrOutOfMemory;
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
|
|
|
vec->buf = realloc(vec->buf, el_size * vec->cap);
|
2024-03-07 08:09:18 -06:00
|
|
|
if (vec->buf == NULL)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
vec->cap = 0;
|
2024-03-05 08:17:19 -06:00
|
|
|
return ErrOutOfMemory;
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MIN_SIZE 4096
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
int slurp(FILE *fd, ByteVec *out)
|
|
|
|
{
|
|
|
|
ByteVec rv = {malloc(MIN_SIZE), MIN_SIZE, 0};
|
2024-03-04 14:24:03 -06:00
|
|
|
size_t bread = 1;
|
|
|
|
int err = 0;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (rv.buf == NULL)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
rv.cap = 0;
|
2024-03-05 08:17:19 -06:00
|
|
|
err = ErrOutOfMemory;
|
2024-03-04 14:24:03 -06:00
|
|
|
bread = 0;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
while (bread > 0)
|
|
|
|
{
|
|
|
|
if (ensure_push(&rv, 1, 1) != 0)
|
|
|
|
{
|
2024-03-05 08:17:19 -06:00
|
|
|
err = ErrOutOfMemory;
|
2024-03-04 14:24:03 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
bread = fread(&rv.buf[rv.len], 1, rv.cap - rv.len, fd);
|
|
|
|
rv.len += bread;
|
|
|
|
}
|
|
|
|
*out = rv;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (err == 0)
|
|
|
|
{
|
2024-03-05 14:19:50 -06:00
|
|
|
err = ferror(fd);
|
|
|
|
}
|
2024-03-04 14:24:03 -06:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
typedef enum TokenKind_e
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
TokInvalid = '!',
|
|
|
|
TokEOF = '$',
|
|
|
|
TokIdent = 'A',
|
|
|
|
TokNeg = '-',
|
|
|
|
TokNumber = '0',
|
|
|
|
TokBadNumber = '9',
|
|
|
|
TokComma = ',',
|
|
|
|
TokDot = '.',
|
|
|
|
TokColon = ':',
|
|
|
|
TokComment = ';',
|
|
|
|
TokNewline = 'n',
|
|
|
|
} TokenKind;
|
2024-03-07 08:09:18 -06:00
|
|
|
typedef struct Token_s
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
TokenKind kind;
|
|
|
|
size_t start;
|
|
|
|
size_t len;
|
|
|
|
uint64_t num;
|
|
|
|
} Token;
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
Token token_ident(char *input, size_t len, size_t pos)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
size_t start = pos;
|
2024-03-07 08:09:18 -06:00
|
|
|
while (pos < len)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
char chr = input[pos];
|
|
|
|
char chru = chr & ~0x20;
|
2024-03-07 08:09:18 -06:00
|
|
|
int good = chr == '_' || (chr >= '0' && chr <= '9') || (chru >= 'A' && chru <= 'Z');
|
|
|
|
if (!good)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos += 1;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
return (Token){TokIdent, start, pos - start, 0};
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
Token token_number(char *input, size_t len, size_t pos)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
char *ptr = &input[pos];
|
|
|
|
char next = '\0';
|
|
|
|
size_t start = pos;
|
|
|
|
size_t digits = 0;
|
|
|
|
uint64_t base = 10;
|
|
|
|
uint64_t rv = 0;
|
|
|
|
uint64_t pre_overflow;
|
|
|
|
AsmError bad_num = ErrOk;
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
if (pos + 1 < len)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
next = ptr[1] & ~0x20;
|
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
if (input[pos] == '0')
|
|
|
|
{
|
|
|
|
if (next == 'X')
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
base = 16;
|
|
|
|
pos += 2;
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else if (next == 'D')
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
base = 10;
|
|
|
|
pos += 2;
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else if (next == 'O')
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
base = 8;
|
|
|
|
pos += 2;
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else if (next == 'B')
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
base = 2;
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pre_overflow = (~(size_t)0) / base;
|
|
|
|
// valid: "0x_0", "0_"
|
|
|
|
// invalid: "0x_"
|
2024-03-07 08:09:18 -06:00
|
|
|
while (pos < len)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
uint64_t digit;
|
|
|
|
uint64_t next;
|
|
|
|
char chr = input[pos];
|
|
|
|
char chru = chr & ~0x20;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (chr == '_')
|
|
|
|
{
|
|
|
|
pos += 1;
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-04 14:24:03 -06:00
|
|
|
digit = (uint64_t)chr - (uint64_t)'0';
|
2024-03-07 08:09:18 -06:00
|
|
|
if (digit >= 10)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
digit = (uint64_t)chru - (uint64_t)('A' - 10);
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (digit >= base)
|
|
|
|
{
|
|
|
|
if (chr >= '0' && chr <= '9')
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
bad_num = ErrBadNumDigit;
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else if (chru >= 'A' && chru <= 'Z')
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
bad_num = ErrBadNumDigit;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += 1;
|
|
|
|
digits += 1;
|
|
|
|
|
|
|
|
next = rv * base + digit;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (rv > pre_overflow || next < rv)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
bad_num = ErrBadNumOverflow;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rv = next;
|
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
if (digits == 0)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
bad_num = ErrBadNumNoDigit;
|
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
if (bad_num)
|
|
|
|
{
|
|
|
|
return (Token){TokBadNumber, start, pos - start, bad_num};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (Token){TokNumber, start, pos - start, rv};
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
Token token(char *input, size_t len, size_t pos)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
char chr, chru;
|
|
|
|
char *ptr = &input[pos];
|
2024-03-07 08:09:18 -06:00
|
|
|
while (pos < len && (input[pos] == ' ' || input[pos] == '\t'))
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
pos += 1;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (pos == len)
|
|
|
|
{
|
|
|
|
return (Token){TokEOF, pos, 0, 0};
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
|
|
|
ptr = &input[pos];
|
|
|
|
chr = *ptr;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (chr == ',' || chr == '-' || chr == '.' || chr == ':')
|
|
|
|
{
|
|
|
|
return (Token){(TokenKind)chr, pos, 1, 0};
|
|
|
|
}
|
|
|
|
if (chr == '\n')
|
|
|
|
{
|
|
|
|
return (Token){TokNewline, pos, 1, 0};
|
|
|
|
}
|
|
|
|
if (chr == '\r')
|
|
|
|
{
|
|
|
|
if (pos + 1 < len && ptr[1] == '\n')
|
|
|
|
{
|
|
|
|
return (Token){TokNewline, pos, 2, 0};
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
return (Token){TokNewline, pos, 1, 0};
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (chr == ';')
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
size_t clen = 1;
|
2024-03-07 08:09:18 -06:00
|
|
|
while (pos + clen < len && ptr[clen] != '\n' && ptr[clen] != '\r')
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
clen += 1;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
return (Token){TokComment, pos, clen, 0};
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (chr >= '0' && chr <= '9')
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return token_number(input, len, pos);
|
|
|
|
}
|
|
|
|
chru = chr & ~0x20;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (chr == '_' || (chru >= 'A' && chru <= 'Z'))
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return token_ident(input, len, pos);
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
return (Token){TokInvalid, pos, 1, 0};
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
typedef struct Hole_s
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
size_t location;
|
|
|
|
size_t origin;
|
|
|
|
char *str;
|
|
|
|
size_t len;
|
|
|
|
size_t size;
|
|
|
|
} Hole;
|
2024-03-07 08:09:18 -06:00
|
|
|
typedef struct HoleVec_s
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
Hole *buf;
|
|
|
|
size_t cap;
|
|
|
|
size_t len;
|
|
|
|
} HoleVec;
|
2024-03-07 08:09:18 -06:00
|
|
|
typedef struct Label_s
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
size_t location;
|
|
|
|
char *str;
|
|
|
|
size_t len;
|
|
|
|
} Label;
|
2024-03-07 08:09:18 -06:00
|
|
|
typedef struct LabelVec_s
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
Label *buf;
|
|
|
|
size_t cap;
|
|
|
|
size_t len;
|
|
|
|
} LabelVec;
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
size_t label_lookup(LabelVec *labels, char *name, size_t len)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
size_t nlabels = labels->len;
|
|
|
|
Label *buf = labels->buf;
|
2024-03-07 08:09:18 -06:00
|
|
|
for (size_t ii = 0; ii < nlabels; ii += 1)
|
|
|
|
{
|
|
|
|
if (len == buf->len && strncmp(buf->str, name, len) == 0)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return ii;
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
2024-03-04 14:24:03 -06:00
|
|
|
buf += 1;
|
|
|
|
}
|
|
|
|
return INVALID;
|
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
int parse_register(char *name, size_t len)
|
|
|
|
{
|
|
|
|
if (name[0] != 'r')
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return 256; // Register name should start with 'r'
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (len > 4)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return 256; // Register name too long
|
|
|
|
}
|
|
|
|
uint16_t rv = 0;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (len > 2 && name[1] == '0')
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return 256; // Extra zero suffix
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
for (size_t ii = 1; ii < len; ii += 1)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
char chr = name[ii];
|
2024-03-07 08:09:18 -06:00
|
|
|
if (!(chr >= '0' && chr <= '9'))
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return 256; // Register name must only contain numbers
|
|
|
|
}
|
|
|
|
rv = rv * 10 + (chr - '0');
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (rv > 255)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return 256; // Register number too large
|
|
|
|
}
|
|
|
|
return (int)rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// safety: assumes the buffer has enough place for specified integer size
|
2024-03-07 08:09:18 -06:00
|
|
|
AsmError push_int_le(char *buf, uint64_t val, size_t size, uint8_t sign)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
int valid_uint = val >> (size * 8) == 0;
|
|
|
|
int64_t int_shifted = ((int64_t)val) >> (size * 8 - 1);
|
|
|
|
int valid_int = int_shifted == 0 || (~int_shifted) == 0;
|
|
|
|
// Note: this assumes the format for `sign` is a bitset.
|
|
|
|
int validity = valid_int | (valid_uint << 1);
|
2024-03-07 08:09:18 -06:00
|
|
|
if ((validity & sign) == 0)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return ErrImmediateOverflow;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
for (size_t ii = 0; ii < size; ii += 1)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
buf[ii] = val & 0xff;
|
|
|
|
val >>= 8;
|
|
|
|
}
|
|
|
|
return ErrOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
AsmError assemble_instr(
|
|
|
|
InstHt ht, char *input, size_t len, Token *tok,
|
2024-03-07 08:09:18 -06:00
|
|
|
ByteVec *rv, HoleVec *holes, LabelVec *labels)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
const InstDesc *inst;
|
|
|
|
const char *type_str;
|
|
|
|
size_t nargs;
|
|
|
|
size_t size;
|
|
|
|
size_t idx = inst_lookup(ht, &input[tok->start], tok->len);
|
|
|
|
size_t inst_start = rv->len;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (idx == INVALID)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return ErrInvalidToken;
|
|
|
|
}
|
|
|
|
inst = &INST[idx];
|
|
|
|
type_str = TYPE_STR[inst->type];
|
|
|
|
nargs = strlen(type_str);
|
|
|
|
size = 1;
|
2024-03-07 08:09:18 -06:00
|
|
|
for (size_t ii = 0; ii < nargs; ii += 1)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
char chr = type_str[ii];
|
|
|
|
ArgMeta meta = arg_meta(chr);
|
2024-03-07 08:09:18 -06:00
|
|
|
if (meta.chr == 0)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return ErrBadArgumentMeta;
|
|
|
|
}
|
|
|
|
size += meta.size;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (ensure_push(rv, 1, size) != 0)
|
|
|
|
{
|
2024-03-05 08:17:19 -06:00
|
|
|
return ErrOutOfMemory;
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
|
|
|
rv->buf[rv->len] = inst->opcode;
|
|
|
|
rv->len += 1;
|
2024-03-07 08:09:18 -06:00
|
|
|
for (size_t ii = 0; ii < nargs; ii += 1)
|
|
|
|
{
|
|
|
|
if (ii > 0)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
*tok = token(input, len, tok->start + tok->len);
|
2024-03-07 08:09:18 -06:00
|
|
|
if (tok->kind != TokComma)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return ErrNeedCommaAfterArgument;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
char chr = type_str[ii];
|
|
|
|
ArgMeta meta = arg_meta(chr);
|
|
|
|
uint64_t is_negative = 0;
|
|
|
|
*tok = token(input, len, tok->start + tok->len);
|
2024-03-07 08:09:18 -06:00
|
|
|
if (tok->kind == TokNeg)
|
|
|
|
{
|
2024-03-06 02:59:17 -06:00
|
|
|
*tok = token(input, len, tok->start + tok->len);
|
|
|
|
is_negative = ~(uint64_t)0;
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (chr == 'R')
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
int reg = parse_register(&input[tok->start], tok->len);
|
2024-03-07 08:09:18 -06:00
|
|
|
if (reg > 255)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return ErrBadRegister;
|
|
|
|
}
|
|
|
|
rv->buf[rv->len] = (char)(reg & 0xff);
|
|
|
|
rv->len += 1;
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
uint64_t num_to_write;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (meta.rel == 1 || meta.size == 8)
|
|
|
|
{
|
|
|
|
if (tok->kind == TokIdent)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
size_t idx = label_lookup(labels, &input[tok->start], tok->len);
|
2024-03-07 08:09:18 -06:00
|
|
|
if (idx == INVALID)
|
|
|
|
{
|
|
|
|
if (ensure_push((ByteVec *)holes, 1, sizeof(Hole)) != 0)
|
|
|
|
{
|
2024-03-05 08:17:19 -06:00
|
|
|
return ErrOutOfMemory;
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
holes->buf[holes->len] = (Hole){
|
2024-03-04 14:24:03 -06:00
|
|
|
.location = rv->len,
|
|
|
|
.origin = inst_start,
|
|
|
|
.str = &input[tok->start],
|
|
|
|
.len = tok->len,
|
|
|
|
.size = (size_t)meta.size,
|
|
|
|
};
|
|
|
|
holes->len += 1;
|
|
|
|
num_to_write = 0;
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
num_to_write = labels->buf[idx].location;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (meta.size != 8)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
num_to_write -= inst_start;
|
|
|
|
}
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else if (tok->kind == TokNumber)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
num_to_write = tok->num;
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return ErrLabelImmediate;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else if (tok->kind == TokNumber)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
num_to_write = tok->num;
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return ErrNumberImmediate;
|
|
|
|
}
|
|
|
|
// num_to_write = num_to_write ^ is_negative - is_negative;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (is_negative)
|
|
|
|
{
|
2024-03-06 13:05:21 -06:00
|
|
|
int64_t tmp = -(int64_t)num_to_write;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (tmp > 0)
|
|
|
|
{
|
2024-03-06 02:59:17 -06:00
|
|
|
return ErrBadNumOverflow;
|
|
|
|
}
|
2024-03-06 13:05:21 -06:00
|
|
|
num_to_write = (uint64_t)tmp;
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
|
|
|
AsmError err = push_int_le(
|
2024-03-07 08:09:18 -06:00
|
|
|
&rv->buf[rv->len], num_to_write, meta.size, meta.sign);
|
|
|
|
if (err != 0)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
rv->len += meta.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
typedef struct EInfo_s
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
Token token;
|
|
|
|
size_t line;
|
|
|
|
size_t line_start;
|
|
|
|
} EInfo;
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
AsmError assemble(InstHt ht, char *input, size_t len, ByteVec *out, EInfo *einfo)
|
|
|
|
{
|
|
|
|
ByteVec rv = {malloc(MIN_SIZE), MIN_SIZE, 0};
|
|
|
|
HoleVec holes = {malloc(MIN_SIZE * sizeof(Hole)), MIN_SIZE, 0};
|
|
|
|
LabelVec labels = {malloc(MIN_SIZE * sizeof(Label)), MIN_SIZE, 0};
|
2024-03-04 14:24:03 -06:00
|
|
|
size_t line = 0;
|
|
|
|
size_t line_start = 0;
|
|
|
|
size_t pos = 0;
|
|
|
|
// init=0, label=1, instruction=2, comment=3, newline -> 0
|
|
|
|
size_t line_state = 0;
|
|
|
|
AsmError err = ErrOk;
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
while (1)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
Token tok = token(input, len, pos);
|
|
|
|
einfo->token = tok;
|
|
|
|
pos = tok.start + tok.len;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (tok.kind == TokInvalid || tok.kind == TokBadNumber)
|
|
|
|
{
|
|
|
|
if (tok.num)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
err = (AsmError)tok.num;
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
err = ErrInvalidToken;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (tok.kind == TokEOF)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
break;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (tok.kind == TokComment)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
line_state = 3;
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (tok.kind == TokNewline)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
line += 1;
|
|
|
|
line_start = tok.start + tok.len;
|
|
|
|
line_state = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (tok.kind == TokDot)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
Token next = token(input, len, pos);
|
2024-03-07 08:09:18 -06:00
|
|
|
if (next.kind == TokIdent)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
err = ErrDirectiveNotImplemented;
|
|
|
|
goto end;
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
err = ErrNeedDirectiveAfterDot;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (tok.kind == TokIdent)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
Token next = token(input, len, pos);
|
2024-03-07 08:09:18 -06:00
|
|
|
if (next.kind == TokColon)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
// Label
|
|
|
|
pos = next.start + next.len;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (line_state >= 1)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
err = ErrLabelAfterLabel;
|
|
|
|
einfo->token = next;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
line_state = 1;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (ensure_push((ByteVec *)&labels, sizeof(Label), 1) != 0)
|
|
|
|
{
|
2024-03-05 08:17:19 -06:00
|
|
|
err = ErrOutOfMemory;
|
2024-03-04 14:24:03 -06:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
size_t idx = label_lookup(&labels, &input[tok.start], tok.len);
|
2024-03-07 08:09:18 -06:00
|
|
|
if (idx != INVALID)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
err = ErrDuplicateLabel;
|
|
|
|
goto end;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
labels.buf[labels.len] = (Label){
|
2024-03-04 14:24:03 -06:00
|
|
|
.location = rv.len,
|
|
|
|
.str = &input[tok.start],
|
|
|
|
.len = tok.len,
|
|
|
|
};
|
|
|
|
labels.len += 1;
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
// Instruction
|
2024-03-07 08:09:18 -06:00
|
|
|
if (line_state >= 2)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
err = ErrTrailingLine;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
line_state = 2;
|
|
|
|
err = assemble_instr(
|
|
|
|
ht, input, len, &tok,
|
2024-03-07 08:09:18 -06:00
|
|
|
&rv, &holes, &labels);
|
2024-03-04 14:24:03 -06:00
|
|
|
pos = tok.start + tok.len;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (err != 0)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-05 16:14:11 -06:00
|
|
|
err = ErrUnexpectedToken;
|
|
|
|
goto end;
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
for (size_t ii = 0; ii < holes.len; ii += 1)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
Hole *hole = &holes.buf[ii];
|
|
|
|
size_t idx = label_lookup(&labels, hole->str, hole->len);
|
|
|
|
uint64_t num_to_write = labels.buf[idx].location;
|
|
|
|
uint8_t sign = 1;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (hole->size != 8)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
sign = 2;
|
|
|
|
num_to_write -= hole->origin;
|
|
|
|
}
|
|
|
|
err = push_int_le(
|
2024-03-07 08:09:18 -06:00
|
|
|
&rv.buf[hole->location], num_to_write, hole->size, sign);
|
|
|
|
if (err != 0)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
end:
|
2024-03-04 14:24:03 -06:00
|
|
|
free(holes.buf);
|
|
|
|
free(labels.buf);
|
|
|
|
*out = rv;
|
|
|
|
einfo->line = line + 1;
|
|
|
|
einfo->line_start = line_start;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2024-03-04 14:30:03 -06:00
|
|
|
int hex_out = 0;
|
2024-03-07 08:09:18 -06:00
|
|
|
if (argc >= 2 && strcmp(argv[1], "--hex") == 0)
|
|
|
|
{
|
2024-03-04 14:30:03 -06:00
|
|
|
hex_out = 1;
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int err = 0;
|
|
|
|
InstHt ht = NULL;
|
|
|
|
ByteVec input;
|
|
|
|
|
|
|
|
err = slurp(stdin, &input);
|
2024-03-07 08:09:18 -06:00
|
|
|
if (err != 0)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
fprintf(stderr, "failed to read the file: %d\n", err);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
ht = build_lookup();
|
2024-03-07 08:09:18 -06:00
|
|
|
if (ht == NULL)
|
|
|
|
{
|
2024-03-05 08:17:19 -06:00
|
|
|
err = ErrOutOfMemory;
|
2024-03-04 14:24:03 -06:00
|
|
|
fprintf(stderr, "failed to init hash table: %d\n", err);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteVec out;
|
|
|
|
EInfo einfo;
|
|
|
|
err = assemble(ht, input.buf, input.len, &out, &einfo);
|
2024-03-07 08:09:18 -06:00
|
|
|
if (err != 0)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
size_t column = einfo.token.start - einfo.line_start + 1;
|
2024-03-06 02:59:30 -06:00
|
|
|
fprintf(stderr, "failed to assemble, %s, line=%zu, col=%zu token=%.*s\n",
|
2024-03-07 08:09:18 -06:00
|
|
|
ERRORS[err], einfo.line, column,
|
|
|
|
(int)einfo.token.len, &input.buf[einfo.token.start]);
|
2024-03-04 14:24:03 -06:00
|
|
|
goto done;
|
|
|
|
}
|
2024-03-07 08:09:18 -06:00
|
|
|
if (hex_out)
|
|
|
|
{
|
2024-03-04 14:24:03 -06:00
|
|
|
hd(out.buf, out.len);
|
2024-03-07 08:09:18 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-04 14:30:03 -06:00
|
|
|
fwrite(out.buf, 1, out.len, stdout);
|
2024-03-04 14:24:03 -06:00
|
|
|
}
|
|
|
|
|
2024-03-07 08:09:18 -06:00
|
|
|
done:
|
2024-03-04 14:24:03 -06:00
|
|
|
free(ht);
|
|
|
|
free(input.buf);
|
|
|
|
free(out.buf);
|
|
|
|
return err;
|
|
|
|
}
|