Skip to content

Commit e1969a6

Browse files
committed
use tab instead of space
Signed-off-by: Peter Rong <PeterRong96@gmail.com>
1 parent cff692d commit e1969a6

File tree

1 file changed

+99
-99
lines changed

1 file changed

+99
-99
lines changed

PA4/src/cgen.cc

+99-99
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ void StringEntry::code_def(ostream& s, int stringclasstag)
426426
<< WORD << (DEFAULT_OBJFIELDS + STRING_SLOTS + (len+4)/4) << endl // size
427427
<< WORD;
428428

429-
emit_disptable_ref(Str, s);
429+
emit_disptable_ref(Str, s);
430430

431431
s << endl; // dispatch table
432432
s << WORD; lensym->code_ref(s); s << endl; // string length
@@ -468,7 +468,7 @@ void IntEntry::code_def(ostream &s, int intclasstag)
468468
<< WORD << (DEFAULT_OBJFIELDS + INT_SLOTS) << endl // object size
469469
<< WORD;
470470

471-
emit_disptable_ref(Int, s);
471+
emit_disptable_ref(Int, s);
472472

473473
s << endl; // dispatch table
474474
s << WORD << str << endl; // integer value
@@ -512,7 +512,7 @@ void BoolConst::code_def(ostream& s, int boolclasstag)
512512
<< WORD << (DEFAULT_OBJFIELDS + BOOL_SLOTS) << endl // object size
513513
<< WORD;
514514

515-
emit_disptable_ref(Bool, s);
515+
emit_disptable_ref(Bool, s);
516516

517517
s << endl; // dispatch table
518518
s << WORD << val << endl; // value (0 or 1)
@@ -969,7 +969,7 @@ void CgenNode::codeDispatchTable(ostream& str) const {
969969
for (std::map<Symbol, Method>::const_iterator iter = method_map_.begin();
970970
iter != method_map_.end();
971971
++iter){
972-
Method m = iter->second;
972+
Method m = iter->second;
973973
str << WORD;
974974
emit_method_ref(m->getNative(), m->getName(), str);
975975
str << endl;
@@ -1184,7 +1184,7 @@ int newLabel(){
11841184
// such code should be posted on pornhub instead of github. :)
11851185
//
11861186
void assign_class::code(ostream &s) {
1187-
// Eval expression
1187+
// Eval expression
11881188
expr->code(s);
11891189
// Get the location(in terms of register).
11901190
ObjectLocation* obj_loc = env.lookup(name);
@@ -1195,104 +1195,104 @@ void assign_class::code(ostream &s) {
11951195
// An helper function for dispatch
11961196
// type_name is of use only when it's non-static dispatch
11971197
void dispatch_common(Expression& expr, Symbol * type_name, Symbol& name, Expressions& actual,
1198-
char * filename, int line_num, bool is_static_dispatch, ostream &s){
1199-
if (cgen_debug) s << "# dispatch_common called" << endl;
1198+
char * filename, int line_num, bool is_static_dispatch, ostream &s){
1199+
if (cgen_debug) s << "# dispatch_common called" << endl;
12001200

12011201

12021202
if (cgen_debug) s << "# creating new label" << endl;
1203-
// create new label
1204-
int label_exec = newLabel();
1205-
1206-
// deal with all args and push to stack (a1-aN)
1207-
for (int i = 0; i < actual->len(); i++){
1208-
Expression arg = actual->nth(i);
1209-
// eval the arg and push to stack
1210-
arg->code(s);
1211-
emit_push(ACC,s);
1212-
}
1213-
1214-
// the first parameter is the object itself
1215-
if (cgen_debug) s << "# generating code for object_id: " << expr->get_type() << endl;
1216-
if (expr->get_type() == SELF_TYPE) {
1217-
// move s0 to a0 (as the first parameter)
1218-
emit_move(ACC, SELF, s);
1219-
}
1220-
else{
1221-
// eval the attr name, the attr get loaded to $a0 automatically
1222-
expr->code(s);
1223-
}
1224-
1225-
if (cgen_debug) s << "# abort on error" << endl;
1226-
// Abort
1227-
// if a0 is $0, abort and call dispatch_abort
1228-
emit_bne(ACC,ZERO,label_exec,s);
1229-
// load filename
1230-
StringEntry * fname = stringtable.lookup_string(filename);
1231-
emit_partial_load_address(ACC,s);
1232-
fname->code_ref(s);
1233-
s << endl;
1234-
// T1 is the line number
1235-
emit_load_imm(T1,line_num,s);
1236-
emit_jal("_dispatch_abort",s);
1237-
1238-
if (cgen_debug) s << "# exec dispatched table" << endl;
1239-
// Valid: preceed to exec
1240-
emit_label_def(label_exec,s);
1241-
1242-
// get the dispatch table of object at $a0
1243-
if (is_static_dispatch){
1244-
emit_partial_load_address(T1,s);
1245-
emit_disptable_ref(*type_name,s);
1246-
s << endl;
1247-
}
1248-
else {
1249-
emit_load(T1, 2, ACC, s);
1250-
}
1251-
1252-
// get the function offset
1253-
int func_offset = 0;
1254-
1255-
std::map<Symbol, Method> method_map;
1256-
// get the dispatch table for object
1257-
if (is_static_dispatch){
1258-
method_map = class_map[*type_name]->getMethodMap();
1259-
}
1260-
else {
1261-
Symbol object_name = expr->get_type();
1262-
if (object_name == SELF_TYPE) {
1263-
method_map = cur_class->getMethodMap();
1264-
} else {
1265-
method_map = class_map[object_name]->getMethodMap();
1266-
}
1267-
}
1268-
1269-
// search for the target function in the dispatch table
1270-
for (std::map<Symbol, Method>::const_iterator iter = method_map.begin();
1271-
iter != method_map.end();
1272-
++iter){
1273-
if (iter->first == name){
1274-
break;
1275-
}
1276-
func_offset ++;
1277-
}
1278-
emit_load(T1,func_offset,T1,s);
1279-
1280-
// jalr
1281-
emit_jalr(T1,s);
1203+
// create new label
1204+
int label_exec = newLabel();
1205+
1206+
// deal with all args and push to stack (a1-aN)
1207+
for (int i = 0; i < actual->len(); i++){
1208+
Expression arg = actual->nth(i);
1209+
// eval the arg and push to stack
1210+
arg->code(s);
1211+
emit_push(ACC,s);
1212+
}
1213+
1214+
// the first parameter is the object itself
1215+
if (cgen_debug) s << "# generating code for object_id: " << expr->get_type() << endl;
1216+
if (expr->get_type() == SELF_TYPE) {
1217+
// move s0 to a0 (as the first parameter)
1218+
emit_move(ACC, SELF, s);
1219+
}
1220+
else{
1221+
// eval the attr name, the attr get loaded to $a0 automatically
1222+
expr->code(s);
1223+
}
1224+
1225+
if (cgen_debug) s << "# abort on error" << endl;
1226+
// Abort
1227+
// if a0 is $0, abort and call dispatch_abort
1228+
emit_bne(ACC,ZERO,label_exec,s);
1229+
// load filename
1230+
StringEntry * fname = stringtable.lookup_string(filename);
1231+
emit_partial_load_address(ACC,s);
1232+
fname->code_ref(s);
1233+
s << endl;
1234+
// T1 is the line number
1235+
emit_load_imm(T1,line_num,s);
1236+
emit_jal("_dispatch_abort",s);
1237+
1238+
if (cgen_debug) s << "# exec dispatched table" << endl;
1239+
// Valid: preceed to exec
1240+
emit_label_def(label_exec,s);
1241+
1242+
// get the dispatch table of object at $a0
1243+
if (is_static_dispatch){
1244+
emit_partial_load_address(T1,s);
1245+
emit_disptable_ref(*type_name,s);
1246+
s << endl;
1247+
}
1248+
else {
1249+
emit_load(T1, 2, ACC, s);
1250+
}
1251+
1252+
// get the function offset
1253+
int func_offset = 0;
1254+
1255+
std::map<Symbol, Method> method_map;
1256+
// get the dispatch table for object
1257+
if (is_static_dispatch){
1258+
method_map = class_map[*type_name]->getMethodMap();
1259+
}
1260+
else {
1261+
Symbol object_name = expr->get_type();
1262+
if (object_name == SELF_TYPE) {
1263+
method_map = cur_class->getMethodMap();
1264+
} else {
1265+
method_map = class_map[object_name]->getMethodMap();
1266+
}
1267+
}
1268+
1269+
// search for the target function in the dispatch table
1270+
for (std::map<Symbol, Method>::const_iterator iter = method_map.begin();
1271+
iter != method_map.end();
1272+
++iter){
1273+
if (iter->first == name){
1274+
break;
1275+
}
1276+
func_offset ++;
1277+
}
1278+
emit_load(T1,func_offset,T1,s);
1279+
1280+
// jalr
1281+
emit_jalr(T1,s);
12821282
}
12831283

12841284
void static_dispatch_class::code(ostream &s) {
12851285
if (cgen_debug) s << "# static_dispatch called" << endl;
12861286

1287-
char * filename = cur_class->get_filename()->get_string();
1288-
dispatch_common(expr, &type_name,name,actual,filename,this->get_line_number(),true,s);
1287+
char * filename = cur_class->get_filename()->get_string();
1288+
dispatch_common(expr, &type_name,name,actual,filename,this->get_line_number(),true,s);
12891289
}
12901290

12911291
void dispatch_class::code(ostream &s) {
1292-
if (cgen_debug) s << "# dispatch called" << endl;
1292+
if (cgen_debug) s << "# dispatch called" << endl;
12931293

1294-
char * filename = cur_class->get_filename()->get_string();
1295-
dispatch_common(expr,NULL,name,actual,filename,this->get_line_number(),false,s);
1294+
char * filename = cur_class->get_filename()->get_string();
1295+
dispatch_common(expr,NULL,name,actual,filename,this->get_line_number(),false,s);
12961296

12971297
}
12981298

@@ -1476,13 +1476,13 @@ void divide_class::code(ostream &s) {
14761476
// Negate.
14771477
void neg_class::code(ostream &s) {
14781478
// eval e1: get int object
1479-
e1->code(s);
1479+
e1->code(s);
14801480
emit_copy(s);
14811481
// load value of int object
1482-
emit_load(T1, 3, ACC, s);
1483-
emit_neg(T1, T1, s);
1484-
// store value back
1485-
emit_store_int(T1, ACC, s);
1482+
emit_load(T1, 3, ACC, s);
1483+
emit_neg(T1, T1, s);
1484+
// store value back
1485+
emit_store_int(T1, ACC, s);
14861486
}
14871487

14881488
// Not
@@ -1502,9 +1502,9 @@ void lessThanCommon(Expression e1, Expression e2, ostream& s, const bool eq){
15021502
// eval e1, e2 and store to t1 t2
15031503
arith_common(e1, e2, s);
15041504
if (eq){
1505-
emit_bleq(T1, T2, label_isles, s);
1505+
emit_bleq(T1, T2, label_isles, s);
15061506
} else {
1507-
emit_blt(T1, T2, label_isles, s);
1507+
emit_blt(T1, T2, label_isles, s);
15081508
}
15091509
// Else branch.
15101510
emit_load_bool(ACC, falsebool, s);
@@ -1622,7 +1622,7 @@ void object_class::code(ostream &s) {
16221622
ObjectLocation * loc = env.lookup(name);
16231623

16241624
// move the attr to a0
1625-
emit_load(ACC,loc->getOffset(), loc->getReg(), s);
1625+
emit_load(ACC,loc->getOffset(), loc->getReg(), s);
16261626
}
16271627

16281628

0 commit comments

Comments
 (0)