Skip to content

Commit

Permalink
Change back interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bynect committed Aug 29, 2022
1 parent b190ba8 commit 07aa013
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
21 changes: 14 additions & 7 deletions src/x86_64_asm.c → src/amd64.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
#include "bfir.h"
#include "label.h"
#include "out.h"
#include "amd64.h"

#define CELLP "rbx"
#define CELLN 30000

void x86_64_asm_instr(Out_Channel *out, Bfir_Instr *instr, Label_Stack *stack) {
static void amd64_asm_instr(Out_Channel *out, Bfir_Instr *instr, Label_Stack *stack) {
uint32_t label;
switch (instr->kind) {
case BFIR_ADD:
Expand Down Expand Up @@ -54,7 +55,7 @@ void x86_64_asm_instr(Out_Channel *out, Bfir_Instr *instr, Label_Stack *stack) {
}
}

bool x86_64_asm_emit(Out_Channel *out, Bfir_Entry *entry, Label_Stack *stack) {
static void amd64_asm_entry(Out_Channel *out, Bfir_Entry *entry, Label_Stack *stack) {
out_print(out, "\tbits 64\n");
out_print(out, "\textern putchar\n");
out_print(out, "\textern getchar\n\n");
Expand All @@ -80,7 +81,7 @@ bool x86_64_asm_emit(Out_Channel *out, Bfir_Entry *entry, Label_Stack *stack) {

Bfir_Instr *instr = bfir_entry_get(entry, entry->head);
while (true) {
x86_64_asm_instr(out, instr, stack);
amd64_asm_instr(out, instr, stack);
if (instr->next == 0) break;
instr = bfir_entry_get(entry, instr->next);
}
Expand All @@ -90,11 +91,17 @@ bool x86_64_asm_emit(Out_Channel *out, Bfir_Entry *entry, Label_Stack *stack) {
out_print(out, "\tpop rbx\n");
out_print(out, "\tpop rbp\n");
out_print(out, "\tret\n");
}

static bool amd64_asm_emit(Back_Emitter *back, Out_Channel *out, Bfir_Entry *entry) {
assert(back->sign.quad == 0x866400aa);

amd64_asm_entry(out, entry, ((Amd64_Asm_Emitter *)back)->stack);
return true;
}

Bflc_Back x86_64_asm_back = {
"x86_64_asm",
x86_64_asm_emit,
};
void amd64_asm_init(Amd64_Asm_Emitter *back, Label_Stack *stack) {
back->back.sign.quad = 0x866400aa;
back->back.emit_f = amd64_asm_emit;
back->stack = stack;
}
14 changes: 14 additions & 0 deletions src/amd64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef AMD64_ASM_H
#define AMD64_ASM_H

#include "back.h"
#include "label.h"

typedef struct {
Back_Emitter back;
Label_Stack *stack;
} Amd64_Asm_Emitter;

void amd64_asm_init(Amd64_Asm_Emitter *back, Label_Stack *stack);

#endif
17 changes: 12 additions & 5 deletions src/back.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
#include "label.h"
#include "out.h"

typedef bool (Bflc_Back_Emit_F)(Out_Channel *out, Bfir_Entry *entry, Label_Stack *stack);
typedef union {
uint8_t bytes[8];
uint64_t quad;
} Signature;

typedef struct {
const char *name;
Bflc_Back_Emit_F *emit_f;
} Bflc_Back;
struct Back_Emitter;

typedef bool (Back_Emitter_F)(struct Back_Emitter *back, Out_Channel *out, Bfir_Entry *entry);

typedef struct Back_Emitter {
Signature sign;
Back_Emitter_F *emit_f;
} Back_Emitter;

#endif
8 changes: 0 additions & 8 deletions src/x86_64_asm.h

This file was deleted.

0 comments on commit 07aa013

Please sign in to comment.