aboutsummaryrefslogtreecommitdiff
path: root/executor/x86_64/executor.c
blob: 335790a9fdd5c2582bfb5b94c94860092852fab5 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "../executor.h"
#include "../../include/std/alloc.h"
#include "../../include/std/buffer.h"
#include "asm.h"
#include <assert.h>

/*
    TODO: Currently almost all operations are performed on memory. This should be optimized
    to take advantage of registers.

    TODO: Operations with memory registers could access outside the stack. Should this be checked?
*/

typedef struct {
    u32 asm_index;
    u16 func_index;
} CallDefer;

typedef struct {
    Asm asm;
    usize *function_indices;
    u16 num_functions;
    u16 func_counter;
    Buffer/*CallDefer*/ call_defer;
} amal_executor_impl;

#define IMPL \
    amal_executor_impl *impl; \
    impl = (amal_executor_impl*)self;

/*
    @reg will be a positive value when accessing local variables, in which case the first
    local variable is located at -sizeof(usize) and the next one is at -(2 * sizeof(usize)).
    @reg will be a negative value starting at -1 when accessing parameters.
    The first parameter is located at 3*sizeof(usize) and the next one is at 4*sizeof(usize).
    Parameter starts at 3*sizeof(usize) because offset 0 is the return address, offset 1*sizeof(usize) is the
    saved RBP and 2*sizeof(usize) is saved RBX.
    TODO: Use different offset when saving more registers, for example on Microsoft Windows.
*/
#define get_register_stack_offset(reg) \
    (reg >= 0 ? (i32)(-reg * (int)sizeof(usize) - sizeof(usize)) : (i32)(-reg * (int)sizeof(usize) + 2*sizeof(usize)))

static i64 abs_i64(i64 value) {
    return value >= 0 ? value : -value;
}

int amal_executor_init(amal_executor **self) {
    amal_executor_impl **impl;
    impl = (amal_executor_impl**)self;
    *impl = NULL;
    return_if_error(am_malloc(sizeof(amal_executor_impl), (void**)impl));
    (*impl)->function_indices = NULL;
    (*impl)->num_functions = 0;
    (*impl)->func_counter = 0;
    ignore_result_int(buffer_init(&(*impl)->call_defer, NULL));
    return asm_init(&(*impl)->asm);
}

void amal_executor_deinit(amal_executor *self) {
    IMPL
    buffer_deinit(&impl->call_defer);
    am_free(impl->function_indices);
    asm_deinit(&impl->asm);
    am_free(impl);
}

int amal_executor_run(amal_executor *self, u32 offset) {
    IMPL
    return asm_execute(&impl->asm, offset);
}

u32 amal_exec_get_code_offset(amal_executor *self) {
    IMPL
    return asm_get_size(&impl->asm);
}

int amal_executor_instructions_start(amal_executor *self, u16 num_functions) {
    void *new_data;
    IMPL
    return_if_error(am_realloc(impl->function_indices, num_functions * sizeof(*impl->function_indices), &new_data));
    impl->function_indices = new_data;
    impl->num_functions = num_functions;
    impl->func_counter = 0;
    buffer_clear(&impl->call_defer);
    return 0;
}

int amal_executor_instructions_end(amal_executor *self) {
    CallDefer *call_defer, *call_defer_end;
    IMPL

    call_defer = buffer_begin(&impl->call_defer);
    call_defer_end = buffer_end(&impl->call_defer);
    for(; call_defer != call_defer_end; ++call_defer) {
        const isize func_offset = (isize)impl->function_indices[call_defer->func_index] - (isize)call_defer->asm_index;
        asm_override_call_rel32(&impl->asm, call_defer->asm_index, func_offset);
    }
    return 0;
}

int amal_exec_nop(amal_executor *self) {
    IMPL
    return asm_nop(&impl->asm);
}

int amal_exec_setz(amal_executor *self, i8 dst_reg) {
    AsmPtr dst;
    IMPL
    asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg));
    return asm_mov_mi(&impl->asm, &dst, 0);
}

int amal_exec_mov(amal_executor *self, i8 dst_reg, i8 src_reg) {
    AsmPtr ptr;
    IMPL

    asm_ptr_init_disp(&ptr, RBP, get_register_stack_offset(src_reg));
    return_if_error(asm_mov_rm(&impl->asm, RAX, &ptr));

    asm_ptr_init_disp(&ptr, RBP, get_register_stack_offset(dst_reg));
    return asm_mov_mr(&impl->asm, &ptr, RAX);
}

int amal_exec_movi(amal_executor *self, i8 dst_reg, i64 imm) {
    IMPL
    /* TODO: if @number is a float then use float instructions */
    if(abs_i64(imm) <= INT32_MAX) {
        AsmPtr dst;
        asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg));
        return_if_error(asm_mov_mi(&impl->asm, &dst, imm));
    } else {
        AsmPtr dst;
        asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg));
        return_if_error(asm_mov_ri(&impl->asm, RAX, imm));
        return_if_error(asm_mov_mr(&impl->asm, &dst, RAX));
    }
    return 0;
}

int amal_exec_movd(amal_executor *self, i8 dst_reg, BufferView data) {
    AsmPtr dst;
    IMPL

    asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg));
    return_if_error(asm_mov_ri(&impl->asm, RAX, (uintptr_t)data.data));
    return asm_mov_mr(&impl->asm, &dst, RAX);
}

int amal_exec_add(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) {
    AsmPtr dst;
    AsmPtr reg1;
    AsmPtr reg2;
    IMPL

    asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg));
    asm_ptr_init_disp(&reg1, RBP, get_register_stack_offset(src_reg1));
    asm_ptr_init_disp(&reg2, RBP, get_register_stack_offset(src_reg2));

    return_if_error(asm_mov_rm(&impl->asm, RAX, &reg1));
    return_if_error(asm_mov_rm(&impl->asm, RCX, &reg2));
    return_if_error(asm_add_rr(&impl->asm, RAX, RCX));
    return asm_mov_mr(&impl->asm, &dst, RAX);
}

int amal_exec_sub(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) {
    AsmPtr dst;
    AsmPtr reg1;
    AsmPtr reg2;
    IMPL

    asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg));
    asm_ptr_init_disp(&reg1, RBP, get_register_stack_offset(src_reg1));
    asm_ptr_init_disp(&reg2, RBP, get_register_stack_offset(src_reg2));

    return_if_error(asm_mov_rm(&impl->asm, RAX, &reg1));
    return_if_error(asm_mov_rm(&impl->asm, RCX, &reg2));
    return_if_error(asm_sub_rr(&impl->asm, RAX, RCX));
    return asm_mov_mr(&impl->asm, &dst, RAX);
}

int amal_exec_imul(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) {
    AsmPtr dst;
    AsmPtr reg1;
    AsmPtr reg2;
    IMPL

    asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg));
    asm_ptr_init_disp(&reg1, RBP, get_register_stack_offset(src_reg1));
    asm_ptr_init_disp(&reg2, RBP, get_register_stack_offset(src_reg2));

    return_if_error(asm_mov_rm(&impl->asm, RAX, &reg1));
    return_if_error(asm_mov_rm(&impl->asm, RCX, &reg2));
    return_if_error(asm_imul_rr(&impl->asm, RAX, RCX));
    return asm_mov_mr(&impl->asm, &dst, RAX);
}

int amal_exec_mul(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) {
    (void)self;
    (void)dst_reg;
    (void)src_reg1;
    (void)src_reg2;
    /* TODO: Implement! */
    #if 0
    AsmPtr dst;
    AsmPtr reg1;
    AsmPtr reg2;

    asm_ptr_init_disp(&dst, RBP, -(i32)get_register_at_offset(0));
    asm_ptr_init_disp(&reg1, RBP, -(i32)get_register_at_offset(1));
    asm_ptr_init_disp(&reg2, RBP, -(i32)get_register_at_offset(2));

    return_if_error(asm_mov_rm(&self->asm, RAX, &reg1));
    return_if_error(asm_mov_rm(&self->asm, RCX, &reg2));
    return_if_error(asm_mul_rr(&self->asm, RAX, RCX));
    return_if_error(asm_mov_mr(&self->asm, &dst, RAX));
    #endif
    assert(bool_false && "TODO: Implement!");
    return 0;
}

int amal_exec_idiv(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) {
    AsmPtr dst;
    AsmPtr reg1;
    AsmPtr reg2;
    IMPL

    asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg));
    asm_ptr_init_disp(&reg1, RBP, get_register_stack_offset(src_reg1));
    asm_ptr_init_disp(&reg2, RBP, get_register_stack_offset(src_reg2));

    return_if_error(asm_mov_rm(&impl->asm, RAX, &reg1));
    return_if_error(asm_mov_rm(&impl->asm, RCX, &reg2));
    return_if_error(asm_cqo(&impl->asm));
    /* TODO: Preserve RDX if needed, since it's also used as a parameter in system-v x86_64 abi */
    return_if_error(asm_idiv_rr(&impl->asm, RCX));
    return asm_mov_mr(&impl->asm, &dst, RAX);
}

int amal_exec_div(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) {
    (void)self;
    (void)dst_reg;
    (void)src_reg1;
    (void)src_reg2;
    /* TODO: Implement! */
    assert(bool_false && "TODO: Implement!");
    return 0;
}

int amal_exec_push(amal_executor *self, i8 reg) {
    AsmPtr reg_ptr;
    IMPL

    asm_ptr_init_disp(&reg_ptr, RBP, get_register_stack_offset(reg));
    return_if_error(asm_mov_rm(&impl->asm, RAX, &reg_ptr));
    return asm_pushr(&impl->asm, RAX);
}

int amal_exec_pushi(amal_executor *self, i64 imm) {
    (void)self;
    (void)imm;
    /* TODO: Implement! */
    assert(bool_false && "TODO: Implement!");
    return 0;
}

int amal_exec_pushd(amal_executor *self, BufferView data) {
    (void)self;
    (void)data;
    /* TODO: Implement! */
    assert(bool_false && "TODO: Implement!");
    return 0;
}

int amal_exec_call(amal_executor *self, u16 func_index, u8 num_args, i8 dst_reg) {
    isize asm_size;
    IMPL
    /* TODO: Preserve necessary registers before call? */
    /* TODO: This assumes all arguments are isize */
    asm_size = asm_get_size(&impl->asm);
    if(func_index < impl->func_counter) {
        return_if_error(asm_call_rel32(&impl->asm, (isize)impl->function_indices[func_index] - asm_size));
    } else {
        /*
            The location of the function has not been defined yet. Use call instruction with dummy data and change
            the location once the location to the function is known
        */
        CallDefer call_defer;
        call_defer.asm_index = asm_size;
        call_defer.func_index = func_index;
        return_if_error(buffer_append(&impl->call_defer, &call_defer, sizeof(call_defer)));
        return_if_error(asm_call_rel32(&impl->asm, 0));
    }

    {
        AsmPtr dst;
        asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg));
        /* TODO: Make this work when result is not stored in RAX (multiple return results) */
        return_if_error(asm_mov_mr(&impl->asm, &dst, RAX));
    }
    if(num_args > 0)
        return asm_add_rm64_imm(&impl->asm, RSP, num_args * sizeof(isize));
    return 0;
}

const Reg64 SYS_V_PARAM_REGS[] = { RDI, RSI, RDX, RCX };

/*
    TODO: Make argument passing work for different calling conventions and different ABI.
    This currently assumes x86_64 system v abi.
    System-V ABI parameters:
     	RDI, RSI, RDX, RCX, R8, R9, XMM0–7.
        The rest are passed in the stack.
*/
/* TODO: Make this work when function returns something else than a POD */
int amal_exec_calle(amal_executor *self, void *func, u8 num_args, i8 dst_reg) {
    AsmPtr dst;
    IMPL

    /* TODO: Support R and XMM registers so more than 5 arguments can be used for functions */
    assert(num_args < 5);
    {
        /*
            TODO: Do this directly in @PUSH instruction instead. For now we copy
            the pushed data to the registers that need to be set for the specific abi for parameters
        */
        int i;
        AsmPtr src;
        asm_ptr_init_disp(&src, RSP, 0);
        for(i = num_args - 1; i >= 0; --i) {
            return_if_error(asm_mov_rm(&impl->asm, SYS_V_PARAM_REGS[i], &src));
            src.disp += 0x8;
        }
    }

    /* TODO: Preserve necessary registers before call? */
    /* TODO: This assumes all arguments are isize */
    return_if_error(asm_mov_ri(&impl->asm, RAX, (intptr_t)func));
    return_if_error(asm_callr(&impl->asm, RAX));
    asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg));
    return_if_error(asm_mov_mr(&impl->asm, &dst, RAX));
    if(num_args > 0)
        return asm_add_rm64_imm(&impl->asm, RSP, num_args * sizeof(isize));
    return 0;
}

/*
int amal_exec_callr(i8 dst_reg, BufferView data) {

}
*/

int amal_exec_cmp(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) {
    (void)self;
    (void)dst_reg;
    (void)src_reg1;
    (void)src_reg2;
    /* TODO: Implement! */
    assert(bool_false && "TODO: Implement!");
    return 0;
}

int amal_exec_jz(amal_executor *self, i8 dst_reg, i16 offset) {
    (void)self;
    (void)dst_reg;
    (void)offset;
    /* TODO: Implement! */
    assert(bool_false && "TODO: Implement!");
    return 0;
}

int amal_exec_jmp(amal_executor *self, i16 offset) {
    (void)self;
    (void)offset;
    /* TODO: Implement! */
    assert(bool_false && "TODO: Implement!");
    return 0;
}

int amal_exec_ret(amal_executor *self, i8 reg) {
    AsmPtr ret_reg;
    IMPL

    asm_ptr_init_disp(&ret_reg, RBP, get_register_stack_offset(reg));
    /* Result is returned in RAX register. TODO: Make this work when returning more than one result */
    return_if_error(asm_mov_rm(&impl->asm, RAX, &ret_reg));
    return amal_exec_func_end(self);
}

int amal_exec_func_start(amal_executor *self, u16 num_regs) {
    /*
        TODO: Validate stack size, or maybe remove all validation? do we really need validation?
        If we need security, we could fork the process instead.
    */

    /*
        Some registers need to be preserved before entering a function scope and these registers are different on different platforms.
        32-bit: EBX, ESI, EDI, EBP
        64-bit Windows: RBX, RSI, RDI, RBP, R12-R15, XMM6-XMM15
        64-bit Linux,BSD,Mac: RBX, RBP, R12-R15
    */
    IMPL
    impl->function_indices[impl->func_counter++] = asm_get_size(&impl->asm);
    return_if_error(asm_pushr(&impl->asm, RBX));
    return_if_error(asm_pushr(&impl->asm, RBP));
    return_if_error(asm_mov_rr(&impl->asm, RBP, RSP));
    return asm_sub_rm64_imm(&impl->asm, RSP, num_regs * sizeof(isize));
}

int amal_exec_func_end(amal_executor *self) {
    IMPL
    return_if_error(asm_mov_rr(&impl->asm, RSP, RBP));
    return_if_error(asm_popr(&impl->asm, RBP));
    return_if_error(asm_popr(&impl->asm, RBX));
    return asm_ret(&impl->asm, 0);
}