undar-lang/emit/rer/emit.c

671 lines
8.7 KiB
C

#include "../../emit.h"
#include <stdio.h>
#include <stdlib.h>
#define RER_OP_2 (1 << 5)
#define RER_OP_R (1 << 6)
#define RER_OP_K (1 << 7)
enum rer_opcode {
/* Stack I */ /* Logic */ /* Memory I */ /* Arithmetic*/
BRK = 0x00, EQU = 0x08, LDZ = 0x10, ADD = 0x18,
INC = 0x01, NEQ = 0x09, STZ = 0x11, SUB = 0x19,
POP = 0x02, GTH = 0x0A, LDR = 0x12, MUL = 0x1A,
NIP = 0x03, LTH = 0x0B, STR = 0x13, DIV = 0x1B,
/* Stack II */ /* Stash */ /* Memory II */ /* Bitwise */
SWP = 0x04, JMP = 0x0C, LDA = 0x14, AND = 0x1C,
ROT = 0x05, JCN = 0x0D, STA = 0x15, ORA = 0x1D,
DUP = 0x06, JSR = 0x0E, DEI = 0x16, EOR = 0x1E,
OVR = 0x07, STH = 0x0F, DEO = 0x17, SFT = 0x1F,
LIT = 0x80, JCI = 0x20, JMI = 0x40, JSI = 0x60,
};
void
rer_emit_error(const char *str, i32 length, i32 line)
{
printf("Error at line: %d > %.*s\n ", line, length, str);
exit(1);
}
void
rer_mem(u32 a)
{
USED(a);
}
void
rer_prolog()
{
printf("|100\n");
}
void
rer_epilogue()
{
printf("\n\n@str/<print> ( str* -- )\n\
LDAk DUP ?{ POP POP2 JMP2r }\n\
#18 DEO\n\
INC2 !/<print>\n");
}
void
rer_emit_add()
{
printf("ADD2 ");
}
void
rer_emit_sub()
{
printf("SUB2 ");
}
void
rer_emit_mul()
{
printf("MUL2 ");
}
void
rer_emit_div()
{
printf("DIV2 ");
}
void
rer_emit_lt()
{
printf("LTH2 ");
}
void
rer_emit_le()
{
printf("ROT SWP LTH ?{ LTH #00 EQU JMPr } GTH JMPr ");
}
void
rer_emit_gt()
{
printf("GTH2 ");
}
void
rer_emit_ge()
{
printf("ROT SWP GTH ?{ GTH #00 EQU JMPr } LTH JMPr ");
}
void
rer_emit_ne()
{
printf("NEQ2 ");
}
void
rer_emit_eq()
{
printf("EQU2 ");
}
void
rer_emit_false()
{
printf("#0000 ");
}
void
rer_emit_true()
{
printf("#0001 ");
}
void
rer_emit_nil()
{
printf("#0000 ");
}
void
rer_emit_neg()
{
printf("#0000 SUB2 ");
}
void
rer_emit_not()
{
printf("#FFFF EOR2 ");
}
void
rer_emit_void()
{
}
i32
rer_emit_bool_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $1 } ", length, str);
else
printf("!{ @%.*s $1 } ", length, str);
return 1;
}
i32
rer_emit_byte_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $1 } ", length, str);
else
printf("!{ @%.*s $1 } ", length, str);
return 1;
}
i32
rer_emit_int_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $2 } ", length, str);
else
printf("!{ @%.*s $2 } ", length, str);
return 2;
}
i32
rer_emit_nat_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $2 } ", length, str);
else
printf("!{ @%.*s $2 } ", length, str);
return 2;
}
i32
rer_emit_real_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $2 } ", length, str);
else
printf("!{ @%.*s $2 } ", length, str);
return 2;
}
i32
rer_emit_str_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $2 } ", length, str);
else
printf("!{ @%.*s $2 } ", length, str);
return 2;
}
i32
rer_emit_u8_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $1 } ", length, str);
else
printf("!{ @%.*s $1 } ", length, str);
return 1;
}
i32
rer_emit_i8_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $1 } ", length, str);
else
printf("!{ @%.*s $1 } ", length, str);
return 1;
}
i32
rer_emit_i16_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $2 } ", length, str);
else
printf("!{ @%.*s $2 } ", length, str);
return 2;
}
i32
rer_emit_u16_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $2 } ", length, str);
else
printf("!{ @%.*s $2 } ", length, str);
return 2;
}
i32
rer_emit_i32_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $4 } ", length, str);
else
printf("!{ @%.*s $4 } ", length, str);
return 4;
}
i32
rer_emit_u32_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $4 } ", length, str);
else
printf("!{ @%.*s $4 } ", length, str);
return 4;
}
i32
rer_emit_f32_type(const char *str, i32 length, bool local)
{
if(local)
printf("!{ &%.*s $4 } ", length, str);
else
printf("!{ @%.*s $4 } ", length, str);
return 4;
}
void
rer_emit_int(const char *str, i32 length)
{
i32 i = (i32)strtol(str, nil, 10);
printf("#%04x ", i);
USED(length);
}
void
rer_emit_nat(const char *str, i32 length)
{
u32 i = (u32)strtol(str, nil, 10);
printf("#%04x ", i);
USED(length);
}
void
rer_emit_real(const char *str, i32 length)
{
USED(str);
USED(length);
/// TODO: implement this
}
void
rer_emit_byte(const char *str, i32 length)
{
u8 i = (u8)strtol(str, nil, 10);
printf("#%04x ", i);
USED(length);
}
void
rer_emit_str(const char *str, i32 length)
{
/* set a pointer to the string literal and then jump over it */
printf(";{ #0002 ADD2 } !{ ");
for (i32 i = 1; i < length - 1 ; i++) {
printf("#%02x ", str[i]);
}
printf("#00 } ");
}
void
rer_emit_constant(const char *str, i32 length, bool local)
{
if(local)
printf(",/%.*s STR2 ", length, str);
else
printf(";%.*s STA2 ", length, str);
}
void
rer_emit_array()
{
}
void
rer_emit_function()
{
}
void
rer_emit_plex()
{
}
void
rer_emit_method()
{
}
void
rer_emit_trait()
{
}
void
rer_emit_const()
{
}
void
rer_emit_putchar()
{
printf("#18 DEO ");
}
void
rer_emit_getchar()
{
printf("#12 DEI ");
}
void
rer_emit_open_paren()
{
}
void
rer_emit_close_paren()
{
}
void
rer_emit_variable(Symbol *sym, bool local)
{
if(local)
printf(",/%.*s LDR2 ", sym->name_length, sym->name);
else
printf(";%.*s LDA2 ", sym->name_length, sym->name);
}
void
rer_emit_write()
{
}
void
rer_emit_read()
{
}
void
rer_emit_open()
{
}
void
rer_emit_close()
{
}
void
rer_emit_stat()
{
}
void
rer_emit_end_statement()
{
}
void
rer_emit_set_value()
{
}
void
rer_emit_plex_def()
{
}
void
rer_emit_cast_int_to_nat()
{
}
void
rer_emit_cast_int_to_real()
{
}
void
rer_emit_cast_int_to_str()
{
}
void
rer_emit_cast_nat_to_int()
{
}
void
rer_emit_cast_nat_to_real()
{
}
void
rer_emit_cast_nat_to_str()
{
}
void
rer_emit_cast_real_to_int()
{
}
void
rer_emit_cast_real_to_nat()
{
}
void
rer_emit_cast_real_to_str()
{
}
void
rer_emit_strbuf_init()
{
}
void
rer_emit_strbuf_append()
{
}
void
rer_emit_strbuf_to_str()
{
}
void
rer_emit_arena_fn_call()
{
}
void
rer_emit_arena_fn_return_plex()
{
}
void
rer_emit_arena_fn_return_array()
{
}
void
rer_emit_arena_fn_return_strbuf()
{
}
void
rer_emit_cast_str_to_int()
{
}
void
rer_emit_cast_str_to_nat()
{
}
void
rer_emit_cast_str_to_real()
{
}
void
rer_emit_sll()
{
printf("#40 SFT SFT ");
}
void
rer_emit_srl()
{
printf("SFT ");
}
void
rer_emit_xor()
{
printf("EOR2 ");
}
void
rer_emit_mod()
{
printf("DIV2k MUL2 SUB2 ");
}
void
rer_emit_or()
{
printf("ORA2 ");
}
void
rer_emit_and()
{
printf("AND2 ");
}
void
rer_emit_print()
{
printf("str/<print> ");
}
void
rer_emit_jump()
{
printf("?{ ");
}
void
rer_emit_patch_jump()
{
printf("} ");
}
Emitter
rer_emitter()
{
return (Emitter){
rer_emit_error,
rer_prolog,
rer_epilogue,
rer_emit_add,
rer_emit_sub,
rer_emit_mul,
rer_emit_div,
rer_emit_lt,
rer_emit_le,
rer_emit_gt,
rer_emit_ge,
rer_emit_ne,
rer_emit_eq,
rer_emit_false,
rer_emit_true,
rer_emit_nil,
rer_emit_void,
rer_emit_int,
rer_emit_nat,
rer_emit_real,
rer_emit_byte,
rer_emit_str,
rer_emit_bool_type,
rer_emit_byte_type,
rer_emit_int_type,
rer_emit_nat_type,
rer_emit_real_type,
rer_emit_str_type,
rer_emit_u8_type,
rer_emit_i8_type,
rer_emit_i16_type,
rer_emit_u16_type,
rer_emit_i32_type,
rer_emit_u32_type,
rer_emit_f32_type,
rer_emit_array,
rer_emit_function,
rer_emit_plex,
rer_emit_method,
rer_emit_trait,
rer_emit_const,
rer_emit_print,
rer_emit_putchar,
rer_emit_getchar,
rer_emit_neg,
rer_emit_not,
rer_emit_open_paren,
rer_emit_close_paren,
rer_emit_constant,
rer_emit_variable,
rer_emit_write,
rer_emit_read,
rer_emit_open,
rer_emit_close,
rer_emit_stat,
rer_emit_end_statement,
rer_emit_set_value,
rer_emit_plex_def,
rer_emit_cast_int_to_nat,
rer_emit_cast_int_to_real,
rer_emit_cast_int_to_str,
rer_emit_cast_nat_to_int,
rer_emit_cast_nat_to_real,
rer_emit_cast_nat_to_str,
rer_emit_cast_real_to_int,
rer_emit_cast_real_to_nat,
rer_emit_cast_real_to_str,
rer_emit_strbuf_init,
rer_emit_strbuf_append,
rer_emit_strbuf_to_str,
rer_emit_arena_fn_call,
rer_emit_arena_fn_return_plex,
rer_emit_arena_fn_return_array,
rer_emit_arena_fn_return_strbuf,
rer_emit_cast_str_to_int,
rer_emit_cast_str_to_nat,
rer_emit_cast_str_to_real,
rer_emit_sll,
rer_emit_srl,
rer_emit_xor,
rer_emit_mod,
rer_emit_and,
rer_emit_or,
rer_emit_jump,
rer_emit_patch_jump,
};
}