Skip to content

Commit

Permalink
[cgen] make 'not' work properly; add source line numbers to the comme…
Browse files Browse the repository at this point in the history
…nts in assembly
  • Loading branch information
tanhevg committed Apr 22, 2016
1 parent f4d044d commit 4d965e9
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 189 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ add_custom_command(OUTPUT cool-parse.cc DEPENDS cool.y
add_executable(cool-lexer ${LEXER_SOURCE_FILES})
target_link_libraries(cool-lexer cooldist ${FLEX_LIB})

add_executable(cool-parser ${PARSER_SOURCE_FILES})
target_link_libraries(cool-parser tree semant cgen cooldist tree ${FLEX_LIB})
add_executable(cool-compiler ${PARSER_SOURCE_FILES})
target_link_libraries(cool-compiler tree semant cgen cooldist tree ${FLEX_LIB})

add_executable(test-app test-main.cpp)

21 changes: 14 additions & 7 deletions cgen/cgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "emit.h"
#include "cool-tree.h"
#include "symtab.h"
#include "cgen_helpers.h"


enum Basicness {Basic, NotBasic};
#define TRUE 1
Expand All @@ -23,10 +25,15 @@ class ObjectEnvRecord {
Symbol get_declaring_type() { return declaring_type; }
char* get_reg() {return reg;}
int get_offset() {return offset;}
void code_ref(ostream &str);
ostream & code_store(ostream &str);

ostream & code_load(ostream &str);
template <typename... Ts> ostream & code_store(ostream &str, int line_no, Ts... logs) {
return emit_store(ACC, offset, reg, str, line_no, logs...);
}

template <typename... Ts> ostream & code_load(ostream &str, int line_no, Ts... logs) {
return emit_load(ACC, offset, reg, str, line_no, logs...);
}

};

typedef SymbolTable<Symbol, ObjectEnvRecord> ObjectEnv;
Expand All @@ -52,10 +59,10 @@ class CodeGenerator: public TreeVisitor {
int scope_index;
class__class *current_class;
method_class *current_method;
void emit_function_entry(int tmp_count);
void emit_function_exit(int tmp_count, int parameter_count);
void dispatch(Expression callee, Symbol type, Symbol name, Expressions actuals, int n_temp);
void code_new(Symbol type_name);
void emit_function_entry(int tmp_count, int line_no);
void emit_function_exit(int tmp_count, int parameter_count, int line_no);
void dispatch(int line_no, Expression callee, Symbol type, Symbol name, Expressions actuals, int n_temp);
void code_new(Symbol type_name, int line_no);

public:
CodeGenerator(ClassTable *_class_table, ostream& _str):
Expand Down
183 changes: 92 additions & 91 deletions cgen/cgen_expressions.cc

Large diffs are not rendered by default.

81 changes: 4 additions & 77 deletions cgen/cgen_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//

#include <cgen_gc.h>
#include "emit.h"
#include "cgen_helpers.h"

extern char *gc_collect_names;
Expand Down Expand Up @@ -42,25 +41,7 @@ static void emit_load_address(char *dest_reg, char *address, ostream &s) {
s << LA << dest_reg << " " << address << endl;
}

static void emit_partial_load_address(char *dest_reg, ostream &s) { s << LA << dest_reg << " "; }

ostream & emit_load_bool(char *dest, const BoolConst &b, ostream &s) {
emit_partial_load_address(dest, s);
b.code_ref(s);
return s;
}

ostream & emit_load_string(char *dest, StringEntry *str, ostream &s) {
emit_partial_load_address(dest, s);
str->code_ref(s);
return s;
}

ostream & emit_load_int(char *dest, IntEntry *i, ostream &s) {
emit_partial_load_address(dest, s);
i->code_ref(s);
return s;
}
void emit_partial_load_address(char *dest_reg, ostream &s) { s << LA << dest_reg << " "; }

ostream & emit_move(char *dest_reg, char *source_reg, ostream &s) {
s << MOVE << dest_reg << " " << source_reg;
Expand Down Expand Up @@ -88,28 +69,15 @@ static void emit_sll(char *dest, char *src1, int num, ostream &s) {
s << SLL << dest << " " << src1 << " " << num << endl;
}

ostream & emit_jalr(char *dest, ostream &s) {
s << JALR << dest;
return s;
}

ostream & emit_jal(char *address, ostream &s) {
s << JAL << address;
return s;
}

ostream & emit_return(ostream &s) {
s << RET;
return s;
}

static void emit_gc_assign(ostream &s) { s << JAL << "_GenGC_Assign" << endl; }

void emit_disptable_ref(Symbol sym, ostream &s) { s << sym << DISPTAB_SUFFIX; }

void emit_init_ref(Symbol sym, ostream &s) { s << sym << CLASSINIT_SUFFIX; }

void emit_label_ref(int l, ostream &s) { s << "label" << l; }
ostream & emit_label_ref(int l, ostream &s) { s << "label" << l; return s; }

ostream & emit_label_ref(const char *ls, int ln, ostream &s) { s << ls << ln; return s; }

void emit_protobj_ref(Symbol sym, ostream &s) { s << sym << PROTOBJ_SUFFIX; }

Expand All @@ -122,52 +90,34 @@ static void emit_label_def(int l, ostream &s) {
s << ":" << endl;
}

static void emit_beqz(char *source, int label, ostream &s) {
s << BEQZ << source << " ";
emit_label_ref(label, s);
s << endl;
}

static void emit_beq(char *src1, char *src2, int label, ostream &s) {
s << BEQ << src1 << " " << src2 << " ";
emit_label_ref(label, s);
s << endl;
}

static void emit_bne(char *src1, char *src2, int label, ostream &s) {
s << BNE << src1 << " " << src2 << " ";
emit_label_ref(label, s);
s << endl;
}

static void emit_bleq(char *src1, char *src2, int label, ostream &s) {
s << BLEQ << src1 << " " << src2 << " ";
emit_label_ref(label, s);
s << endl;
}

static void emit_blt(char *src1, char *src2, int label, ostream &s) {
s << BLT << src1 << " " << src2 << " ";
emit_label_ref(label, s);
s << endl;
}

static void emit_blti(char *src1, int imm, int label, ostream &s) {
s << BLT << src1 << " " << imm << " ";
emit_label_ref(label, s);
s << endl;
}

static void emit_bgti(char *src1, int imm, int label, ostream &s) {
s << BGT << src1 << " " << imm << " ";
emit_label_ref(label, s);
s << endl;
}

static void emit_branch(int l, ostream &s) {
s << BRANCH;
emit_label_ref(l, s);
s << endl;
}

//
Expand All @@ -178,23 +128,6 @@ ostream & emit_push(char *reg, ostream &str) {
return emit_addiu(SP, SP, -4, str);
}

//
// Fetch the integer value in an Int object.
// Emits code to fetch the integer value of the Integer object pointed
// to by register source into the register dest
//
ostream & emit_fetch_int(char *dest, char *source, ostream &s) {
return emit_load(dest, DEFAULT_OBJFIELDS, source, s);
}

//
// Emits code to store the integer value contained in register source
// into the Integer object pointed to by dest.
//
ostream & emit_store_int(char *source, char *dest, ostream &s) {
return emit_store(source, DEFAULT_OBJFIELDS, dest, s);
}


static void emit_test_collector(ostream &s) {
emit_push(ACC, s) << endl;
Expand All @@ -210,10 +143,4 @@ static void emit_gc_check(char *source, ostream &s) {
s << JAL << "_gc_check" << endl;
}

ostream & emit_new(Symbol sym, ostream &str) {
emit_partial_load_address(ACC, str);
str << sym << PROTOBJ_SUFFIX << endl;
return emit_jal(OBJECT_COPY, str);
}


123 changes: 114 additions & 9 deletions cgen/cgen_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,133 @@
#ifndef COOL_COMPILER_CGEN_HELPERS_H_H
#define COOL_COMPILER_CGEN_HELPERS_H_H

#include "emit.h"
#include <cool-tree.h>

ostream & emit_object_header(class__class* cls, int tag, ostream& s);

void emit_disptable_ref(Symbol sym, ostream &s);
void emit_label_ref(int l, ostream &s);
ostream & emit_label_ref(int l, ostream &s);
ostream & emit_label_ref(const char *ls, int ln, ostream &s);
void emit_protobj_ref(Symbol sym, ostream &s);
void emit_method_ref(Symbol classname, Symbol methodname, ostream &s);
void emit_init_ref(Symbol sym, ostream &s);
ostream & emit_store(char *source_reg, int offset, char *dest_reg, ostream &s);
ostream & emit_load_bool(char *dest, const BoolConst &b, ostream &s);
ostream & emit_load_string(char *dest, StringEntry *str, ostream &s) ;
ostream & emit_load_int(char *dest, IntEntry *i, ostream &s);

ostream & emit_load(char *dest_reg, int offset, char *source_reg, ostream &s);
ostream & emit_fetch_int(char *dest, char *source, ostream &s);
ostream & emit_store_int(char *source, char *dest, ostream &s);

ostream & emit_move(char *dest_reg, char *source_reg, ostream &s);

ostream & emit_new(Symbol sym, ostream &str);
ostream & emit_push(char *reg, ostream &str);
ostream & emit_addiu(char *dest, char *src1, int imm, ostream &s);
ostream & emit_return(ostream &s);
ostream & emit_jalr(char *address, ostream &s);

void emit_partial_load_address(char *dest_reg, ostream &s);

template <typename T> ostream & _comment(ostream &os, T arg) {
return (os << arg);
}

template <typename T, typename... Ts> ostream & _comment(ostream &os, T arg, Ts... args) {
os << arg;
return _comment(os, args...);
}

template <typename... Ts> ostream & comment(ostream &os, int line_no, Ts... args) {
os << "\t# " << line_no << ": ";
_comment(os, args...);
os << endl;
return os;
}

template <typename... Ts> ostream & emit_store(char *source_reg, int offset, char *dest_reg, ostream &s, int line_no, Ts... comments) {
emit_store(source_reg, offset, dest_reg, s);
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_load(char *dest_reg, int offset, char *source_reg, ostream &s, int line_no, Ts... comments) {
emit_load(dest_reg, offset, source_reg, s);
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_fetch_int(char *dest, char *src, ostream &s, int line_no, Ts... comments) {
emit_load(dest, DEFAULT_OBJFIELDS, src, s);
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_new(Symbol sym, ostream &s, int line_no, Ts... comments) {
emit_partial_load_address(ACC, s);
s << sym << PROTOBJ_SUFFIX << endl;
s << JAL << OBJECT_COPY;
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_store_int(char *source, char *dest, ostream &s, int line_no, Ts... comments) {
emit_store(source, DEFAULT_OBJFIELDS, dest, s);
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_load_bool(char *dest, const BoolConst &b, ostream &s, int line_no, Ts... comments) {
emit_partial_load_address(dest, s);
b.code_ref(s);
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_load_string(char *dest, StringEntry *str, ostream &s, int line_no, Ts... comments) {
emit_partial_load_address(dest, s);
str->code_ref(s);
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_load_int(char *dest, IntEntry *i, ostream &s, int line_no, Ts... comments) {
emit_partial_load_address(dest, s);
i->code_ref(s);
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_push(char *reg, ostream &str, int line_no, Ts... comments) {
emit_push(reg, str);
return comment(str, line_no, comments...);
}

template <typename... Ts> ostream & emit_jalr(char *address, ostream &s, int line_no, Ts... comments) {
s << JALR << address;
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_beqz(const char *source, const char *ls, int ln, ostream &s, int line_no, Ts... comments) {
s << BEQZ << source << " ";
emit_label_ref(ls, ln, s);
return comment(s, line_no, comments...);
}
template <typename... Ts> ostream & emit_branch(const char *ls, int ln, ostream &s, int line_no, Ts... comments) {
s << BRANCH;
emit_label_ref(ls, ln, s);
return comment(s, line_no, comments...);
}
template <typename... Ts> ostream & emit_label_def(const char *ls, int ln, ostream &s, int line_no, Ts... comments) {
emit_label_ref(ls, ln, s) << ':';
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_move(char *dest_reg, char *source_reg, ostream &s, int line_no, Ts... comments) {
emit_move(dest_reg, source_reg, s);
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_seq(char *dest, char *src1, char *src2, ostream &s, int line_no, Ts... comments) {
s << SEQ << " " << src1 << " " << src2;
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_return(ostream &s, int line_no, Ts... comments) {
s << RET;
return comment(s, line_no, comments...);
}

template <typename... Ts> ostream & emit_addiu(char *dest, char *src1, int imm, ostream &s, int line_no, Ts... comments) {
emit_addiu(dest, src1, imm, s);
return comment(s, line_no, comments...);
}


#endif //COOL_COMPILER_CGEN_HELPERS_H_H
29 changes: 29 additions & 0 deletions cgen/string_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Created by Evgeny Tanhilevich on 22/04/2016.
//

#ifndef COOL_COMPILER_STRING_UTIL_H
#define COOL_COMPILER_STRING_UTIL_H
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>

// trim from start
static inline std::string &ltrim(std::string &s) {
s.erase(s.begin(), std::find_if_not(s.begin(), s.end(), std::ptr_fun<int, int>(std::isspace)));
return s;
}

// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if_not(s.rbegin(), s.rend(), std::ptr_fun<int, int>(std::isspace)).base(), s.end());
return s;
}

// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}

#endif //COOL_COMPILER_STRING_UTIL_H
Loading

0 comments on commit 4d965e9

Please sign in to comment.