def constant_folding(three_addr_code: ThreeAddressCode, symbol_table: SymbolTable): to_remove = list() for idx, line in enumerate(three_addr_code.code): if len(line) == 4: node1 = symbol_table.search_node(line[2]) node2 = symbol_table.search_node(line[3]) if node1: three_addr_code.code[idx][2] = node1.value to_remove += get_all_stmt_lhs(three_addr_code, node1.name) if node2: three_addr_code.code[idx][3] = node2.value to_remove += get_all_stmt_lhs(three_addr_code, node2.name) three_addr_code.code = [ three_addr_code.code[i] for i in range(len(three_addr_code.code)) if i not in to_remove ] return three_addr_code
def p_ExprCaseClauseList(p): '''ExprCaseClauseList : empty | ExprCaseClauseList ExprCaseClause ''' TAC1 = ThreeAddressCode() TAC2 = ThreeAddressCode() if len(p) == 3: TAC1 = p[1].data TAC2 = p[2].data p[0] = TreeNode('ExprCaseClauseList', TAC1, 'INT', 0, p[1].children + p[2].children, p[1].TAC) p[0].TAC.add_leader(p[0].TAC.length()) p[0].TAC.append_TAC(p[2].TAC) p[0].data.append_TAC(TAC2) else: p[0] = TreeNode('ExprCaseClauseList', TAC1, 'INT') return
def convert_tac(ThreeAddressCode): """Reads three adress code generated from parser and converts to TAC for codegen; generates the three_addr_code along with leaders; populates generate symbol table as per three_addr_code""" for i in range(ThreeAddressCode.length()): three_addr_instr = ThreeAddressCode.code[i] three_addr_instr = [str(i + 1)] + three_addr_instr three_addr_code.add_line(three_addr_instr) if len(three_addr_instr) != 5: print("Incorrect size for the following instruction: ") print(three_addr_instr) return -1 if three_addr_instr[0] == '': print("Line number not given in the following instruction: ") print(three_addr_instr) return -1 import re if re.search(r'\D', three_addr_instr[0]) != None: print("Invalid line number given in the following instruction: ") print(three_addr_instr) return -1 leader_generating_if_instr = [] leader_generating_if_instr += ['ifgotoeq'] leader_generating_if_instr += ['ifgotoneq'] leader_generating_if_instr += ['ifgotolt'] leader_generating_if_instr += ['ifgotolteq'] leader_generating_if_instr += ['ifgotogt'] leader_generating_if_instr += ['ifgotogteq'] if three_addr_instr[1] in leader_generating_if_instr: three_addr_code.add_leader(three_addr_code.length()) leader_generating_other_instr = ['label'] if three_addr_instr[1] in leader_generating_if_instr: three_addr_code.add_leader(three_addr_code.length() - 1) leader_generating_other_instr = [] leader_generating_other_instr += ['goto'] leader_generating_other_instr += ['break'] leader_generating_other_instr += ['continue'] if three_addr_instr[1] in leader_generating_other_instr: three_addr_code.add_leader(three_addr_code.length()) three_addr_code.leaders = sorted(three_addr_code.leaders, key=int) return three_addr_code
#!/usr/bin/python """Generate Assembly code from 3AC""" import sys from code import Code, ThreeAddressCode from registers import Registers from symbol_table import SymbolTable three_addr_code = ThreeAddressCode() assembly_code = Code() registers = Registers() input_file = '' start_main = 0 start_param = 0 def convert_tac(ThreeAddressCode): """Reads three adress code generated from parser and converts to TAC for codegen; generates the three_addr_code along with leaders; populates generate symbol table as per three_addr_code""" for i in range(ThreeAddressCode.length()): three_addr_instr = ThreeAddressCode.code[i] three_addr_instr = [str(i + 1)] + three_addr_instr three_addr_code.add_line(three_addr_instr) if len(three_addr_instr) != 5: print("Incorrect size for the following instruction: ") print(three_addr_instr) return -1
from code import ThreeAddressCode from symboltable import SymbolTable from symboltable import symboltable_node #import codegen import optimize_tac import sys from random import * import logging import re parsed = [] current_scope = 0 temp_counter = 0 label_counter = 0 ThreeAddrCode = ThreeAddressCode() SymbolTable = SymbolTable() def tempGen(): global temp_counter tempname = 'temp_' + str(temp_counter) temp_counter += 1 return tempname def labelGen(): global label_counter labelname = 'label_' + str(label_counter) label_counter += 1 return labelname