Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

various cleanups and improvements.

  • Loading branch information...
commit 668456ebf9f09547753a622b3a9cf13650f92bb7 1 parent 940112b
@kjs kjs authored
Showing with 34 additions and 239 deletions.
  1. +7 −4 src/compiler.h
  2. +3 −1 src/gencode.c
  3. +14 −233 src/instr.c
  4. +10 −1 src/instr.h
View
11 src/compiler.h
@@ -9,6 +9,7 @@
#include <stdio.h>
+
/* needed for declaring yyscan_t as a member of compiler struct below. */
#ifndef YY_TYPEDEF_YY_SCANNER_T
# define YY_TYPEDEF_YY_SCANNER_T
@@ -25,9 +26,9 @@ typedef struct M1_compiler {
unsigned int warnings;
struct m1_chunk *ast; /* root of the AST */
- int constindex; /* constant table index counter */
- int label; /* label generator */
- int regs[NUM_TYPES]; /* for the register allocator */
+ unsigned int constindex; /* constant table index counter */
+ unsigned int label; /* label generator */
+ unsigned int regs[NUM_TYPES]; /* for the register allocator */
int expect_usertype; /* identifiers can be types or identifiers.
Keep track what the lexer should return (TK_IDENT or TK_USERTYPE) */
@@ -48,13 +49,15 @@ typedef struct M1_compiler {
struct m1_symboltable *globalsymtab; /* to store function names */
- int enum_const_counter; /* for parsing enums that don't specify values. */
+ unsigned int enum_const_counter; /* for parsing enums that don't specify values. */
char registers[REG_TYPE_NUM][REG_NUM]; /* register allocation system. */
int no_reg_opt; /* command-line option to turn off register allocator. */
+ /* code generator fields. */
FILE *outfile;
+ struct m0_instr *lastgenerated;
} M1_compiler;
View
4 src/gencode.c
@@ -54,7 +54,9 @@ static unsigned gencode_obj(M1_compiler *comp, m1_object *obj, m1_object **paren
static const char type_chars[REG_TYPE_NUM] = {'i', 'n', 's', 'p'};
static const char reg_chars[REG_TYPE_NUM] = {'I', 'N', 'S', 'P'};
-#define INS(opcode, format, ...) instr(comp, opcode, format, ##__VA_ARGS__)
+#define INS(opcode, format, ...) mk_instr(comp, opcode, format, ##__VA_ARGS__)
+#define CHUNK() mk_chunk(comp)
+
static void
reset_reg(M1_compiler *comp) {
View
247 src/instr.c
@@ -52,214 +52,11 @@ char const * const m0_instr_names[] = {
};
-static const char types[REG_TYPE_NUM] = {'i', 'n', 's', 'p'};
static const char regs[REG_TYPE_NUM + 1] = {'I', 'N', 'S', 'P', ' '};
#define OUT stdout
-/*
-static void
-write_noop(m0_instr *i) {
- fprintf(OUT, "noop");
-}
-
-static void
-write_goto(m0_instr *i) {
- fprintf(OUT, "goto");
-}
-
-static void
-write_goto_if(m0_instr *i) {
- fprintf(OUT, "goto_if");
-}
-
-static void
-write_goto_chunk(m0_instr *i) {
- fprintf(OUT, "goto_chunk");
-}
-
-static void
-write_add_i(m0_instr *i) {
- fprintf(OUT, "add_i");
-}
-
-static void
-write_add_n(m0_instr *i) {
- fprintf(OUT, "add_n");
-}
-
-static void
-write_sub_i(m0_instr *i) {
- fprintf(OUT, "sub_i");
-}
-
-static void
-write_sub_n(m0_instr *i) {
- fprintf(OUT, "sub_n");
-}
-
-static void
-write_mult_i(m0_instr *i) {
- fprintf(OUT, "mult_i");
-}
-
-static void
-write_mult_n(m0_instr *i) {
- fprintf(OUT, "mult_n");
-}
-
-static void
-write_div_i(m0_instr *i) {
- fprintf(OUT, "div_i");
-}
-
-static void
-write_div_n(m0_instr *i) {
- fprintf(OUT, "div_n");
-}
-
-static void
-write_mod_i(m0_instr *i) {
- fprintf(OUT, "mod_i");
-}
-
-static void
-write_mod_n(m0_instr *i) {
- fprintf(OUT, "mod_n");
-}
-
-static void
-write_iton(m0_instr *i) {
- fprintf(OUT, "iton");
-}
-static void
-write_ntoi(m0_instr *i) {
- fprintf(OUT, "ntoi");
-}
-static void
-write_ashr(m0_instr *i) {
- fprintf(OUT, "ashr");
-}
-static void
-write_lshr(m0_instr *i) {
- fprintf(OUT, "lshr");
-}
-static void
-write_shl(m0_instr *i) {
- fprintf(OUT, "shl");
-}
-
-static void
-write_and(m0_instr *i) {
- fprintf(OUT, "and");
-}
-
-static void
-write_or(m0_instr *i) {
- fprintf(OUT, "or");
-}
-
-static void
-write_xor(m0_instr *i) {
- fprintf(OUT, "xor");
-}
-
-static void
-write_gc_alloc(m0_instr *i) {
- fprintf(OUT, "gc_alloc");
-}
-
-static void
-write_sys_alloc(m0_instr *i) {
- fprintf(OUT, "sys_alloc");
-}
-static void
-write_sys_free(m0_instr *i) {
- fprintf(OUT, "sys_free");
-}
-
-static void
-write_copy_mem(m0_instr *i) {
- fprintf(OUT, "copy_mem");
-}
-
-static void
-write_set(m0_instr *i) {
- fprintf(OUT, "set");
-}
-
-static void
-write_set_imm(m0_instr *i) {
- fprintf(OUT, "set_imm");
-}
-
-static void
-write_deref(m0_instr *i) {
- fprintf(OUT, "deref");
-}
-
-static void
-write_set_ref(m0_instr *i) {
- fprintf(OUT, "set_ref");
-}
-
-static void
-write_set_byte(m0_instr *i) {
- fprintf(OUT, "set_byte");
-}
-static void
-write_get_byte(m0_instr *i) {
- fprintf(OUT, "get_byte");
-}
-static void
-write_set_word(m0_instr *i) {
- fprintf(OUT, "set_word");
-}
-static void
-write_get_word(m0_instr *i) {
- fprintf(OUT, "get_word");
-}
-static void
-write_csym(m0_instr *i) {
- fprintf(OUT, "csym");
-}
-
-static void
-write_ccall_arg(m0_instr *i) {
- fprintf(OUT, "ccall_arg");
-}
-
-static void
-write_ccall_ret(m0_instr *i) {
- fprintf(OUT, "ccall_ret");
-}
-
-static void
-write_ccall(m0_instr *i) {
- fprintf(OUT, "ccall");
-}
-
-static void
-write_print_s(m0_instr *i) {
- fprintf(OUT, "print_s");
-}
-static void
-write_print_i(m0_instr *i) {
- fprintf(OUT, "print_i");
-}
-
-static void
-write_print_n(m0_instr *i) {
- fprintf(OUT, "print_n");
-}
-
-static void
-write_exit(m0_instr *i) {
- fprintf(OUT, "exit");
-}
-*/
-
static void
@@ -311,7 +108,7 @@ write_instructions(M1_compiler *comp, m0_instr *i) {
}
m0_instr *
-instr(M1_compiler *comp, m0_opcode opcode, char const * const format, ...) {
+mk_instr(M1_compiler *comp, m0_opcode opcode, char const * const format, ...) {
va_list argp;
char const *p;
int index = 0;
@@ -330,9 +127,8 @@ instr(M1_compiler *comp, m0_opcode opcode, char const * const format, ...) {
for (p = format; *p != '\0'; p++) {
- if (*p != '%') {
- continue;
- }
+ if (*p != '%')
+ continue;
switch (*++p) {
case 'N':
@@ -367,39 +163,24 @@ instr(M1_compiler *comp, m0_opcode opcode, char const * const format, ...) {
ins->numops = index;
ins->next = NULL;
- va_end(argp);
-
- return ins;
-}
-
-
-
-
-/*
-void
-ins_label(M1_compiler *comp, unsigned labelno) {
+ /* if not first generated instruction, then set that item's next to the new instr. */
+ if (comp->lastgenerated != NULL)
+ comp->lastgenerated->next = ins;
+ /* update last generated instruction for fast access. */
+ comp->lastgenerated = ins;
-}
+ va_end(argp);
-void
-ins_goto_if(M1_compiler *comp, unsigned labelno, char regtype, char regno) {
+ return ins;
}
-void
-ins_goto(M1_compiler *comp, unsigned labelno) {
-
-}
-void
-ins_set_imm(M1_compiler *comp, m1_reg *target, unsigned num256, unsigned numlt256) {
-
+m0_chunk *
+mk_chunk(M1_compiler *comp) {
+ m0_chunk *ch = NULL;
+ return ch;
}
-void
-ins_deref(M1_compiler *comp, m1_reg *target, M0_alias obj, m1_reg *index) {
-
-}
-*/
View
11 src/instr.h
@@ -98,7 +98,16 @@ typedef struct m0_instr {
struct m0_instr *next;
} m0_instr;
-extern m0_instr *instr(M1_compiler *comp, m0_opcode, char const * const format, ...);
+typedef struct m0_chunk {
+
+ m0_instr *instructions;
+
+ struct m0_chunk *next;
+
+} m0_chunk;
+
+extern m0_chunk *mk_chunk(M1_compiler *comp);
+extern m0_instr *mk_instr(M1_compiler *comp, m0_opcode, char const * const format, ...);
#endif

0 comments on commit 668456e

Please sign in to comment.
Something went wrong with that request. Please try again.