aboutsummaryrefslogtreecommitdiff
path: root/tasm/src
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2024-04-30 15:44:47 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2024-04-30 15:44:47 +0300
commit912dd003b081b628085e86bc151b8d91fbccb0c8 (patch)
tree8e27533fc5555e4c152e3f132c2b09e0dba9d86d /tasm/src
parent9fce87134559f8b5f86e105283b59e61a1707d39 (diff)
downloadtri-912dd003b081b628085e86bc151b8d91fbccb0c8.tar.gz
tri-912dd003b081b628085e86bc151b8d91fbccb0c8.zip
janitorial duties
+ Add doc and formatting tooling
Diffstat (limited to 'tasm/src')
-rw-r--r--tasm/src/assembler.c64
-rw-r--r--tasm/src/main.c12
2 files changed, 45 insertions, 31 deletions
diff --git a/tasm/src/assembler.c b/tasm/src/assembler.c
index 896ce74..e9f82a8 100644
--- a/tasm/src/assembler.c
+++ b/tasm/src/assembler.c
@@ -87,8 +87,8 @@ tri_t parse_imm(const char *imm)
}
void emit_r(struct asm_ctx *ctx, tri_t opcode,
- enum gpr_num rd, tri_t fn0, enum gpr_num rs1,
- enum gpr_num rs2, tri_t fn5)
+ enum gpr_num rd, tri_t fn0, enum gpr_num rs1,
+ enum gpr_num rs2, tri_t fn5)
{
tri_t xrd = gpr_tri(rd );
tri_t xrs1 = gpr_tri(rs1);
@@ -98,12 +98,13 @@ void emit_r(struct asm_ctx *ctx, tri_t opcode,
}
void emit_i(struct asm_ctx *ctx, tri_t opcode,
- enum gpr_num rd, tri_t fn0, enum gpr_num rs1, tri_t imm9)
+ enum gpr_num rd, tri_t fn0, enum gpr_num rs1, tri_t imm9)
{
tri_t xrd = gpr_tri(rd );
tri_t xrs1 = gpr_tri(rs1);
if (!check_imm(imm9, 9)) {
- fprintf(stderr, "immediate %lx doesn't fit into 9 trits\n", imm9);
+ fprintf(stderr, "immediate %lx doesn't fit into 9 trits\n",
+ imm9);
abort();
return;
}
@@ -112,12 +113,13 @@ void emit_i(struct asm_ctx *ctx, tri_t opcode,
}
void emit_s(struct asm_ctx *ctx, tri_t opcode,
- tri_t fn0, enum gpr_num rs1, enum gpr_num rs2, tri_t imm9)
+ tri_t fn0, enum gpr_num rs1, enum gpr_num rs2, tri_t imm9)
{
tri_t xrs1 = gpr_tri(rs1);
tri_t xrs2 = gpr_tri(rs2);
if (!check_imm(imm9, 9)) {
- fprintf(stderr, "immediate %lx doesn't fit into 9 trits\n", imm9);
+ fprintf(stderr, "immediate %lx doesn't fit into 9 trits\n",
+ imm9);
abort();
return;
}
@@ -132,11 +134,12 @@ void emit_s(struct asm_ctx *ctx, tri_t opcode,
}
void emit_u(struct asm_ctx *ctx, tri_t opcode,
- enum gpr_num rd, tri_t imm18)
+ enum gpr_num rd, tri_t imm18)
{
tri_t xrd = gpr_tri(rd);
if (!check_imm(imm18, 18)) {
- fprintf(stderr, "immediate %lx does not fit into 18 trits\n", imm18);
+ fprintf(stderr, "immediate %lx does not fit into 18 trits\n",
+ imm18);
abort();
return;
}
@@ -146,14 +149,15 @@ void emit_u(struct asm_ctx *ctx, tri_t opcode,
}
void emit_d(struct asm_ctx *ctx, tri_t opcode,
- enum gpr_num rd, enum gpr_num rs1, enum gpr_num rs2, tri_t imm10)
+ enum gpr_num rd, enum gpr_num rs1, enum gpr_num rs2, tri_t imm10)
{
tri_t xrd = gpr_tri(rd );
tri_t xrs1 = gpr_tri(rs1);
tri_t xrs2 = gpr_tri(rs2);
if (!check_imm(imm10, 10)) {
- fprintf(stderr, "immediate %lx does not fit into 10 trits\n", imm10);
+ fprintf(stderr, "immediate %lx does not fit into 10 trits\n",
+ imm10);
abort();
return;
}
@@ -240,7 +244,8 @@ static int process(struct asm_ctx *ctx, const char *file)
{
FILE *f = fopen(file, "rb");
if (!f) {
- fprintf(stderr, "failed opening %s: %s\n", file, strerror(errno));
+ fprintf(stderr, "failed opening %s: %s\n", file,
+ strerror(errno));
return -1;
}
@@ -369,7 +374,7 @@ static void fix_reloc_b(struct asm_ctx *ctx, size_t ro, size_t so)
/* should really be line based rather than offset, but good
* enough for now */
fprintf(stderr, "branch offset at %llu too large\n",
- (unsigned long long)ro);
+ (unsigned long long)ro);
abort();
}
@@ -395,7 +400,7 @@ static void fix_reloc_j(struct asm_ctx *ctx, size_t ro, size_t so)
tri_t t = tri_from(off);
if (tri_mask(t, 18) != t) {
fprintf(stderr, "jump offset at %llu too large\n",
- (unsigned long long)ro);
+ (unsigned long long)ro);
abort();
}
@@ -442,12 +447,12 @@ static int try_fix_reloc(struct asm_ctx *ctx, struct asm_reloc r)
size_t so = s.offset;
/* matching */
switch (r.kind) {
- case RELOC_LA: fix_reloc_la(ctx, ro, so); break;
- case RELOC_B: fix_reloc_b(ctx, ro, so); break;
- case RELOC_J: fix_reloc_j(ctx, ro, so); break;
- case RELOC_CALL: fix_reloc_call(ctx, ro, so); break;
- default: fprintf(stderr, "unimplemented reloc: %d\n", r.kind);
- abort();
+ case RELOC_LA: fix_reloc_la(ctx, ro, so); break;
+ case RELOC_B: fix_reloc_b(ctx, ro, so); break;
+ case RELOC_J: fix_reloc_j(ctx, ro, so); break;
+ case RELOC_CALL: fix_reloc_call(ctx, ro, so); break;
+ default: fprintf(stderr, "unimplemented reloc: %d\n", r.kind);
+ abort();
}
return 0;
}
@@ -462,7 +467,8 @@ static int fix_relocs(struct asm_ctx *ctx)
struct asm_reloc r = ctx->relocs[i];
if (try_fix_reloc(ctx, r)) {
/* we were unable to fulfill this reloc, fail */
- fprintf(stderr, "unable to fulfill reloc for %s\n", r.sym);
+ fprintf(stderr, "unable to fulfill reloc for %s\n",
+ r.sym);
return 1;
}
}
@@ -535,7 +541,9 @@ tri_t check_nop(const char *nop)
case ' ': continue;
default:
- fprintf(stderr, "invalid character in unop format: %c\n", nop[i]);
+ fprintf(stderr,
+ "invalid character in unop format: %c\n",
+ nop[i]);
abort();
}
@@ -567,7 +575,9 @@ tri_t check_nop3(const char *nop)
case ' ': continue;
default:
- fprintf(stderr, "invalid character in unop format: %c\n", nop[i]);
+ fprintf(stderr,
+ "invalid character in unop format: %c\n",
+ nop[i]);
abort();
}
@@ -609,11 +619,14 @@ tri_t check_width(const char *width)
return 0;
}
-void emit_reloc(struct asm_ctx *ctx, enum asm_reloc_kind reloc, const char *name)
+void emit_reloc(struct asm_ctx *ctx, enum asm_reloc_kind reloc,
+ const char *name)
{
if (ctx->reloc_count >= ctx->reloc_max) {
ctx->reloc_max *= 2;
- ctx->relocs = realloc(ctx->relocs, ctx->reloc_max * sizeof(struct asm_reloc));
+ ctx->relocs = realloc(ctx->relocs,
+ ctx->reloc_max *
+ sizeof(struct asm_reloc));
}
ctx->relocs[ctx->reloc_count++] = (struct asm_reloc){
@@ -627,7 +640,8 @@ void emit_label(struct asm_ctx *ctx, const char *name)
{
if (ctx->sym_count >= ctx->sym_max) {
ctx->sym_max *= 2;
- ctx->syms = realloc(ctx->syms, ctx->sym_max * sizeof(struct asm_symbol));
+ ctx->syms = realloc(ctx->syms,
+ ctx->sym_max * sizeof(struct asm_symbol));
}
ctx->syms[ctx->sym_count++] = (struct asm_symbol){
diff --git a/tasm/src/main.c b/tasm/src/main.c
index 7925b89..6304f2a 100644
--- a/tasm/src/main.c
+++ b/tasm/src/main.c
@@ -5,12 +5,12 @@
#include <stdio.h>
static const char *cmdline_usage =
-"tasm trinary assembler usage:\n"
-" tasm [-I <dir>...] -o outfile infile\n"
-" -h Show usage (this)\n"
-" -I <dir> Add directory to include path\n"
-" -o <outfile> File to output to\n"
-" infile Top file to assemble\n"
+ "tasm trinary assembler usage:\n"
+ " tasm [-I <dir>...] -o outfile infile\n"
+ " -h Show usage (this)\n"
+ " -I <dir> Add directory to include path\n"
+ " -o <outfile> File to output to\n"
+ " infile Top file to assemble\n"
;
static void usage()