aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--main.c120
-rw-r--r--minias.h72
-rw-r--r--parse.c30
-rw-r--r--util.c56
5 files changed, 143 insertions, 140 deletions
diff --git a/Makefile b/Makefile
index d3ef7da..0e08837 100644
--- a/Makefile
+++ b/Makefile
@@ -25,7 +25,10 @@ main.o parse.o util.o: minias.h
fmt:
clang-format \
- -style='{BasedOnStyle: WebKit, AlwaysBreakAfterReturnType: TopLevelDefinitions, ColumnLimit: 80}'\
+ -style="{BasedOnStyle: WebKit,\
+ AlwaysBreakAfterReturnType: TopLevelDefinitions,\
+ ColumnLimit: 80,\
+ PointerAlignment: Right}"\
-i *.c *.h
check:
diff --git a/main.c b/main.c
index 9d734c0..273ee7f 100644
--- a/main.c
+++ b/main.c
@@ -1,16 +1,16 @@
#include "minias.h"
/* Parsed assembly */
-static AsmLine* allasm = NULL;
+static AsmLine *allasm = NULL;
/* Number of assembly relaxation passes. */
static int nrelax = 1;
/* Symbols before writing to symtab section. */
-static struct hashtable* symbols = NULL;
+static struct hashtable *symbols = NULL;
/* Array of all relocations before adding to the rel section. */
-static Relocation* relocs = NULL;
+static Relocation *relocs = NULL;
static size_t nrelocs = 0;
static size_t reloccap = 0;
@@ -18,21 +18,21 @@ static size_t reloccap = 0;
static Section sections[MAXSECTIONS];
static size_t nsections = 1; // first is reserved.
-static Section* cursection = NULL;
-static Section* shstrtab = NULL;
-static Section* strtab = NULL;
-static Section* symtab = NULL;
-static Section* bss = NULL;
-static Section* text = NULL;
-static Section* data = NULL;
-static Section* textrel = NULL;
-static Section* datarel = NULL;
-
-static char* infilename = "<stdin>";
+static Section *cursection = NULL;
+static Section *shstrtab = NULL;
+static Section *strtab = NULL;
+static Section *symtab = NULL;
+static Section *bss = NULL;
+static Section *text = NULL;
+static Section *data = NULL;
+static Section *textrel = NULL;
+static Section *datarel = NULL;
+
+static char *infilename = "<stdin>";
static size_t curlineno = 0;
static void
-lfatal(const char* fmt, ...)
+lfatal(const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "%s:%ld: ", infilename, curlineno);
@@ -42,14 +42,14 @@ lfatal(const char* fmt, ...)
exit(1);
}
-static Symbol*
-getsym(const char* name)
+static Symbol *
+getsym(const char *name)
{
Symbol **ps, *s;
struct hashtablekey htk;
htabkey(&htk, name, strlen(name));
- ps = (Symbol**)htabput(symbols, &htk);
+ ps = (Symbol **)htabput(symbols, &htk);
if (!*ps) {
*ps = xmalloc(sizeof(Symbol));
**ps = (Symbol){
@@ -62,7 +62,7 @@ getsym(const char* name)
}
static void
-secaddbytes(Section* s, const void* bytes, size_t n)
+secaddbytes(Section *s, const void *bytes, size_t n)
{
if (s->hdr.sh_type == SHT_NOBITS) {
@@ -80,23 +80,23 @@ secaddbytes(Section* s, const void* bytes, size_t n)
}
static void
-secaddbyte(Section* s, uint8_t b)
+secaddbyte(Section *s, uint8_t b)
{
secaddbytes(s, &b, 1);
}
static Elf64_Word
-elfstr(Section* sec, const char* s)
+elfstr(Section *sec, const char *s)
{
Elf64_Word i = sec->hdr.sh_size;
secaddbytes(sec, s, strlen(s) + 1);
return i;
}
-static Section*
+static Section *
newsection()
{
- Section* s;
+ Section *s;
if (nsections >= MAXSECTIONS)
fatal("too many sections");
s = &sections[nsections];
@@ -105,15 +105,15 @@ newsection()
return s;
}
-static Section*
-getsection(const char* name)
+static Section *
+getsection(const char *name)
{
size_t i;
- char* secname;
- Section* s;
+ char *secname;
+ Section *s;
for (i = 0; i < nsections; i++) {
- secname = (char*)shstrtab->data + sections[i].hdr.sh_name;
+ secname = (char *)shstrtab->data + sections[i].hdr.sh_name;
if (strcmp(secname, name) == 0)
return &sections[i];
}
@@ -178,7 +178,7 @@ initsections(void)
datarel->hdr.sh_entsize = sizeof(Elf64_Rela);
}
-static Relocation*
+static Relocation *
newreloc()
{
if (nrelocs == reloccap) {
@@ -197,7 +197,7 @@ sb(uint8_t b)
}
static void
-sbn(uint8_t* bytes, size_t n)
+sbn(uint8_t *bytes, size_t n)
{
secaddbytes(cursection, bytes, n);
}
@@ -327,10 +327,10 @@ assemblerex(Rex rex)
/* Assemble a symbolic value. */
static void
-assemblereloc(const char* l, int64_t c, int nbytes, int type)
+assemblereloc(const char *l, int64_t c, int nbytes, int type)
{
- Relocation* reloc;
- Symbol* sym;
+ Relocation *reloc;
+ Symbol *sym;
if (l != NULL) {
reloc = newreloc();
@@ -347,7 +347,7 @@ assemblereloc(const char* l, int64_t c, int nbytes, int type)
/* Assemble a r <-> mem operation. */
static void
-assemblemem(const Memarg* memarg, Rex rex, VarBytes prefix, VarBytes opcode,
+assemblemem(const Memarg *memarg, Rex rex, VarBytes prefix, VarBytes opcode,
uint8_t reg, int32_t nexti)
{
@@ -480,11 +480,11 @@ assemblemem(const Memarg* memarg, Rex rex, VarBytes prefix, VarBytes opcode,
}
static void
-assemblejmp(const Jmp* j)
+assemblejmp(const Jmp *j)
{
int jmpsize;
int64_t distance;
- Symbol* target;
+ Symbol *target;
// clang-format off
static uint8_t cc2op[31] = {
@@ -523,7 +523,7 @@ assemblejmp(const Jmp* j)
}
static void
-assembleabsimm(const Imm* imm)
+assembleabsimm(const Imm *imm)
{
int reltype;
@@ -542,11 +542,11 @@ assembleabsimm(const Imm* imm)
}
static void
-assembleinstr(const Instr* instr)
+assembleinstr(const Instr *instr)
{
Rex rex;
- const Memarg* memarg;
- const Imm* imm;
+ const Memarg *memarg;
+ const Imm *imm;
uint8_t reg, rm;
switch (instr->encoder) {
@@ -696,9 +696,9 @@ assembleinstr(const Instr* instr)
static void
assemble(void)
{
- Symbol* sym;
- AsmLine* l;
- const Parsev* v;
+ Symbol *sym;
+ AsmLine *l;
+ const Parsev *v;
cursection = text;
curlineno = 0;
@@ -716,8 +716,8 @@ assemble(void)
sym->global = 1;
break;
case ASM_DIR_SECTION: {
- const char* fp;
- Section* s;
+ const char *fp;
+ Section *s;
s = getsection(v->section.name);
s->hdr.sh_type = v->section.type;
@@ -821,8 +821,8 @@ assemble(void)
static void
relaxreset(void)
{
- Symbol* sym;
- Section* sec;
+ Symbol *sym;
+ Section *sec;
size_t i;
/* Reset relocations and section data but retain capacity. */
@@ -847,7 +847,7 @@ relaxreset(void)
}
static void
-addtosymtab(Symbol* sym)
+addtosymtab(Symbol *sym)
{
Elf64_Sym elfsym;
int stype;
@@ -874,7 +874,7 @@ addtosymtab(Symbol* sym)
static void
fillsymtab(void)
{
- Symbol* sym;
+ Symbol *sym;
size_t i;
// Local symbols
@@ -904,10 +904,10 @@ fillsymtab(void)
}
static int
-resolvereloc(Relocation* reloc)
+resolvereloc(Relocation *reloc)
{
- Symbol* sym;
- uint8_t* rdata;
+ Symbol *sym;
+ uint8_t *rdata;
int64_t value;
sym = reloc->sym;
@@ -939,10 +939,10 @@ resolvereloc(Relocation* reloc)
}
static void
-appendreloc(Relocation* reloc)
+appendreloc(Relocation *reloc)
{
- Symbol* sym;
- Section* relsection;
+ Symbol *sym;
+ Section *relsection;
Elf64_Rela elfrel;
memset(&elfrel, 0, sizeof(elfrel));
@@ -975,7 +975,7 @@ appendreloc(Relocation* reloc)
static void
handlerelocs(void)
{
- Relocation* reloc;
+ Relocation *reloc;
size_t i;
for (i = 0; i < nrelocs; i++) {
reloc = &relocs[i];
@@ -986,7 +986,7 @@ handlerelocs(void)
}
static void
-out(const void* buf, size_t n)
+out(const void *buf, size_t n)
{
fwrite(buf, 1, n, stdout);
if (ferror(stdout))
@@ -1036,7 +1036,7 @@ outelf(void)
}
static void
-usage(char* argv0)
+usage(char *argv0)
{
fprintf(stderr, "minias - a mini x86-64 assembler.\n\n");
fprintf(stderr, "usage: %s [-r iter] [-o out] [input]\n", argv0);
@@ -1047,7 +1047,7 @@ usage(char* argv0)
}
static void
-parseargs(int argc, char* argv[])
+parseargs(int argc, char *argv[])
{
char *a, *argv0, *outfname;
@@ -1087,7 +1087,7 @@ parseargs(int argc, char* argv[])
}
int
-main(int argc, char* argv[])
+main(int argc, char *argv[])
{
symbols = mkhtab(256);
parseargs(argc, argv);
diff --git a/minias.h b/minias.h
index 1baaf89..f211116 100644
--- a/minias.h
+++ b/minias.h
@@ -17,23 +17,23 @@ typedef struct {
int64_t wco;
int64_t offset;
size_t capacity;
- uint8_t* data;
+ uint8_t *data;
} Section;
typedef struct {
- const char* name;
+ const char *name;
int32_t idx;
int64_t offset;
int64_t wco; /* worst case offset */
int64_t size;
int global;
int defined;
- Section* section;
+ Section *section;
} Symbol;
typedef struct {
- Section* section;
- Symbol* sym;
+ Section *section;
+ Symbol *sym;
int type;
int64_t offset;
int64_t addend;
@@ -162,24 +162,24 @@ typedef union Parsev Parsev;
typedef struct Label {
AsmKind kind;
- const char* name;
+ const char *name;
} Label;
typedef struct Globl {
AsmKind kind;
- const char* name;
+ const char *name;
} Globl;
typedef struct DirSection {
AsmKind kind;
int32_t type;
- const char* name;
- const char* flags;
+ const char *name;
+ const char *flags;
} DirSection;
typedef struct {
int64_t c;
- const char* l;
+ const char *l;
} Value;
typedef struct Byte {
@@ -231,7 +231,7 @@ typedef struct Memarg {
typedef struct String {
AsmKind kind;
size_t len;
- uint8_t* data;
+ uint8_t *data;
} String;
typedef String Ascii;
typedef String Asciiz;
@@ -239,7 +239,7 @@ typedef String Asciiz;
typedef struct Jmp {
AsmKind kind;
uint32_t cc; /* 0 means unconditional. */
- const char* target;
+ const char *target;
} Jmp;
/* Rex opcode prefix. */
@@ -281,9 +281,9 @@ typedef struct Instr {
uint32_t fixedreg;
int32_t opcode;
int32_t prefix;
- const Parsev* arg1;
- const Parsev* arg2;
- const Parsev* arg3;
+ const Parsev *arg1;
+ const Parsev *arg2;
+ const Parsev *arg3;
} Instr;
union Parsev {
@@ -306,7 +306,7 @@ union Parsev {
String string;
// Temporary values.
Value value;
- const char* charptr;
+ const char *charptr;
int64_t i64;
};
@@ -315,40 +315,40 @@ union Parsev {
typedef struct AsmLine AsmLine;
struct AsmLine {
int64_t lineno;
- const Parsev* v;
- AsmLine* next;
+ const Parsev *v;
+ AsmLine *next;
};
-AsmLine* parseasm(void);
+AsmLine *parseasm(void);
/* util.c */
-void vwarn(const char* fmt, va_list ap);
-void fatal(const char* fmt, ...);
+void vwarn(const char *fmt, va_list ap);
+void fatal(const char *fmt, ...);
void unreachable(void);
-void* xmalloc(size_t);
-void* xrealloc(void*, size_t);
-void* xreallocarray(void*, size_t, size_t);
-char* xmemdup(const char*, size_t);
-char* xstrdup(const char* s);
-void* zalloc(size_t n);
+void *xmalloc(size_t);
+void *xrealloc(void *, size_t);
+void *xreallocarray(void *, size_t, size_t);
+char *xmemdup(const char *, size_t);
+char *xstrdup(const char *s);
+void *zalloc(size_t n);
struct hashtable {
size_t len, cap;
- struct hashtablekey* keys;
- void** vals;
+ struct hashtablekey *keys;
+ void **vals;
};
struct hashtablekey {
uint64_t hash;
- const char* str;
+ const char *str;
size_t len;
};
-void htabkey(struct hashtablekey*, const char*, size_t);
-struct hashtable* mkhtab(size_t);
-void delhtab(struct hashtable*, void(void*));
-void** htabput(struct hashtable*, struct hashtablekey*);
-void* htabget(struct hashtable*, struct hashtablekey*);
-uint64_t murmurhash64a(const void*, size_t); \ No newline at end of file
+void htabkey(struct hashtablekey *, const char *, size_t);
+struct hashtable *mkhtab(size_t);
+void delhtab(struct hashtable *, void(void *));
+void **htabput(struct hashtable *, struct hashtablekey *);
+void *htabget(struct hashtable *, struct hashtablekey *);
+uint64_t murmurhash64a(const void *, size_t); \ No newline at end of file
diff --git a/parse.c b/parse.c
index da01509..eaa9c67 100644
--- a/parse.c
+++ b/parse.c
@@ -1,8 +1,8 @@
#include "minias.h"
/* Cache of Parsev* by value. */
-static const Parsev*
-internparsev(Parsev* p)
+static const Parsev *
+internparsev(Parsev *p)
{
/*
A simple direct mapped cache that prevents our parser
@@ -16,26 +16,26 @@ internparsev(Parsev* p)
variants.
*/
size_t idx;
- const Parsev* interned;
- static const Parsev* cache[4096] = { 0 };
+ const Parsev *interned;
+ static const Parsev *cache[4096] = { 0 };
- idx = murmurhash64a((char*)p, sizeof(Parsev)) % sizeof(cache)
+ idx = murmurhash64a((char *)p, sizeof(Parsev)) % sizeof(cache)
/ sizeof(cache[0]);
interned = cache[idx];
if (interned && memcmp(p, interned, sizeof(Parsev)) == 0)
return interned;
- interned = (const Parsev*)xmemdup((char*)p, sizeof(Parsev));
+ interned = (const Parsev *)xmemdup((char *)p, sizeof(Parsev));
cache[idx] = interned;
return interned;
}
/* Cache of char* by value. */
-const char*
-internstring(const char* s)
+const char *
+internstring(const char *s)
{
size_t idx, len;
- const char* interned;
- static const char* cache[4096] = { 0 };
+ const char *interned;
+ static const char *cache[4096] = { 0 };
len = strlen(s);
idx = murmurhash64a(s, len) % sizeof(cache) / sizeof(cache[0]);
@@ -48,12 +48,12 @@ internstring(const char* s)
}
static String
-decodestring(char* s)
+decodestring(char *s)
{
- char* end;
+ char *end;
size_t len = 0;
size_t cap = 0;
- uint8_t* data = NULL;
+ uint8_t *data = NULL;
uint8_t c = 0;
/* The string is already validated by the parser so we omit some checks*/
@@ -92,7 +92,7 @@ decodestring(char* s)
}
static int
-needsmovabs(Imm* imm)
+needsmovabs(Imm *imm)
{
int64_t mask, maskedc;
@@ -275,7 +275,7 @@ needsmovabs(Imm* imm)
#define YY_CTX_MEMBERS Parsev v;
#include "asm.peg.inc"
-AsmLine*
+AsmLine *
parseasm(void)
{
AsmLine *result, *l, *prevl;
diff --git a/util.c b/util.c
index 68b0b01..995f4b3 100644
--- a/util.c
+++ b/util.c
@@ -1,7 +1,7 @@
#include "minias.h"
void
-vwarn(const char* fmt, va_list ap)
+vwarn(const char *fmt, va_list ap)
{
vfprintf(stderr, fmt, ap);
if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
@@ -13,7 +13,7 @@ vwarn(const char* fmt, va_list ap)
}
void
-fatal(const char* fmt, ...)
+fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
@@ -28,10 +28,10 @@ unreachable(void)
fatal("BUG: unexpected internal condition");
}
-void*
+void *
xmalloc(size_t n)
{
- void* p;
+ void *p;
p = malloc(n);
if (!p)
@@ -40,10 +40,10 @@ xmalloc(size_t n)
return p;
}
-void*
+void *
zalloc(size_t n)
{
- void* p;
+ void *p;
p = malloc(n);
if (!p)
@@ -52,8 +52,8 @@ zalloc(size_t n)
return p;
}
-void*
-xrealloc(void* p, size_t n)
+void *
+xrealloc(void *p, size_t n)
{
p = realloc(p, n);
if (!p)
@@ -62,8 +62,8 @@ xrealloc(void* p, size_t n)
return p;
}
-void*
-xreallocarray(void* p, size_t n, size_t m)
+void *
+xreallocarray(void *p, size_t n, size_t m)
{
p = reallocarray(p, n, m);
if (!p)
@@ -72,10 +72,10 @@ xreallocarray(void* p, size_t n, size_t m)
return p;
}
-char*
-xmemdup(const char* s, size_t n)
+char *
+xmemdup(const char *s, size_t n)
{
- char* p;
+ char *p;
p = xmalloc(n);
memcpy(p, s, n);
@@ -83,24 +83,24 @@ xmemdup(const char* s, size_t n)
return p;
}
-char*
-xstrdup(const char* s)
+char *
+xstrdup(const char *s)
{
return xmemdup(s, strlen(s) + 1);
}
void
-htabkey(struct hashtablekey* k, const char* s, size_t n)
+htabkey(struct hashtablekey *k, const char *s, size_t n)
{
k->str = s;
k->len = n;
k->hash = murmurhash64a(s, n);
}
-struct hashtable*
+struct hashtable *
mkhtab(size_t cap)
{
- struct hashtable* h;
+ struct hashtable *h;
size_t i;
assert(!(cap & (cap - 1)));
@@ -116,7 +116,7 @@ mkhtab(size_t cap)
}
void
-delhtab(struct hashtable* h, void del(void*))
+delhtab(struct hashtable *h, void del(void *))
{
size_t i;
@@ -134,7 +134,7 @@ delhtab(struct hashtable* h, void del(void*))
}
static bool
-keyequal(struct hashtablekey* k1, struct hashtablekey* k2)
+keyequal(struct hashtablekey *k1, struct hashtablekey *k2)
{
if (k1->hash != k2->hash || k1->len != k2->len)
return false;
@@ -142,7 +142,7 @@ keyequal(struct hashtablekey* k1, struct hashtablekey* k2)
}
static size_t
-keyindex(struct hashtable* h, struct hashtablekey* k)
+keyindex(struct hashtable *h, struct hashtablekey *k)
{
size_t i;
@@ -152,11 +152,11 @@ keyindex(struct hashtable* h, struct hashtablekey* k)
return i;
}
-void**
-htabput(struct hashtable* h, struct hashtablekey* k)
+void **
+htabput(struct hashtable *h, struct hashtablekey *k)
{
- struct hashtablekey* oldkeys;
- void** oldvals;
+ struct hashtablekey *oldkeys;
+ void **oldvals;
size_t i, j, oldcap;
if (h->cap / 2 < h->len) {
@@ -188,8 +188,8 @@ htabput(struct hashtable* h, struct hashtablekey* k)
return &h->vals[i];
}
-void*
-htabget(struct hashtable* h, struct hashtablekey* k)
+void *
+htabget(struct hashtable *h, struct hashtablekey *k)
{
size_t i;
@@ -198,7 +198,7 @@ htabget(struct hashtable* h, struct hashtablekey* k)
}
uint64_t
-murmurhash64a(const void* ptr, size_t len)
+murmurhash64a(const void *ptr, size_t len)
{
const uint64_t seed = 0xdecafbaddecafbadull;
const uint64_t m = 0xc6a4a7935bd1e995ull;