aboutsummaryrefslogtreecommitdiff
path: root/minias.h
blob: 5e456fa407fd45dec54d27efc69e824ca1305be3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <assert.h>
#include <elf.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef struct {
  Elf64_Shdr hdr;
  int16_t idx;
  int64_t wco;
  int64_t offset;
  size_t capacity;
  uint8_t *data;
} Section;

typedef struct {
  const char *name;
  int64_t offset;
  int64_t size;
  int global;
  Section *section;
} Symbol;

typedef enum {
  // Misc
  ASM_SYNTAX_ERROR,
  ASM_BLANK,
  ASM_LABEL,
  ASM_IMM,
  ASM_IDENT,
  ASM_NUMBER,
  ASM_MEMARG,
  // Directives
  ASM_DIR_GLOBL,
  ASM_DIR_DATA,
  ASM_DIR_TEXT,
  ASM_DIR_BYTE,
  ASM_DIR_BALIGN,
  // Instructions
  ASM_NOP,
  ASM_RET,
  ASM_JMP,
  ASM_LEAVE,
  ASM_ADD,
  ASM_AND,
  ASM_LEA,
  ASM_MOV,
  ASM_OR,
  ASM_SUB,
  ASM_XOR,
  // Registers, order matters.
  ASM_REG_BEGIN,
  ASM_EAX,
  ASM_ECX,
  ASM_EDX,
  ASM_EBX,
  ASM_ESP,
  ASM_EBP,
  ASM_ESI,
  ASM_EDI,
  ASM_RAX,
  ASM_RCX,
  ASM_RDX,
  ASM_RBX,
  ASM_RSP,
  ASM_RBP,
  ASM_RSI,
  ASM_RDI,
  ASM_R8,
  ASM_R9,
  ASM_R10,
  ASM_R11,
  ASM_R12,
  ASM_R13,
  ASM_R14,
  ASM_R15,
  ASM_REG_END,
} AsmKind;

typedef union Parsev Parsev;

typedef struct {
  AsmKind kind;
  const char *name;
} Label;

typedef struct {
  AsmKind kind;
  const char *name;
} Globl;

typedef struct {
  AsmKind kind;
  uint8_t b;
} Byte;

typedef struct {
  AsmKind kind;
  uint64_t align;
} Balign;

typedef struct {
  AsmKind kind;
  const char *l; /* label */
  int64_t c;     /* constant */
} Imm;

typedef struct {
  AsmKind kind;
  AsmKind reg;
  const char *l; /* label */
  int64_t c;     /* constant */
} Memarg;

typedef struct {
  AsmKind kind;
  const char *name;
} Ident;

typedef struct {
  AsmKind kind;
  int64_t v;
} Number;

typedef struct {
  AsmKind kind;
  const char *target;
} Jmp;

typedef struct {
  AsmKind kind;
  char type;
  Parsev *src;
  Parsev *dst;
} ModRMBinop;

typedef ModRMBinop Add;
typedef ModRMBinop And;
typedef ModRMBinop Lea;
typedef ModRMBinop Mov;
typedef ModRMBinop Or;
typedef ModRMBinop Sub;
typedef ModRMBinop Xor;

union Parsev {
  AsmKind kind;
  Label label;
  Globl globl;
  Balign balign;
  Memarg memarg;
  ModRMBinop modrmbinop;
  Add add;
  And and;
  Lea lea;
  Mov mov;
  Or or;
  Xor xor;
  Sub sub;
  Jmp jmp;
  Byte byte;
  Imm imm;
  Ident ident;
  Number number;
};

typedef struct AsmLine AsmLine;
struct AsmLine {
  int64_t lineno;
  Parsev v;
  AsmLine *next;
};

/* util.c */

void fatal(const char *fmt, ...);

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 {
  uint64_t hash;
  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);