format
This commit is contained in:
parent
4dfd6f2fc0
commit
757fb71b9a
|
@ -56,8 +56,7 @@ const char *TYPE_STR[] = {
|
|||
|
||||
const size_t NARGS = sizeof(ARGS) / sizeof(ARGS[0]);
|
||||
|
||||
static
|
||||
ArgMeta arg_meta(char arg) {
|
||||
static ArgMeta arg_meta(char arg) {
|
||||
for (size_t ii = 0; ii < NARGS; ii += 1) {
|
||||
ArgMeta meta = ARGS[ii];
|
||||
if (meta.chr == arg) {
|
||||
|
|
|
@ -6,8 +6,7 @@ typedef struct ByteVec_s {
|
|||
size_t len;
|
||||
} ByteVec;
|
||||
|
||||
static
|
||||
AsmError ensure_push(ByteVec *vec, size_t el_size, size_t extra) {
|
||||
static AsmError ensure_push(ByteVec *vec, size_t el_size, size_t extra) {
|
||||
if (vec->len + extra < vec->len) {
|
||||
return ErrOutOfMemory;
|
||||
}
|
||||
|
|
|
@ -5,8 +5,7 @@ typedef struct InstHtNode_s {
|
|||
} InstHtNode;
|
||||
typedef InstHtNode *InstHt;
|
||||
|
||||
static
|
||||
uint32_t inst_hash(const char *s, size_t len) {
|
||||
static uint32_t inst_hash(const char *s, size_t len) {
|
||||
uint32_t hash = 0;
|
||||
uint32_t mul = 75;
|
||||
for (size_t ii = 0; ii < len; ii += 1) {
|
||||
|
@ -16,8 +15,7 @@ uint32_t inst_hash(const char *s, size_t len) {
|
|||
return hash;
|
||||
}
|
||||
|
||||
static
|
||||
InstHt build_lookup(void) {
|
||||
static InstHt build_lookup(void) {
|
||||
const size_t size = 256;
|
||||
InstHt table = (InstHt)malloc(size * sizeof(InstHtNode));
|
||||
if (table == NULL) {
|
||||
|
@ -42,8 +40,7 @@ InstHt build_lookup(void) {
|
|||
return table;
|
||||
}
|
||||
|
||||
static
|
||||
size_t inst_lookup(InstHt ht, const char *s, size_t len) {
|
||||
static size_t inst_lookup(InstHt ht, const char *s, size_t len) {
|
||||
uint32_t hash = inst_hash(s, len);
|
||||
uint8_t *node = (uint8_t *)&ht[(size_t)(hash & 0xff)];
|
||||
for (size_t ii = 0; ii < 2; ii += 1) {
|
||||
|
|
117
src/hbas.c
117
src/hbas.c
|
@ -20,8 +20,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -42,8 +42,7 @@ SOFTWARE.
|
|||
|
||||
// Print space-separated hex dump of each byte, 16 bytes per line.
|
||||
// Can be reversed with `xxd -p -r`.
|
||||
static
|
||||
void hex_dump(char *data, size_t len) {
|
||||
static void hex_dump(char *data, size_t len) {
|
||||
char buf[48];
|
||||
const char *alphabet = "0123456789abcdef";
|
||||
for (size_t ii = 0; ii < len; ii += 1) {
|
||||
|
@ -61,8 +60,7 @@ void hex_dump(char *data, size_t len) {
|
|||
|
||||
#define MIN_SIZE 4096
|
||||
|
||||
static
|
||||
int slurp(FILE *fd, ByteVec *out) {
|
||||
static int slurp(FILE *fd, ByteVec *out) {
|
||||
ByteVec rv = {malloc(MIN_SIZE), MIN_SIZE, 0};
|
||||
size_t bread = 1;
|
||||
int err = 0;
|
||||
|
@ -109,8 +107,7 @@ typedef struct LabelVec_s {
|
|||
size_t len;
|
||||
} LabelVec;
|
||||
|
||||
static
|
||||
size_t label_lookup(LabelVec *labels, char *name, size_t len) {
|
||||
static size_t label_lookup(LabelVec *labels, char *name, size_t len) {
|
||||
size_t nlabels = labels->len;
|
||||
Label *buf = labels->buf;
|
||||
for (size_t ii = 0; ii < nlabels; ii += 1) {
|
||||
|
@ -122,8 +119,7 @@ size_t label_lookup(LabelVec *labels, char *name, size_t len) {
|
|||
return INVALID;
|
||||
}
|
||||
|
||||
static
|
||||
bool check_valid_int(uint64_t val, size_t size, uint8_t sign) {
|
||||
static bool check_valid_int(uint64_t val, size_t size, uint8_t sign) {
|
||||
// All 64-bit values are considered valid.
|
||||
if (size == 8) {
|
||||
return true;
|
||||
|
@ -162,8 +158,8 @@ bool check_valid_int(uint64_t val, size_t size, uint8_t sign) {
|
|||
// safety: assumes the buffer has enough place for specified integer size.
|
||||
// `sign` is a bitset, where bit `1` indicates that value accepts a signed int,
|
||||
// and bit `2` indicates that value accepts an unsigned int.
|
||||
static
|
||||
AsmError push_int_le(char *buf, uint64_t val, size_t size, uint8_t sign) {
|
||||
static AsmError push_int_le(char *buf, uint64_t val, size_t size,
|
||||
uint8_t sign) {
|
||||
if (!check_valid_int(val, size, sign)) {
|
||||
return ErrImmediateOverflow;
|
||||
}
|
||||
|
@ -186,37 +182,37 @@ AsmError push_string(char *buf, char *input, size_t len) {
|
|||
pos += 1;
|
||||
chr = input[pos];
|
||||
switch (chr) {
|
||||
case '\\':
|
||||
chr = '\\';
|
||||
break;
|
||||
case '"':
|
||||
chr = '"';
|
||||
break;
|
||||
case 'r':
|
||||
chr = '\r';
|
||||
break;
|
||||
case 'n':
|
||||
chr = '\n';
|
||||
break;
|
||||
case '0':
|
||||
chr = '\0';
|
||||
break;
|
||||
case 't':
|
||||
chr = '\t';
|
||||
break;
|
||||
case 'x':
|
||||
if (pos + 2 >= len) {
|
||||
return ErrDanglingEscape;
|
||||
}
|
||||
char high = get_hex(input[pos + 1]);
|
||||
char low = get_hex(input[pos + 2]);
|
||||
if (high > 15 || low > 15) {
|
||||
return ErrStringBadHex;
|
||||
}
|
||||
chr = high << 4 | low;
|
||||
break;
|
||||
default:
|
||||
return ErrBadStringEscape;
|
||||
case '\\':
|
||||
chr = '\\';
|
||||
break;
|
||||
case '"':
|
||||
chr = '"';
|
||||
break;
|
||||
case 'r':
|
||||
chr = '\r';
|
||||
break;
|
||||
case 'n':
|
||||
chr = '\n';
|
||||
break;
|
||||
case '0':
|
||||
chr = '\0';
|
||||
break;
|
||||
case 't':
|
||||
chr = '\t';
|
||||
break;
|
||||
case 'x':
|
||||
if (pos + 2 >= len) {
|
||||
return ErrDanglingEscape;
|
||||
}
|
||||
char high = get_hex(input[pos + 1]);
|
||||
char low = get_hex(input[pos + 2]);
|
||||
if (high > 15 || low > 15) {
|
||||
return ErrStringBadHex;
|
||||
}
|
||||
chr = high << 4 | low;
|
||||
break;
|
||||
default:
|
||||
return ErrBadStringEscape;
|
||||
}
|
||||
}
|
||||
buf[ndata] = chr;
|
||||
|
@ -225,9 +221,8 @@ AsmError push_string(char *buf, char *input, size_t len) {
|
|||
return ErrOk;
|
||||
}
|
||||
|
||||
static
|
||||
AsmError assemble_instr(InstHt ht, char *input, size_t len, Token *tok,
|
||||
ByteVec *rv, HoleVec *holes) {
|
||||
static AsmError assemble_instr(InstHt ht, char *input, size_t len, Token *tok,
|
||||
ByteVec *rv, HoleVec *holes) {
|
||||
const InstDesc *inst;
|
||||
const char *type_str;
|
||||
size_t nargs;
|
||||
|
@ -327,8 +322,8 @@ AsmError assemble_instr(InstHt ht, char *input, size_t len, Token *tok,
|
|||
return ErrOk;
|
||||
}
|
||||
|
||||
static
|
||||
AsmError push_data(char *input, size_t len, ByteVec *out, Token *tok, size_t word_size) {
|
||||
static AsmError push_data(char *input, size_t len, ByteVec *out, Token *tok,
|
||||
size_t word_size) {
|
||||
while (1) {
|
||||
*tok = token(input, len, tok->start + tok->len);
|
||||
if (tok->kind == TokNumber) {
|
||||
|
@ -375,20 +370,20 @@ AsmError assemble_directive(char *input, size_t len, ByteVec *out, Token *tok) {
|
|||
if (byte0 == 'd') {
|
||||
size_t word_size;
|
||||
switch (byte1) {
|
||||
case 'b':
|
||||
word_size = 1;
|
||||
break;
|
||||
case 'w':
|
||||
word_size = 2;
|
||||
break;
|
||||
case 'd':
|
||||
word_size = 4;
|
||||
break;
|
||||
case 'q':
|
||||
word_size = 8;
|
||||
break;
|
||||
default:
|
||||
return ErrInvalidDirective;
|
||||
case 'b':
|
||||
word_size = 1;
|
||||
break;
|
||||
case 'w':
|
||||
word_size = 2;
|
||||
break;
|
||||
case 'd':
|
||||
word_size = 4;
|
||||
break;
|
||||
case 'q':
|
||||
word_size = 8;
|
||||
break;
|
||||
default:
|
||||
return ErrInvalidDirective;
|
||||
}
|
||||
return push_data(input, len, out, tok, word_size);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
static
|
||||
int parse_register(char *name, size_t len) {
|
||||
static int parse_register(char *name, size_t len) {
|
||||
if (name[0] != 'r') {
|
||||
return 256; // Register name should start with 'r'
|
||||
}
|
||||
|
|
61
src/token.c
61
src/token.c
|
@ -19,8 +19,7 @@ typedef struct Token_s {
|
|||
uint64_t num;
|
||||
} Token;
|
||||
|
||||
static
|
||||
Token token_ident(char *input, size_t len, size_t pos) {
|
||||
static Token token_ident(char *input, size_t len, size_t pos) {
|
||||
size_t start = pos;
|
||||
while (pos < len) {
|
||||
char chr = input[pos];
|
||||
|
@ -35,8 +34,7 @@ Token token_ident(char *input, size_t len, size_t pos) {
|
|||
return (Token){TokIdent, start, pos - start, 0};
|
||||
}
|
||||
|
||||
static
|
||||
Token token_number(char *input, size_t len, size_t pos) {
|
||||
static Token token_number(char *input, size_t len, size_t pos) {
|
||||
char *ptr = &input[pos];
|
||||
char next = '\0';
|
||||
size_t start = pos;
|
||||
|
@ -112,8 +110,7 @@ Token token_number(char *input, size_t len, size_t pos) {
|
|||
}
|
||||
}
|
||||
|
||||
static
|
||||
char get_hex(char chr) {
|
||||
static char get_hex(char chr) {
|
||||
char chru = chr & ~0x20;
|
||||
if (chr >= '0' && chr <= '9') {
|
||||
return chr - '0';
|
||||
|
@ -124,8 +121,7 @@ char get_hex(char chr) {
|
|||
return 16;
|
||||
}
|
||||
|
||||
static
|
||||
Token token_string(char *input, size_t len, size_t pos) {
|
||||
static Token token_string(char *input, size_t len, size_t pos) {
|
||||
size_t start = pos;
|
||||
size_t ndata = 0;
|
||||
for (pos += 1; pos < len; pos += 1) {
|
||||
|
@ -133,32 +129,38 @@ Token token_string(char *input, size_t len, size_t pos) {
|
|||
return (Token){TokString, start, pos + 1 - start, ndata};
|
||||
}
|
||||
if (input[pos] == '\n' || input[pos] == '\r') {
|
||||
return (Token){TokInvalid, start, pos + 1 - start, ErrStringNewLine};
|
||||
return (Token){TokInvalid, start, pos + 1 - start,
|
||||
ErrStringNewLine};
|
||||
}
|
||||
if (input[pos] == '\\') {
|
||||
if (pos + 1 >= len) {
|
||||
return (Token){TokInvalid, start, pos - start, ErrDanglingEscape};
|
||||
return (Token){TokInvalid, start, pos - start,
|
||||
ErrDanglingEscape};
|
||||
}
|
||||
pos += 1;
|
||||
switch (input[pos]) {
|
||||
case '\\':
|
||||
case '"':
|
||||
case 'r':
|
||||
case 'n':
|
||||
case '0':
|
||||
case 't':
|
||||
break;
|
||||
case 'x':
|
||||
if (pos + 2 >= len) {
|
||||
return (Token){TokInvalid, start, pos - start, ErrDanglingEscape};
|
||||
}
|
||||
if (get_hex(input[pos + 1]) > 15 || get_hex(input[pos + 2]) > 15) {
|
||||
return (Token){TokInvalid, start, pos - start, ErrStringBadHex};
|
||||
}
|
||||
pos += 2;
|
||||
break;
|
||||
default:
|
||||
return (Token){TokInvalid, start, pos - start, ErrBadStringEscape};
|
||||
case '\\':
|
||||
case '"':
|
||||
case 'r':
|
||||
case 'n':
|
||||
case '0':
|
||||
case 't':
|
||||
break;
|
||||
case 'x':
|
||||
if (pos + 2 >= len) {
|
||||
return (Token){TokInvalid, start, pos - start,
|
||||
ErrDanglingEscape};
|
||||
}
|
||||
if (get_hex(input[pos + 1]) > 15 ||
|
||||
get_hex(input[pos + 2]) > 15) {
|
||||
return (Token){TokInvalid, start, pos - start,
|
||||
ErrStringBadHex};
|
||||
}
|
||||
pos += 2;
|
||||
break;
|
||||
default:
|
||||
return (Token){TokInvalid, start, pos - start,
|
||||
ErrBadStringEscape};
|
||||
}
|
||||
}
|
||||
ndata += 1;
|
||||
|
@ -166,8 +168,7 @@ Token token_string(char *input, size_t len, size_t pos) {
|
|||
return (Token){TokString, start, pos - start, ndata};
|
||||
}
|
||||
|
||||
static
|
||||
Token token(char *input, size_t len, size_t pos) {
|
||||
static Token token(char *input, size_t len, size_t pos) {
|
||||
char chr, chru;
|
||||
char *ptr = &input[pos];
|
||||
while (pos < len && (input[pos] == ' ' || input[pos] == '\t')) {
|
||||
|
|
Loading…
Reference in a new issue