diff --git a/compiler/src/CMakeLists.txt b/compiler/src/CMakeLists.txt new file mode 100644 index 0000000..232aca2 --- /dev/null +++ b/compiler/src/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.16) + +add_subdirectory(parser) +add_subdirectory(ir) +add_subdirectory(opt) +add_subdirectory(riscv) + +set(SOURCE_FILES main.cpp) + +add_executable(carrotcompiler ${SOURCE_FILES}) +set_target_properties(carrotcompiler PROPERTIES OUTPUT_NAME "compiler") + +target_include_directories(compiler PRIVATE parser ir opt riscv) + +target_link_libraries(compiler parser ir opt riscv) diff --git a/compiler/src/ir/CMakeLists.txt b/compiler/src/ir/CMakeLists.txt new file mode 100644 index 0000000..eb085e5 --- /dev/null +++ b/compiler/src/ir/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.21) + +set(SOURCE_FILES "ir.cpp" "genIR.cpp") + +add_library(ir STATIC ${SOURCE_FILES}) + +target_include_directories(ir PRIVATE "${PARSER_INCLUDE}") diff --git a/compiler/src/ir/genIR.cpp b/compiler/src/ir/genIR.cpp new file mode 100644 index 0000000..d3f685e --- /dev/null +++ b/compiler/src/ir/genIR.cpp @@ -0,0 +1,1082 @@ +#include "genIR.h" +#include "ir.h" + +#define CONST_INT(num) new ConstantInt(module->int32_ty_, num) +#define CONST_FLOAT(num) new ConstantFloat(module->float32_ty_, num) +#define VOID_T (module->void_ty_) +#define INT1_T (module->int1_ty_) +#define INT32_T (module->int32_ty_) +#define FLOAT_T (module->float32_ty_) +#define INT32PTR_T (module->get_pointer_type(module->int32_ty_)) +#define FLOATPTR_T (module->get_pointer_type(module->float32_ty_)) + +// store temporary value +Type *curType; // 当前decl类型 +bool isConst; // 当前decl是否是const +bool useConst = false; // 计算是否使用常量 +std::vector params; // 函数形参类型表 +std::vector paramNames; // 函数形参名表 +Value *retAlloca = nullptr; // 返回值 +BasicBlock *retBB = nullptr; // 返回语句块 +bool isNewFunc = false; // 判断是否为新函数,用来处理函数作用域问题 +bool requireLVal = false; // 告诉LVal节点不需要发射load指令 +Function *currentFunction = nullptr; // 当前函数 +Value *recentVal = nullptr; // 最近的表达式的value +BasicBlock *functionBB = nullptr; // To Fix the bug: current function's basic block +BasicBlock *whileCondBB = nullptr; // while语句cond分支 +BasicBlock *trueBB = nullptr; // 通用true分支,即while和if为真时所跳转的基本块 +BasicBlock *falseBB = nullptr; // 通用false分支,即while和if为假时所跳转的基本块 +BasicBlock *whileFalseBB; // while语句false分支,用于break跳转 +int id = 1; // recent标号 +bool has_br = false; // 一个BB中是否已经出现了br +bool is_single_exp = false; // 作为单独的exp语句出现,形如 "exp;" + +// 判断得到的赋值与声明类型是否一致,并做转换 +void GenIR::checkInitType() const { + if (curType == INT32_T) { + if (dynamic_cast(recentVal)) { + auto temp = dynamic_cast(recentVal); + recentVal = CONST_INT((int)temp->value_); + } else if (recentVal->type_->tid_ == Type::FloatTyID) { + recentVal = builder->create_fptosi(recentVal, INT32_T); + } + } else if (curType == FLOAT_T) { + if (dynamic_cast(recentVal)) { + auto temp = dynamic_cast(recentVal); + recentVal = CONST_FLOAT(temp->value_); + } else if (recentVal->type_->tid_ == Type::IntegerTyID) { + recentVal = builder->create_sitofp(recentVal, FLOAT_T); + } + } +} + +void GenIR::visit(CompUnitAST &ast) { + for (const auto &def : ast.declDefList) { + def->accept(*this); + } +} + +void GenIR::visit(DeclDefAST &ast) { + if (ast.Decl != nullptr) { + ast.Decl->accept(*this); + } else { + ast.funcDef->accept(*this); + } +} + +void GenIR::visit(DeclAST &ast) { + isConst = ast.isConst; + curType = ast.bType == TYPE_INT ? INT32_T : FLOAT_T; + for (auto &def : ast.defList) { + def->accept(*this); + } +} + +void GenIR::visit(DefAST &ast) { + string varName = *ast.id; + // 全局变量或常量 + if (scope.in_global()) { + if (ast.arrays.empty()) { // 不是数组,即全局量 + if (ast.initVal == nullptr) { // 无初始化 + if (isConst) + cout << "no initVal when define const!" + << endl; // 无初始化全局常量报错 + // 无初始化全局量一定是变量 + GlobalVariable *var; + if (curType == INT32_T) + var = new GlobalVariable(varName, module.get(), curType, false, + CONST_INT(0)); + else + var = new GlobalVariable(varName, module.get(), curType, false, + CONST_FLOAT(0)); + scope.push(varName, var); + } else { // 有初始化 + useConst = true; + ast.initVal->accept(*this); + useConst = false; + checkInitType(); + if (isConst) { + scope.push(varName, recentVal); // 单个常量定义不用new GlobalVariable + } else { // 全局变量 + auto initializer = static_cast(recentVal); + GlobalVariable *var; + var = new GlobalVariable(varName, module.get(), curType, false, + initializer); + scope.push(varName, var); + } + } + } else { // 是数组,即全局数组量 + vector dimensions; // 数组各维度; [2][3][4]对应 + useConst = true; + // 获取数组各维度 + for (auto &exp : ast.arrays) { + exp->accept(*this); + int dimension = dynamic_cast(recentVal)->value_; + dimensions.push_back(dimension); + } + useConst = false; + vector arrayTys( + dimensions.size()); // 数组类型, {[2 x [3 x [4 x i32]]], [3 x [4 x + // i32]], [4 x i32]} + for (int i = dimensions.size() - 1; i >= 0; i--) { + if (i == dimensions.size() - 1) + arrayTys[i] = module->get_array_type(curType, dimensions[i]); + else + arrayTys[i] = module->get_array_type(arrayTys[i + 1], dimensions[i]); + } + // 无初始化或者初始化仅为大括号 + if (ast.initVal == nullptr || ast.initVal->initValList.empty()) { + auto init = new ConstantZero(arrayTys[0]); + auto var = new GlobalVariable(varName, module.get(), arrayTys[0], + isConst, init); + scope.push(varName, var); + } else { + useConst = true; // 全局数组量的初始值必为常量 + auto init = + globalInit(dimensions, arrayTys, 0, ast.initVal->initValList); + useConst = false; + auto var = new GlobalVariable(varName, module.get(), arrayTys[0], + isConst, init); + scope.push(varName, var); + } + } + return; + } + + // 修复在循环中多次分配同名变量的Bug + // 局部变量或常量 + if (ast.arrays.empty()) { // 不是数组,即普通局部量 + if (ast.initVal == nullptr) { // 无初始化 + if (isConst) + cout << "no initVal when define const!" << endl; // 无初始化局部常量报错 + else { // 无初始化变量 + auto _backupBB = builder->get_insert_block(); + builder->set_insert_point(functionBB); // 将局部变量移动至当前函数头部的 Basic block + AllocaInst *varAlloca; + varAlloca = builder->create_alloca(curType); + builder->set_insert_point(_backupBB); // 还原插入点 + scope.push(varName, varAlloca); + } + } else { // 有初始化 + ast.initVal->accept(*this); + checkInitType(); + if (isConst) { + scope.push(varName, recentVal); // 单个常量定义不用create_alloca + } else { + auto _backupBB = builder->get_insert_block(); + builder->set_insert_point(functionBB); + AllocaInst *varAlloca; + varAlloca = builder->create_alloca(curType); + builder->set_insert_point(_backupBB); + scope.push(varName, varAlloca); + builder->create_store(recentVal, varAlloca); + } + } + } else { // 局部数组量 + vector dimensions(ast.arrays.size()), + dimensionsCnt((ast.arrays.size())); // 数组各维度, [2][3][4]对应; + // 次维度数组元素个数, [24][12][4] + int totalByte = 1; // 存储总共的字节数 + useConst = true; + // 获取数组各维度 + for (int i = dimensions.size() - 1; i >= 0; i--) { + ast.arrays[i]->accept(*this); + int dimension = dynamic_cast(recentVal)->value_; + totalByte *= dimension; + dimensions[i] = dimension; + dimensionsCnt[i] = totalByte; + } + totalByte *= 4; // 计算字节数 + useConst = false; + ArrayType *arrayTy = nullptr; // 数组类型 + for (int i = dimensions.size() - 1; i >= 0; i--) { + if (i == dimensions.size() - 1) + arrayTy = module->get_array_type(curType, dimensions[i]); + else + arrayTy = module->get_array_type(arrayTy, dimensions[i]); + } + auto arrayAlloc = builder->create_alloca(arrayTy); + scope.push(varName, arrayAlloc); + if (ast.initVal == nullptr) { // 无初始化 + if (isConst) + cout << "no initVal when define const!" << endl; // 无初始化局部常量报错 + return; // 无初始化变量数组无需再做处理 + } + Value *i32P = builder->create_bitcast(arrayAlloc, INT32PTR_T); + auto memclr = scope.find("memclr"); + builder->create_call( + memclr, {i32P, CONST_INT(totalByte)}); // 全部清零,但float可以清零吗 + // 数组初始化时,成员exp一定是空,若initValList也是空,即是大括号,已经置零了直接返回 + if (ast.initVal->initValList.empty()) + return; + vector idxs(dimensions.size() + 1); + for (int i = 0; i < dimensions.size() + 1; i++) { + idxs[i] = CONST_INT(0); + } + Value *ptr = builder->create_gep(arrayAlloc, idxs); // 获取数组开头地址 + localInit(ptr, ast.initVal->initValList, dimensionsCnt, 1); + } +} + +// 嵌套大括号数组的维度,即倒数连续0的第一个。 +// 如[0,1,0,0],返回2;[0,0,0,1],返回4; 若全是0,[0,0,0,0],返回1 +int GenIR::getNextDim(vector &elementsCnts, int up) { + for (int i = elementsCnts.size() - 1; i > up; i--) { + if (elementsCnts[i] != 0) + return i + 1; + } + return up + 1; +} + +// 增加元素后,合并所有能合并的数组元素,即对齐 +void GenIR::mergeElements(vector &dimensions, + vector &arrayTys, int up, int dimAdd, + vector &elements, + vector &elementsCnts) { + for (int i = dimAdd; i > up; i--) { + if (elementsCnts[i] % dimensions[i] == 0) { + vector temp; + temp.assign(elements.end() - dimensions[i], elements.end()); + elements.erase(elements.end() - dimensions[i], elements.end()); + elements.push_back(new ConstantArray(arrayTys[i], temp)); + elementsCnts[i] = 0; + elementsCnts[i - 1]++; + } else + break; + } +} + +// 最后合并所有元素,不足合并则填0元素,使得elements只剩下一个arrayTys[up]类型的最终数组 +void GenIR::finalMerge(vector &dimensions, vector &arrayTys, + int up, vector &elements, + vector &elementsCnts) const { + for (int i = dimensions.size() - 1; i >= up; i--) { + while (elementsCnts[i] % dimensions[i] != 0) { // 补充当前数组类型所需0元素 + if (i == dimensions.size() - 1) { + if (curType == INT32_T) { + elements.push_back(CONST_INT(0)); + } else { + elements.push_back(CONST_FLOAT(0)); + } + } else { + elements.push_back(new ConstantZero(arrayTys[i + 1])); + } + elementsCnts[i]++; + } + if (elementsCnts[i] != 0) { + vector temp; + temp.assign(elements.end() - dimensions[i], elements.end()); + elements.erase(elements.end() - dimensions[i], elements.end()); + elements.push_back(new ConstantArray(arrayTys[i], temp)); + elementsCnts[i] = 0; + if (i != up) + elementsCnts[i - 1]++; + } + } +} + +// 生成变量数组的初始化 +ConstantArray *GenIR::globalInit(vector &dimensions, + vector &arrayTys, int up, + vector> &list) { + vector elementsCnts(dimensions.size()); // 对应各个维度的子数组的元素个数 + vector elements; // 各个元素 + int dimAdd; + for (auto &val : list) { + if (val->exp != nullptr) { + dimAdd = dimensions.size() - 1; + val->exp->accept(*this); + checkInitType(); + elements.push_back((ConstantInt *)recentVal); + } else { + auto nextUp = getNextDim(elementsCnts, up); // 该嵌套数组的维度 + dimAdd = nextUp - 1; // 比他高一维度的数组需要添加一个元素 + if (nextUp == dimensions.size()) + cout << "initial invalid" << endl; // 没有连续0,没对齐,不合法 + if (val->initValList.empty()) { + elements.push_back(new ConstantZero(arrayTys[nextUp])); + } else { + auto temp = globalInit(dimensions, arrayTys, nextUp, val->initValList); + elements.push_back(temp); + } + } + elementsCnts[dimAdd]++; + mergeElements(dimensions, arrayTys, up, dimAdd, elements, elementsCnts); + } + finalMerge(dimensions, arrayTys, up, elements, elementsCnts); + return static_cast(elements[0]); +} + +// 根据初始化的量决定嵌套数组的维度 +int GenIR::getNextDim(vector &dimensionsCnt, int up, int cnt) { + for (int i = up; i < dimensionsCnt.size(); i++) { + if (cnt % dimensionsCnt[i] == 0) + return i; + } + return 0; +} + +// 根据首指针递归初始化数组,up表示子数组的最高对齐位置,比如[4][2][4],子数组最高对齐[2][4],up为1 +void GenIR::localInit(Value *ptr, vector> &list, + vector &dimensionsCnt, int up) { + int cnt = 0; + Value *tempPtr = ptr; + for (auto &initVal : list) { + if (initVal->exp) { + if (cnt == 0) + cnt++; // 第一次赋值时可以少一次create_gep + else + tempPtr = builder->create_gep(ptr, {CONST_INT(cnt++)}); + initVal->exp->accept(*this); + checkInitType(); + builder->create_store(recentVal, tempPtr); + } else { + auto nextUp = getNextDim(dimensionsCnt, up, cnt); + if (nextUp == 0) + cout << "initial invalid!" << endl; + if (!initVal->initValList.empty()) { + if (cnt != 0) + tempPtr = builder->create_gep( + ptr, {CONST_INT(cnt)}); // 没赋值过,那tempPtr实际就是ptr + localInit(tempPtr, initVal->initValList, dimensionsCnt, nextUp); + } + cnt += dimensionsCnt[nextUp]; // 数组初始化量一定增加这么多 + } + } +} + +void GenIR::visit(InitValAST &ast) { + // 不是数组则求exp的值,若是数组不会进入此函数 + if (ast.exp != nullptr) { + ast.exp->accept(*this); + } +} + +void GenIR::visit(FuncDefAST &ast) { + isNewFunc = true; + params.clear(); + paramNames.clear(); + Type *retType; + if (ast.funcType == TYPE_INT) + retType = INT32_T; + else if (ast.funcType == TYPE_FLOAT) + retType = FLOAT_T; + else + retType = VOID_T; + + // 获取参数列表 + for (auto &funcFParam : ast.funcFParamList) { + funcFParam->accept(*this); + } + // 获取函数类型 + auto funTy = new FunctionType(retType, params); + // 添加函数 + auto func = new Function(funTy, *ast.id, module.get()); + currentFunction = func; + scope.push(*ast.id, func); // 在进入新的作用域之前添加到符号表中 + // 进入函数(进入新的作用域) + scope.enter(); + + std::vector args; // 获取函数的形参,通过Function中的iterator + for (auto arg = func->arguments_.begin(); arg != func->arguments_.end(); + arg++) + args.push_back(*arg); + + auto bb = new BasicBlock(module.get(), "label_entry", func); + builder->BB_ = bb; + functionBB = bb; + for (int i = 0; i < (int)(paramNames.size()); i++) { + auto alloc = builder->create_alloca(params[i]); // 分配形参空间 + builder->create_store(args[i], alloc); // store 形参 + scope.push(paramNames[i], alloc); // 加入作用域 + } + // 创建统一return分支 + retBB = new BasicBlock(module.get(), "label_ret", func); + if (retType == VOID_T) { + // void类型无需返回值 + builder->BB_ = retBB; + builder->create_void_ret(); + } else { + retAlloca = builder->create_alloca(retType); // 在内存中分配返回值的位置 + builder->BB_ = retBB; + auto retLoad = builder->create_load(retAlloca); + builder->create_ret(retLoad); + } + // 重新回到函数开始 + builder->BB_ = bb; + has_br = false; + ast.block->accept(*this); + + // 处理没有return的空块 + if (!builder->BB_->get_terminator()) + builder->create_br(retBB); +} + +void GenIR::visit(FuncFParamAST &ast) { + // 获取参数类型 + Type *paramType; + if (ast.bType == TYPE_INT) + paramType = INT32_T; + else + paramType = FLOAT_T; + // 是否为数组 + if (ast.isArray) { + useConst = true; // 数组维度是整型常量 + for (int i = ast.arrays.size() - 1; i >= 0; i--) { + ast.arrays[i]->accept(*this); + paramType = + module->get_array_type(paramType, ((ConstantInt *)recentVal)->value_); + } + useConst = false; + // 如int a[][2],则参数为[2 x i32]* ; int a[],参数为i32 * + paramType = module->get_pointer_type(paramType); + } + params.push_back(paramType); + paramNames.push_back(*ast.id); +} + +void GenIR::visit(BlockAST &ast) { + // 如果是一个新的函数,则不用再进入一个新的作用域 + if (isNewFunc) + isNewFunc = false; + // 其它情况,需要进入一个新的作用域 + else { + scope.enter(); + } + // 遍历每一个语句块 + for (auto &item : ast.blockItemList) { + if (has_br) + break; // 此BB已经出现了br,后续指令无效 + item->accept(*this); + } + + scope.exit(); +} + +void GenIR::visit(BlockItemAST &ast) { + if (ast.decl != nullptr) { + ast.decl->accept(*this); + } else { + ast.stmt->accept(*this); + } +} + +void GenIR::visit(StmtAST &ast) { + switch (ast.sType) { + case SEMI: + break; + case ASS: { + // Init lVal state + requireLVal = true; + // Visit lVal + ast.lVal->accept(*this); + // Get lVal + auto lVal = recentVal; + auto lValType = static_cast(lVal->type_)->contained_; + // Visit expression + ast.exp->accept(*this); + auto rVal = recentVal; + // if lVal.type != rVal.type + // Forge a cast + if (lValType != recentVal->type_) { + if (lValType == FLOAT_T) { + rVal = builder->create_sitofp(recentVal, FLOAT_T); + } else { + rVal = builder->create_fptosi(recentVal, INT32_T); + } + } + // Create a store primitive + builder->create_store(rVal, lVal); + break; + } + case EXP: + is_single_exp = true; + ast.exp->accept(*this); + is_single_exp = false; + break; + case CONT: + builder->create_br(whileCondBB); + has_br = true; + break; + case BRE: + builder->create_br(whileFalseBB); + has_br = true; + break; + case RET: + ast.returnStmt->accept(*this); + break; + case BLK: + ast.block->accept(*this); + break; + case SEL: + ast.selectStmt->accept(*this); + break; + case ITER: + ast.iterationStmt->accept(*this); + break; + } +} + +void GenIR::visit(ReturnStmtAST &ast) { + if (ast.exp == nullptr) { + recentVal = builder->create_br(retBB); + } else { + // 先把返回值store在retAlloca中,再跳转到统一的返回入口 + ast.exp->accept(*this); + // 类型转换 + if (recentVal->type_ == FLOAT_T && + currentFunction->get_return_type() == INT32_T) { + auto temp = builder->create_fptosi(recentVal, INT32_T); + builder->create_store(temp, retAlloca); + } else if (recentVal->type_ == INT32_T && + currentFunction->get_return_type() == FLOAT_T) { + auto temp = builder->create_sitofp(recentVal, FLOAT_T); + builder->create_store(temp, retAlloca); + } else + builder->create_store(recentVal, retAlloca); + recentVal = builder->create_br(retBB); + } + has_br = true; + // builder->BB_ = new BasicBlock(module.get(), to_string(id++), + // currentFunction); //return语句后必是新块 +} + +void GenIR::visit(SelectStmtAST &ast) { + // 先保存trueBB和falseBB,防止嵌套导致返回上一层后丢失块的地址 + auto tempTrue = trueBB; + auto tempFalse = falseBB; + + trueBB = new BasicBlock(module.get(), to_string(id++), currentFunction); + falseBB = new BasicBlock(module.get(), to_string(id++), currentFunction); + BasicBlock *nextIf; // if语句后的基本块 + if (ast.elseStmt == nullptr) + nextIf = falseBB; + else + nextIf = new BasicBlock(module.get(), to_string(id++), currentFunction); + ast.cond->accept(*this); + // 检查是否是i1,不是则进行比较 + if (recentVal->type_ == INT32_T) { + recentVal = builder->create_icmp_ne(recentVal, CONST_INT(0)); + } else if (recentVal->type_ == FLOAT_T) { + recentVal = builder->create_fcmp_ne(recentVal, CONST_FLOAT(0)); + } + builder->create_cond_br(recentVal, trueBB, falseBB); + + builder->BB_ = trueBB; // 开始构建trueBB + has_br = false; + ast.ifStmt->accept(*this); + if (!builder->BB_->get_terminator()) { + builder->create_br(nextIf); + } + + if (ast.elseStmt != nullptr) { // 开始构建falseBB + builder->BB_ = falseBB; + has_br = false; + ast.elseStmt->accept(*this); + if (!builder->BB_->get_terminator()) { + builder->create_br(nextIf); + } + } + + builder->BB_ = nextIf; + has_br = false; + // 还原trueBB和falseBB + trueBB = tempTrue; + falseBB = tempFalse; +} + +void GenIR::visit(IterationStmtAST &ast) { + // 先保存trueBB和falseBB,防止嵌套导致返回上一层后丢失块的地址 + auto tempTrue = trueBB; + auto tempFalse = falseBB; // 即while的next block + auto tempCond = whileCondBB; + auto tempWhileFalseBB = + whileFalseBB; // break只跳while的false,而不跳全局false + + whileCondBB = new BasicBlock(module.get(), to_string(id++), currentFunction); + trueBB = new BasicBlock(module.get(), to_string(id++), currentFunction); + falseBB = new BasicBlock(module.get(), to_string(id++), currentFunction); + whileFalseBB = falseBB; + + builder->create_br(whileCondBB); + builder->BB_ = whileCondBB; // 条件也是一个基本块 + has_br = false; + ast.cond->accept(*this); + if (recentVal->type_ == INT32_T) { + recentVal = builder->create_icmp_ne(recentVal, CONST_INT(0)); + } else if (recentVal->type_ == FLOAT_T) { + recentVal = builder->create_fcmp_ne(recentVal, CONST_FLOAT(0.0)); + } + builder->create_cond_br(recentVal, trueBB, falseBB); + + builder->BB_ = trueBB; + has_br = false; + ast.stmt->accept(*this); + // while语句体一定是跳回cond + if (!builder->BB_->get_terminator()) { + builder->create_br(whileCondBB); + } + + builder->BB_ = falseBB; + has_br = false; + + // 还原trueBB,falseBB,tempCond + trueBB = tempTrue; + falseBB = tempFalse; + whileCondBB = tempCond; + whileFalseBB = tempWhileFalseBB; +} + +// 根据待计算的两个Constant的类型,求出对应的值赋值到intVal,floatVal中,返回计算结果是否为int +bool GenIR::checkCalType(Value *val[], int intVal[], float floatVal[]) { + bool resultIsInt = false; + if (dynamic_cast(val[0]) && + dynamic_cast(val[1])) { + resultIsInt = true; + intVal[0] = dynamic_cast(val[0])->value_; + intVal[1] = dynamic_cast(val[1])->value_; + } else { // 操作结果一定是float + if (dynamic_cast(val[0])) + floatVal[0] = dynamic_cast(val[0])->value_; + else + floatVal[0] = dynamic_cast(val[0])->value_; + if (dynamic_cast(val[1])) + floatVal[1] = dynamic_cast(val[1])->value_; + else + floatVal[1] = dynamic_cast(val[1])->value_; + } + return resultIsInt; +} + +// 根据待计算的两个寄存器数的类型,若需要转换类型输出转换指令 +void GenIR::checkCalType(Value *val[]) { + if (val[0]->type_ == INT1_T) { + val[0] = builder->create_zext(val[0], INT32_T); + } + if (val[1]->type_ == INT1_T) { + val[1] = builder->create_zext(val[1], INT32_T); + } + if (val[0]->type_ == INT32_T && val[1]->type_ == FLOAT_T) { + val[0] = builder->create_sitofp(val[0], FLOAT_T); + } + if (val[1]->type_ == INT32_T && val[0]->type_ == FLOAT_T) { + val[1] = builder->create_sitofp(val[1], FLOAT_T); + } +} + +void GenIR::visit(AddExpAST &ast) { + if (ast.addExp == nullptr) { + ast.mulExp->accept(*this); + return; + } + + Value *val[2]; // lVal, rVal + ast.addExp->accept(*this); + val[0] = recentVal; + ast.mulExp->accept(*this); + val[1] = recentVal; + + // 若都是常量 + if (useConst) { + int intVal[3]; // lInt, rInt, relInt; + float floatVal[3]; // lFloat, rFloat, relFloat; + bool resultIsInt = checkCalType(val, intVal, floatVal); + switch (ast.op) { + case AOP_ADD: + intVal[2] = intVal[0] + intVal[1]; + floatVal[2] = floatVal[0] + floatVal[1]; + break; + case AOP_MINUS: + intVal[2] = intVal[0] - intVal[1]; + floatVal[2] = floatVal[0] - floatVal[1]; + break; + } + if (resultIsInt) + recentVal = CONST_INT(intVal[2]); + else + recentVal = CONST_FLOAT(floatVal[2]); + return; + } + + // 若不是常量,进行计算,输出指令 + checkCalType(val); + if (val[0]->type_ == INT32_T) { + switch (ast.op) { + case AOP_ADD: + recentVal = builder->create_iadd(val[0], val[1]); + break; + case AOP_MINUS: + recentVal = builder->create_isub(val[0], val[1]); + break; + } + } else { + switch (ast.op) { + case AOP_ADD: + recentVal = builder->create_fadd(val[0], val[1]); + break; + case AOP_MINUS: + recentVal = builder->create_fsub(val[0], val[1]); + break; + } + } +} + +void GenIR::visit(MulExpAST &ast) { + if (ast.mulExp == nullptr) { + ast.unaryExp->accept(*this); + return; + } + + Value *val[2]; // lVal, rVal + ast.mulExp->accept(*this); + val[0] = recentVal; + ast.unaryExp->accept(*this); + val[1] = recentVal; + + // 若都是常量 + if (useConst) { + int intVal[3]; // lInt, rInt, relInt; + float floatVal[3]; // lFloat, rFloat, relFloat; + bool resultIsInt = checkCalType(val, intVal, floatVal); + switch (ast.op) { + case MOP_MUL: + intVal[2] = intVal[0] * intVal[1]; + floatVal[2] = floatVal[0] * floatVal[1]; + break; + case MOP_DIV: + intVal[2] = intVal[0] / intVal[1]; + floatVal[2] = floatVal[0] / floatVal[1]; + break; + case MOP_MOD: + intVal[2] = intVal[0] % intVal[1]; + break; + } + if (resultIsInt) + recentVal = CONST_INT(intVal[2]); + else + recentVal = CONST_FLOAT(floatVal[2]); + return; + } + + // 若不是常量,进行计算,输出指令 + checkCalType(val); + if (val[0]->type_ == INT32_T) { + switch (ast.op) { + case MOP_MUL: + recentVal = builder->create_imul(val[0], val[1]); + break; + case MOP_DIV: + recentVal = builder->create_isdiv(val[0], val[1]); + break; + case MOP_MOD: + recentVal = builder->create_isrem(val[0], val[1]); + break; + } + } else { + switch (ast.op) { + case MOP_MUL: + recentVal = builder->create_fmul(val[0], val[1]); + break; + case MOP_DIV: + recentVal = builder->create_fdiv(val[0], val[1]); + break; + case MOP_MOD: // never occur + break; + } + } +} + +void GenIR::visit(UnaryExpAST &ast) { + // 为常量算式 + if (useConst) { + if (ast.primaryExp) { + ast.primaryExp->accept(*this); + } else if (ast.unaryExp) { + ast.unaryExp->accept(*this); + if (ast.op == UOP_MINUS) { + // 是整型常量 + if (dynamic_cast(recentVal)) { + auto temp = (ConstantInt *)recentVal; + temp->value_ = -temp->value_; + recentVal = temp; + } else { + auto temp = (ConstantFloat *)recentVal; + temp->value_ = -temp->value_; + recentVal = temp; + } + } + } else { + cout << "Function call in ConstExp!" << endl; + } + return; + } + + // 不是常量算式 + if (ast.primaryExp != nullptr) { + ast.primaryExp->accept(*this); + } else if (ast.call != nullptr) { + ast.call->accept(*this); + } else { + ast.unaryExp->accept(*this); + if (recentVal->type_ == VOID_T) + return; + else if (recentVal->type_ == INT1_T) // INT1-->INT32 + recentVal = builder->create_zext(recentVal, INT32_T); + + if (recentVal->type_ == INT32_T) { + switch (ast.op) { + case UOP_MINUS: + recentVal = builder->create_isub(CONST_INT(0), recentVal); + break; + case UOP_NOT: + recentVal = builder->create_icmp_eq(recentVal, CONST_INT(0)); + break; + case UOP_ADD: + break; + } + } else { + switch (ast.op) { + case UOP_MINUS: + recentVal = builder->create_fsub(CONST_FLOAT(0), recentVal); + break; + case UOP_NOT: + recentVal = builder->create_fcmp_eq(recentVal, CONST_FLOAT(0)); + break; + case UOP_ADD: + break; + } + } + } +} + +void GenIR::visit(PrimaryExpAST &ast) { + if (ast.exp) { + ast.exp->accept(*this); + } else if (ast.lval) { + ast.lval->accept(*this); + } else if (ast.number) { + ast.number->accept(*this); + } +} + +void GenIR::visit(LValAST &ast) { + bool isTrueLVal = requireLVal; // 是否真是作为左值 + requireLVal = false; + auto var = scope.find(*ast.id); + // 全局作用域内,一定使用常量,全局作用域下访问到LValAST,那么use_const一定被置为了true + if (scope.in_global()) { + // 不是数组,直接返回该常量 + if (ast.arrays.empty()) { + recentVal = var; + return; + } + // 若是数组,则var一定是全局常量数组 + vector index; + for (auto &exp : ast.arrays) { + exp->accept(*this); + index.push_back(dynamic_cast(recentVal)->value_); + } + recentVal = + ((GlobalVariable *)var)->init_val_; // 使用var的初始化数组查找常量元素 + for (int i : index) { + // 某数组元素为ConstantZero,则该数一定是0 + if (dynamic_cast(recentVal)) { + Type *arrayTy = recentVal->type_; + // 找数组元素标签 + while (dynamic_cast(arrayTy)) { + arrayTy = dynamic_cast(arrayTy)->contained_; + } + if (arrayTy == INT32_T) + recentVal = CONST_INT(0); + else + recentVal = CONST_FLOAT(0); + return; + } + if (dynamic_cast(recentVal)) { + recentVal = ((ConstantArray *)recentVal)->const_array[i]; + } + } + return; + } + + // 局部作用域 + if (var->type_->tid_ == Type::IntegerTyID || + var->type_->tid_ == Type::FloatTyID) { // 说明为局部常量 + recentVal = var; + return; + } + // 不是常量那么var一定是指针类型 + Type *varType = + static_cast(var->type_)->contained_; // 所指的类型 + if (!ast.arrays.empty()) { // 说明是数组 + vector idxs; + for (auto &exp : ast.arrays) { + exp->accept(*this); + idxs.push_back(recentVal); + } + // 当函数传入参数i32 *,会生成类型为i32 **的局部变量,即此情况 + if (varType->tid_ == Type::PointerTyID) { + var = builder->create_load(var); + } else if (varType->tid_ == Type::ArrayTyID) { + idxs.insert(idxs.begin(), CONST_INT(0)); + } + var = builder->create_gep(var, idxs); // 获取的一定是指针类型 + varType = ((PointerType *)var->type_)->contained_; + } + + // 指向的还是数组,那么一定是传数组参,数组若为x[2], 参数为int a[],需要传i32 * + if (varType->tid_ == Type::ArrayTyID) { + recentVal = builder->create_gep(var, {CONST_INT(0), CONST_INT(0)}); + } else if (!isTrueLVal) { // 如果不是取左值,那么load + recentVal = builder->create_load(var); + } else { // 否则返回地址值 + recentVal = var; + } +} + +void GenIR::visit(NumberAST &ast) { + if (ast.isInt) + recentVal = CONST_INT(ast.intval); + else + recentVal = CONST_FLOAT(ast.floatval); +} + +void GenIR::visit(CallAST &ast) { + auto fun = (Function *)scope.find(*ast.id); + // 引用函数返回值 + if (fun->basic_blocks_.size() && !is_single_exp) + fun->use_ret_cnt++; + is_single_exp = false; + vector args; + for (int i = 0; i < ast.funcCParamList.size(); i++) { + ast.funcCParamList[i]->accept(*this); + // 检查函数形参与实参类型是否匹配 + if (recentVal->type_ == INT32_T && fun->arguments_[i]->type_ == FLOAT_T) { + recentVal = builder->create_sitofp(recentVal, FLOAT_T); + } else if (recentVal->type_ == FLOAT_T && + fun->arguments_[i]->type_ == INT32_T) { + recentVal = builder->create_fptosi(recentVal, INT32_T); + } + args.push_back(recentVal); + } + recentVal = builder->create_call(fun, args); +} + +void GenIR::visit(RelExpAST &ast) { + if (ast.relExp == nullptr) { + ast.addExp->accept(*this); + return; + } + Value *val[2]; + ast.relExp->accept(*this); + val[0] = recentVal; + ast.addExp->accept(*this); + val[1] = recentVal; + checkCalType(val); + if (val[0]->type_ == INT32_T) { + switch (ast.op) { + case ROP_LTE: + recentVal = builder->create_icmp_le(val[0], val[1]); + break; + case ROP_LT: + recentVal = builder->create_icmp_lt(val[0], val[1]); + break; + case ROP_GT: + recentVal = builder->create_icmp_gt(val[0], val[1]); + break; + case ROP_GTE: + recentVal = builder->create_icmp_ge(val[0], val[1]); + break; + } + } else { + switch (ast.op) { + case ROP_LTE: + recentVal = builder->create_fcmp_le(val[0], val[1]); + break; + case ROP_LT: + recentVal = builder->create_fcmp_lt(val[0], val[1]); + break; + case ROP_GT: + recentVal = builder->create_fcmp_gt(val[0], val[1]); + break; + case ROP_GTE: + recentVal = builder->create_fcmp_ge(val[0], val[1]); + break; + } + } +} + +void GenIR::visit(EqExpAST &ast) { + if (ast.eqExp == nullptr) { + ast.relExp->accept(*this); + return; + } + Value *val[2]; + ast.eqExp->accept(*this); + val[0] = recentVal; + ast.relExp->accept(*this); + val[1] = recentVal; + checkCalType(val); + if (val[0]->type_ == INT32_T) { + switch (ast.op) { + case EOP_EQ: + recentVal = builder->create_icmp_eq(val[0], val[1]); + break; + case EOP_NEQ: + recentVal = builder->create_icmp_ne(val[0], val[1]); + break; + } + } else { + switch (ast.op) { + case EOP_EQ: + recentVal = builder->create_fcmp_eq(val[0], val[1]); + break; + case EOP_NEQ: + recentVal = builder->create_fcmp_ne(val[0], val[1]); + break; + } + } +} + +void GenIR::visit(LAndExpAST &ast) { + if (ast.lAndExp == nullptr) { + ast.eqExp->accept(*this); + return; + } + auto tempTrue = trueBB; // 防止嵌套and导致原trueBB丢失。用于生成短路模块 + trueBB = new BasicBlock(module.get(), to_string(id++), currentFunction); + ast.lAndExp->accept(*this); + + if (recentVal->type_ == INT32_T) { + recentVal = builder->create_icmp_ne(recentVal, CONST_INT(0)); + } else if (recentVal->type_ == FLOAT_T) { + recentVal = builder->create_fcmp_ne(recentVal, CONST_FLOAT(0)); + } + builder->create_cond_br(recentVal, trueBB, falseBB); + builder->BB_ = trueBB; + has_br = false; + trueBB = tempTrue; // 还原原来的true模块 + + ast.eqExp->accept(*this); +} + +void GenIR::visit(LOrExpAST &ast) { + if (ast.lOrExp == nullptr) { + ast.lAndExp->accept(*this); + return; + } + auto tempFalse = falseBB; // 防止嵌套and导致原trueBB丢失。用于生成短路模块 + falseBB = new BasicBlock(module.get(), to_string(id++), currentFunction); + ast.lOrExp->accept(*this); + if (recentVal->type_ == INT32_T) { + recentVal = builder->create_icmp_ne(recentVal, CONST_INT(0)); + } else if (recentVal->type_ == FLOAT_T) { + recentVal = builder->create_fcmp_ne(recentVal, CONST_FLOAT(0)); + } + builder->create_cond_br(recentVal, trueBB, falseBB); + builder->BB_ = falseBB; + has_br = false; + falseBB = tempFalse; + + ast.lAndExp->accept(*this); +} diff --git a/compiler/src/ir/genIR.h b/compiler/src/ir/genIR.h new file mode 100644 index 0000000..ec25179 --- /dev/null +++ b/compiler/src/ir/genIR.h @@ -0,0 +1,210 @@ +#pragma once + +#include "ast.h" +#include "ir.h" +#include + +class Scope { +public: + // enter a new scope + void enter() { symbol.push_back({}); } + + // exit a scope + void exit() { symbol.pop_back(); } + + bool in_global() { return symbol.size() == 1; } + + // push a name to scope + // return true if successful + // return false if this name already exits + // but func name could be same with variable name + bool push(std::string name, Value *val) { + bool result; + result = (symbol[symbol.size() - 1].insert({name, val})).second; + return result; + } + + Value *find(std::string name) { + for (auto s = symbol.rbegin(); s != symbol.rend(); s++) { + auto iter = s->find(name); + if (iter != s->end()) { + return iter->second; + } + } + return nullptr; + } + +private: + std::vector> symbol; +}; + +class GenIR : public Visitor { +public: + void visit(CompUnitAST &ast) override; + void visit(DeclDefAST &ast) override; + void visit(DeclAST &ast) override; + void visit(DefAST &ast) override; + void visit(InitValAST &ast) override; + void visit(FuncDefAST &ast) override; + void visit(FuncFParamAST &ast) override; + void visit(BlockAST &ast) override; + void visit(BlockItemAST &ast) override; + void visit(StmtAST &ast) override; + void visit(ReturnStmtAST &ast) override; + void visit(SelectStmtAST &ast) override; + void visit(IterationStmtAST &ast) override; + void visit(AddExpAST &ast) override; + void visit(LValAST &ast) override; + void visit(MulExpAST &ast) override; + void visit(UnaryExpAST &ast) override; + void visit(PrimaryExpAST &ast) override; + void visit(CallAST &ast) override; + void visit(NumberAST &ast) override; + void visit(RelExpAST &ast) override; + void visit(EqExpAST &ast) override; + void visit(LAndExpAST &ast) override; + void visit(LOrExpAST &ast) override; + + IRStmtBuilder *builder; + Scope scope; + std::unique_ptr module; + + GenIR() { + module = std::unique_ptr(new Module()); + builder = new IRStmtBuilder(nullptr, module.get()); + auto TyVoid = module->void_ty_; + auto TyInt32 = module->int32_ty_; + auto TyInt32Ptr = module->get_pointer_type(module->int32_ty_); + auto TyFloat = module->float32_ty_; + auto TyFloatPtr = module->get_pointer_type(module->float32_ty_); + + auto input_type = new FunctionType(TyInt32, {}); + auto get_int = new Function(input_type, "getint", module.get()); + + input_type = new FunctionType(TyFloat, {}); + auto get_float = new Function(input_type, "getfloat", module.get()); + + input_type = new FunctionType(TyInt32, {}); + auto get_char = new Function(input_type, "getch", module.get()); + + std::vector input_params; + std::vector().swap(input_params); + input_params.push_back(TyInt32Ptr); + input_type = new FunctionType(TyInt32, input_params); + auto get_int_array = new Function(input_type, "getarray", module.get()); + + std::vector().swap(input_params); + input_params.push_back(TyFloatPtr); + input_type = new FunctionType(TyInt32, input_params); + auto get_float_array = new Function(input_type, "getfarray", module.get()); + + std::vector output_params; + std::vector().swap(output_params); + output_params.push_back(TyInt32); + auto output_type = new FunctionType(TyVoid, output_params); + auto put_int = new Function(output_type, "putint", module.get()); + + std::vector().swap(output_params); + output_params.push_back(TyFloat); + output_type = new FunctionType(TyVoid, output_params); + auto put_float = new Function(output_type, "putfloat", module.get()); + + std::vector().swap(output_params); + output_params.push_back(TyInt32); + output_type = new FunctionType(TyVoid, output_params); + auto put_char = new Function(output_type, "putch", module.get()); + + std::vector().swap(output_params); + output_params.push_back(TyInt32); + output_params.push_back(TyInt32Ptr); + output_type = new FunctionType(TyVoid, output_params); + auto put_int_array = new Function(output_type, "putarray", module.get()); + + std::vector().swap(output_params); + output_params.push_back(TyInt32); + output_params.push_back(TyFloatPtr); + output_type = new FunctionType(TyVoid, output_params); + auto put_float_array = new Function(output_type, "putfarray", module.get()); + + output_params.clear(); + // output_params.push_back(TyInt32); + auto time_type = new FunctionType(TyVoid, output_params); + auto sysy_start_time = + new Function(time_type, "_sysy_starttime", module.get()); + auto sysy_stop_time = + new Function(time_type, "_sysy_stoptime", module.get()); + + output_params.clear(); + output_params.push_back(TyInt32Ptr); + output_params.push_back(TyInt32Ptr); + output_params.push_back(TyInt32); + output_type = new FunctionType(TyVoid, output_params); + auto memcpy = new Function(output_type, "__aeabi_memcpy4", module.get()); + + output_params.clear(); + output_params.push_back(TyInt32Ptr); + output_params.push_back(TyInt32); + output_type = new FunctionType(TyVoid, output_params); + auto memclr = new Function(output_type, "__aeabi_memclr4", module.get()); + + output_params.push_back(TyInt32); + output_type = new FunctionType(TyVoid, output_params); + auto memset = new Function(output_type, "__aeabi_memset4", module.get()); + + output_params.clear(); + output_type = new FunctionType(TyVoid, output_params); + auto llvm_memset = + new Function(output_type, "llvm.memset.p0.i32", module.get()); + + // output_params.clear(); + // output_params.push_back(TyInt32); + // output_type = new FunctionType(TyInt32, output_params); + // auto my_malloc = new Function(output_type, "malloc", module.get()); + + scope.enter(); + scope.push("getint", get_int); + scope.push("getfloat", get_float); + scope.push("getch", get_char); + scope.push("getarray", get_int_array); + scope.push("getfarray", get_float_array); + scope.push("putint", put_int); + scope.push("putfloat", put_float); + scope.push("putch", put_char); + scope.push("putarray", put_int_array); + scope.push("putfarray", put_float_array); + scope.push("starttime", sysy_start_time); + scope.push("stoptime", sysy_stop_time); + scope.push("memcpy", memcpy); + scope.push("memclr", memclr); + scope.push("memset", memset); + scope.push("llvm.memset.p0.i32", llvm_memset); + // scope.push("malloc",my_malloc); + } + std::unique_ptr getModule() { return std::move(module); } + + void checkInitType() const; + + static int getNextDim(vector &dimensionsCnt, int up, int cnt); + + void localInit(Value *ptr, vector> &list, + vector &dimensionsCnt, int up); + + static int getNextDim(vector &elementsCnts, int up); + + ConstantArray *globalInit(vector &dimensions, + vector &arrayTys, int up, + vector> &list); + + static void mergeElements(vector &dimensions, + vector &arrayTys, int up, int dimAdd, + vector &elements, + vector &elementsCnts); + + void finalMerge(vector &dimensions, vector &arrayTys, + int up, vector &elements, + vector &elementsCnts) const; + + bool checkCalType(Value **val, int *intVal, float *floatVal); + + void checkCalType(Value **val); +}; diff --git a/compiler/src/ir/ir.cpp b/compiler/src/ir/ir.cpp new file mode 100644 index 0000000..bfb5cf0 --- /dev/null +++ b/compiler/src/ir/ir.cpp @@ -0,0 +1,838 @@ +#include "ir.h" + +std::map instr_id2string_ = { + {Instruction::Ret, "ret"}, + {Instruction::Br, "br"}, + {Instruction::FNeg, "fneg"}, + {Instruction::Add, "add"}, + {Instruction::Sub, "sub"}, + {Instruction::Mul, "mul"}, + {Instruction::SDiv, "sdiv"}, + {Instruction::SRem, "srem"}, + {Instruction::UDiv, "udiv"}, + {Instruction::URem, "urem"}, + {Instruction::FAdd, "fadd"}, + {Instruction::FSub, "fsub"}, + {Instruction::FMul, "fmul"}, + {Instruction::FDiv, "fdiv"}, + {Instruction::Shl, "shl"}, + {Instruction::LShr, "lshr"}, + {Instruction::AShr, "ashr"}, + {Instruction::And, "and"}, + {Instruction::Or, "or"}, + {Instruction::Xor, "xor"}, + {Instruction::Alloca, "alloca"}, + {Instruction::Load, "load"}, + {Instruction::Store, "store"}, + {Instruction::GetElementPtr, "getelementptr"}, + {Instruction::ZExt, "zext"}, + {Instruction::FPtoSI, "fptosi"}, + {Instruction::SItoFP, "sitofp"}, + {Instruction::BitCast, "bitcast"}, + {Instruction::ICmp, "icmp"}, + {Instruction::FCmp, "fcmp"}, + {Instruction::PHI, "phi"}, + {Instruction::Call, "call"}}; // Instruction from opid to string +const std::map ICmpInst::ICmpOpName = { + {ICmpInst::ICmpOp::ICMP_EQ, "eq"}, {ICmpInst::ICmpOp::ICMP_NE, "ne"}, + {ICmpInst::ICmpOp::ICMP_UGT, "hi"}, {ICmpInst::ICmpOp::ICMP_UGE, "cs"}, + {ICmpInst::ICmpOp::ICMP_ULT, "cc"}, {ICmpInst::ICmpOp::ICMP_ULE, "ls"}, + {ICmpInst::ICmpOp::ICMP_SGT, "gt"}, {ICmpInst::ICmpOp::ICMP_SGE, "ge"}, + {ICmpInst::ICmpOp::ICMP_SLT, "lt"}, {ICmpInst::ICmpOp::ICMP_SLE, "le"}}; +const std::map FCmpInst::FCmpOpName = { + {FCmpInst::FCmpOp::FCMP_FALSE, "nv"}, {FCmpInst::FCmpOp::FCMP_OEQ, "eq"}, + {FCmpInst::FCmpOp::FCMP_OGT, "gt"}, {FCmpInst::FCmpOp::FCMP_OGE, "ge"}, + {FCmpInst::FCmpOp::FCMP_OLT, "cc"}, {FCmpInst::FCmpOp::FCMP_OLE, "ls"}, + {FCmpInst::FCmpOp::FCMP_ONE, "ne"}, {FCmpInst::FCmpOp::FCMP_ORD, "vc"}, + {FCmpInst::FCmpOp::FCMP_UNO, "vs"}, {FCmpInst::FCmpOp::FCMP_UEQ, "eq"}, + {FCmpInst::FCmpOp::FCMP_UGT, "hi"}, {FCmpInst::FCmpOp::FCMP_UGE, "cs"}, + {FCmpInst::FCmpOp::FCMP_ULT, "lt"}, {FCmpInst::FCmpOp::FCMP_ULE, "le"}, + {FCmpInst::FCmpOp::FCMP_UNE, "ne"}, {FCmpInst::FCmpOp::FCMP_TRUE, "al"}}; +std::string print_as_op(Value *v, bool print_ty); +std::string print_cmp_type(ICmpInst::ICmpOp op); +std::string print_fcmp_type(FCmpInst::FCmpOp op); +//-----------------------------------------------Type----------------------------------------------- +std::string Type::print() { + std::string type_ir; + switch (this->tid_) { + case VoidTyID: + type_ir += "void"; + break; + case LabelTyID: + type_ir += "label"; + break; + case IntegerTyID: + type_ir += "i"; + type_ir += std::to_string(static_cast(this)->num_bits_); + break; + case FloatTyID: + type_ir += "float"; + break; + case FunctionTyID: + type_ir += static_cast(this)->result_->print(); + type_ir += " ("; + for (size_t i = 0; i < static_cast(this)->args_.size(); + i++) { + if (i) + type_ir += ", "; + type_ir += static_cast(this)->args_[i]->print(); + } + type_ir += ")"; + break; + case PointerTyID: + type_ir += static_cast(this)->contained_->print(); + type_ir += "*"; + break; + case ArrayTyID: + type_ir += "["; + type_ir += std::to_string(static_cast(this)->num_elements_); + type_ir += " x "; + type_ir += static_cast(this)->contained_->print(); + type_ir += "]"; + break; + default: + break; + } + return type_ir; +} + +//-----------------------------------------------Value----------------------------------------------- +void Value::replace_all_use_with(Value *new_val) { + for (auto use : use_list_) { + auto val = dynamic_cast(use.val_); +#ifdef DEBUG + assert(val && "new_val is not a user"); +#endif + val->set_operand(use.arg_no_, new_val); + } +} +bool Value::remove_used(Instruction *user, unsigned int i) { + if (this != user->operands_[i]) { + return false; + } + auto pos = user->use_pos_[i]; + use_list_.erase(pos); + user->operands_[i] = + nullptr; // 表示user->use_pos_[i]失效了,提示set_operand不要再删除 + return true; +} + +bool Value::is_constant() { return name_[0] == 0; } + +//-----------------------------------------------Constant----------------------------------------------- +std::string ConstantInt::print() { + std::string const_ir; + if (this->type_->tid_ == Type::IntegerTyID && + static_cast(this->type_)->num_bits_ == 1) { + // int1 + const_ir += (this->value_ == 0) ? "0" : "1"; + } else // int32 + const_ir += std::to_string(this->value_); + return const_ir; +} + +std::string ConstantFloat::print() { + std::stringstream fp_ir_ss; + std::string fp_ir; + double val = this->value_; + fp_ir_ss << "0x" << std::hex << *(uint64_t *)&val << std::endl; + fp_ir_ss >> fp_ir; + return fp_ir; +} + +std::string ConstantFloat::print32() { + std::stringstream fp_ir_ss; + std::string fp_ir; + float val = this->value_; + fp_ir_ss << "0x" << std::hex << *(uint32_t *)&val << std::endl; + fp_ir_ss >> fp_ir; + return fp_ir; +} + +std::string ConstantArray::print() { + std::string const_ir; + const_ir += "["; + const_ir += static_cast(this->type_)->contained_->print(); + const_ir += " "; + const_ir += const_array[0]->print(); + for (size_t i = 1; i < this->const_array.size(); i++) { + const_ir += ", "; + const_ir += static_cast(this->type_)->contained_->print(); + const_ir += " "; + const_ir += const_array[i]->print(); + } + const_ir += "]"; + return const_ir; +} + +std::string ConstantZero::print() { return "zeroinitializer"; } + +//-----------------------------------------------Module----------------------------------------------- +std::string Module::print() { + std::string module_ir; + for (auto global_val : this->global_list_) { + module_ir += global_val->print(); + module_ir += "\n"; + } + for (auto func : this->function_list_) { + module_ir += func->print(); + module_ir += "\n"; + } + return module_ir; +} + +Function *Module::getMainFunc() { + for (auto f : function_list_) { + if (f->name_ == "main") { + return f; + } + } + return nullptr; +} + +//-----------------------------------------------GlobalVariable----------------------------------------------- +std::string GlobalVariable::print() { + std::string global_val_ir; + global_val_ir += print_as_op(this, false); + global_val_ir += " = "; + global_val_ir += (this->is_const_ ? "constant " : "global "); + global_val_ir += static_cast(this->type_)->contained_->print(); + global_val_ir += " "; + global_val_ir += this->init_val_->print(); + return global_val_ir; +} + +//-----------------------------------------------Function----------------------------------------------- +std::string Function::print() { + if (this->name_ == "llvm.memset.p0.i32") { + std::string func_ir = "declare void @llvm.memset.p0.i32(i32*, i8, i32, i1)"; + return func_ir; + } + set_instr_name(); + std::string func_ir; + if (this->is_declaration()) + func_ir += "declare "; + else + func_ir += "define "; + + func_ir += this->get_return_type()->print(); + func_ir += " "; + func_ir += print_as_op(this, false); + func_ir += "("; + + // print arg + if (this->is_declaration()) { + for (size_t i = 0; i < this->arguments_.size(); i++) { + if (i) + func_ir += ", "; + func_ir += static_cast(this->type_)->args_[i]->print(); + } + } else { + for (auto arg = this->arguments_.begin(); arg != arguments_.end(); arg++) { + if (arg != this->arguments_.begin()) { + func_ir += ", "; + } + func_ir += static_cast(*arg)->print(); + } + } + func_ir += ")"; + + // print bb + if (!this->is_declaration()) { + func_ir += " {"; + func_ir += "\n"; + for (auto bb : this->basic_blocks_) { + func_ir += bb->print(); + } + func_ir += "}"; + } + + return func_ir; +} + +std::string Argument::print() { + std::string arg_ir; + arg_ir += this->type_->print(); + arg_ir += " %"; + arg_ir += this->name_; + return arg_ir; +} + +void Function::remove_bb(BasicBlock *bb) { + // basic_blocks_.remove(bb); + basic_blocks_.erase( + std::remove(basic_blocks_.begin(), basic_blocks_.end(), bb), + basic_blocks_.end()); + for (auto pre : bb->pre_bbs_) { + pre->remove_succ_basic_block(bb); + } + for (auto succ : bb->succ_bbs_) { + succ->remove_pre_basic_block(bb); + } +} + +BasicBlock *Function::getRetBB() { + for (auto bb : basic_blocks_) { + if (bb->get_terminator()->is_ret()) { + return bb; + } + } + return nullptr; +} + +//-----------------------------------------------BasicBlock----------------------------------------------- +std::string BasicBlock::print() { + std::string bb_ir; + bb_ir += this->name_; + bb_ir += ":"; + // print prebb + if (!this->pre_bbs_.empty()) { + bb_ir += " ; preds = "; + } + for (auto bb : this->pre_bbs_) { + if (bb != *this->pre_bbs_.begin()) + bb_ir += ", "; + bb_ir += print_as_op(bb, false); + } + + // print prebb + if (!this->parent_) { + bb_ir += "\n"; + bb_ir += "; Error: Block without parent!"; + } + bb_ir += "\n"; + for (auto instr : this->instr_list_) { + bb_ir += " "; + bb_ir += instr->print(); + bb_ir += "\n"; + } + + return bb_ir; +} + +Instruction *BasicBlock::get_terminator() { + if (instr_list_.empty()) + return nullptr; + switch (instr_list_.back()->op_id_) { + case Instruction::Ret: + case Instruction::Br: + return instr_list_.back(); + default: + return nullptr; + } +} + +bool BasicBlock::delete_instr(Instruction *instr) { + //******************--------instvec2list-----qwc20220814 + // instr_list_.remove(instr); + if ((!instr) || instr->pos_in_bb.size() != 1 || instr->parent_ != this) + return false; + this->instr_list_.erase(instr->pos_in_bb.back()); + // instr_list_.erase(std::remove(instr_list_.begin(), instr_list_.end(), + // instr) , instr_list_.end()); + instr->remove_use_of_ops(); + instr->pos_in_bb.clear(); // 保证指令自由身 + instr->parent_ = nullptr; + return true; +} + +bool BasicBlock::add_instruction(Instruction *instr) { + //******************--------instvec2list-----qwc20220814 + + if (instr->pos_in_bb.size() != 0) { // 指令已经插入到某个地方了 + return false; + } else { + instr_list_.push_back(instr); + std::list::iterator tail = instr_list_.end(); + instr->pos_in_bb.emplace_back(--tail); + instr->parent_ = this; + return true; + } +} + +bool BasicBlock::add_instruction_front(Instruction *instr) { + //******************--------instvec2list-----qwc20220814 + if (instr->pos_in_bb.size() != 0) { // 指令已经插入到某个地方了 + return false; + } else { + instr_list_.push_front(instr); + std::list::iterator head = instr_list_.begin(); + instr->pos_in_bb.emplace_back(head); + instr->parent_ = this; + return true; + } +} + +// 插入到倒数第二位 +bool BasicBlock::add_instruction_before_terminator(Instruction *instr) { + if (instr->pos_in_bb.size() != 0) { // 指令已经插入到某个地方了 + return false; + } else if (instr_list_.empty()) { // 没有“倒数第1位”何来的倒数第二位 + return false; + } else { + auto it = std::end(instr_list_); // 最后一位的后一位位置 + instr_list_.emplace( + --it, + instr); // 插入使得代替最后一位(--it)的位置,此时it是最后一位,插入的是倒数第二位 + instr->pos_in_bb.emplace_back(--it); // 记录插入的结果 + instr->parent_ = this; + return true; + } +} + +bool BasicBlock::add_instruction_before_inst(Instruction *new_instr, + Instruction *instr) { + if ((!instr) || instr->pos_in_bb.size() != 1 || instr->parent_ != this) + return false; + if (new_instr->pos_in_bb.size() != 0) // 指令已经插入到某个地方了 + return false; + else if ( + instr_list_ + .empty()) // bb原本没有指令,那instr不在bb内,那为啥instr->parent_== + // this + return false; + else { + auto it = instr->pos_in_bb.back(); // + instr_list_.emplace( + it, + new_instr); // 插入使得代替最后一位(--it)的位置,此时it是最后一位,插入的是倒数第二位 + new_instr->pos_in_bb.emplace_back(--it); // 记录插入的结果 + new_instr->parent_ = this; + return true; + } +} + +// 从bb移出一个指令,但是不删指令的use关系,因为还要插入其他bb +bool BasicBlock::remove_instr(Instruction *instr) { + // instr_list_.remove(instr); + if ((!instr) || instr->pos_in_bb.size() != 1 || instr->parent_ != this) + return false; + this->instr_list_.erase(instr->pos_in_bb.back()); + // instr->remove_use_of_ops(); + instr->pos_in_bb.clear(); // 保证指令自由身 + instr->parent_ = nullptr; + return true; +} + +//-----------------------------------------------Instruction----------------------------------------------- +std::string BinaryInst::print() { + std::string instr_ir; + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + instr_ir += this->operands_[0]->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), false); + instr_ir += ", "; + assert(this->get_operand(0)->type_->tid_ == + this->get_operand(1)->type_->tid_); + instr_ir += print_as_op(this->get_operand(1), false); + // instr_ir += print_as_op(this->get_operand(1), true); + return instr_ir; +} + +std::string UnaryInst::print() { + std::string instr_ir; + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + instr_ir += this->operands_[0]->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), false); + switch (this->op_id_) { + case Instruction::ZExt: + assert(this->type_->tid_ == Type::IntegerTyID); + instr_ir += " to i32"; + break; + case Instruction::FPtoSI: + assert(this->type_->tid_ == Type::IntegerTyID); + instr_ir += " to i32"; + break; + case Instruction::SItoFP: + assert(this->type_->tid_ == Type::FloatTyID); + instr_ir += " to float"; + break; + default: + assert(0 && "UnaryInst opID invalid!"); + break; + } + return instr_ir; +} + +std::string ICmpInst::print() { + std::string instr_ir; + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + instr_ir += print_cmp_type(this->icmp_op_); + instr_ir += " "; + instr_ir += this->get_operand(0)->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), false); + instr_ir += ", "; + if (this->get_operand(0)->type_->tid_ == this->get_operand(1)->type_->tid_) { + instr_ir += print_as_op(this->get_operand(1), false); + } else { + instr_ir += print_as_op(this->get_operand(1), true); + } + return instr_ir; +} + +std::string FCmpInst::print() { + std::string instr_ir; + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + instr_ir += print_fcmp_type(this->fcmp_op_); + instr_ir += " "; + instr_ir += this->get_operand(0)->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), false); + instr_ir += ", "; + if (this->get_operand(0)->type_->tid_ == this->get_operand(1)->type_->tid_) { + instr_ir += print_as_op(this->get_operand(1), false); + } else { + instr_ir += print_as_op(this->get_operand(1), true); + } + return instr_ir; +} + +std::string CallInst::print() { + std::string instr_ir; + if (!(this->type_->tid_ == Type::VoidTyID)) { + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + } + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + unsigned int numops = this->num_ops_; + instr_ir += static_cast(this->get_operand(numops - 1)->type_) + ->result_->print(); + + instr_ir += " "; + assert(dynamic_cast(this->get_operand(numops - 1)) && + "Wrong call operand function"); + //__aeabi_memclr4 -> llvm_memset + if (dynamic_cast(this->get_operand(numops - 1))->name_ == + "__aeabi_memclr4") { + instr_ir += "@llvm.memset.p0.i32("; + // i32* 目的内存地址 + instr_ir += this->get_operand(0)->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), false); + // i8 0 + instr_ir += ", i8 0, "; + // i32 修改总字节数 + instr_ir += this->get_operand(1)->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(1), false); + // i1 false + instr_ir += ", i1 false)"; + return instr_ir; + } + + instr_ir += print_as_op(this->get_operand(numops - 1), false); + instr_ir += "("; + for (unsigned int i = 0; i < numops - 1; i++) { + if (i > 0) + instr_ir += ", "; + instr_ir += this->get_operand(i)->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(i), false); + } + instr_ir += ")"; + return instr_ir; +} + +std::string BranchInst::print() { + std::string instr_ir; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), true); + + if (this->num_ops_ == 3) { + instr_ir += ", "; + instr_ir += print_as_op(this->get_operand(1), true); + instr_ir += ", "; + instr_ir += print_as_op(this->get_operand(2), true); + } + return instr_ir; +} + +std::string ReturnInst::print() { + std::string instr_ir; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + if (this->num_ops_ != 0) { + instr_ir += this->get_operand(0)->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), false); + } else { + instr_ir += "void"; + } + + return instr_ir; +} + +std::string GetElementPtrInst::print() { + std::string instr_ir; + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + assert(this->get_operand(0)->type_->tid_ == Type::PointerTyID); + instr_ir += static_cast(this->get_operand(0)->type_) + ->contained_->print(); + instr_ir += ", "; + for (unsigned int i = 0; i < this->num_ops_; i++) { + if (i > 0) + instr_ir += ", "; + instr_ir += this->get_operand(i)->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(i), false); + } + return instr_ir; +} + +std::string StoreInst::print() { + std::string instr_ir; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + instr_ir += this->get_operand(0)->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), false); + instr_ir += ", "; + instr_ir += print_as_op(this->get_operand(1), true); + return instr_ir; +} + +std::string LoadInst::print() { + std::string instr_ir; + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + assert(this->get_operand(0)->type_->tid_ == Type::PointerTyID); + instr_ir += static_cast(this->get_operand(0)->type_) + ->contained_->print(); + instr_ir += ","; + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), true); + return instr_ir; +} + +std::string AllocaInst::print() { + std::string instr_ir; + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + instr_ir += alloca_ty_->print(); + return instr_ir; +} + +std::string ZextInst::print() { + std::string instr_ir; + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + instr_ir += this->get_operand(0)->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), false); + instr_ir += " to "; + instr_ir += this->dest_ty_->print(); + return instr_ir; +} + +std::string FpToSiInst::print() { + std::string instr_ir; + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + instr_ir += this->get_operand(0)->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), false); + instr_ir += " to "; + instr_ir += this->dest_ty_->print(); + return instr_ir; +} + +std::string SiToFpInst::print() { + std::string instr_ir; + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + instr_ir += this->get_operand(0)->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), false); + instr_ir += " to "; + instr_ir += this->dest_ty_->print(); + return instr_ir; +} + +std::string Bitcast::print() { + std::string instr_ir; + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + instr_ir += this->get_operand(0)->type_->print(); + instr_ir += " "; + instr_ir += print_as_op(this->get_operand(0), false); + instr_ir += " to "; + instr_ir += this->dest_ty_->print(); + return instr_ir; +} + +std::string PhiInst::print() { + std::string instr_ir; + instr_ir += "%"; + instr_ir += this->name_; + instr_ir += " = "; + instr_ir += instr_id2string_[this->op_id_]; + instr_ir += " "; + instr_ir += this->get_operand(0)->type_->print(); + instr_ir += " "; + for (int i = 0; i < this->num_ops_ / 2; i++) { + if (i > 0) + instr_ir += ", "; + instr_ir += "[ "; + instr_ir += print_as_op(this->get_operand(2 * i), false); + instr_ir += ", "; + instr_ir += print_as_op(this->get_operand(2 * i + 1), false); + instr_ir += " ]"; + } + if (this->num_ops_ / 2 < this->parent_->pre_bbs_.size()) { + for (auto pre_bb : this->parent_->pre_bbs_) { + if (std::find(this->operands_.begin(), this->operands_.end(), + static_cast(pre_bb)) == this->operands_.end()) { + // find a pre_bb is not in phi + instr_ir += ", [ undef, " + print_as_op(pre_bb, false) + " ]"; + } + } + } + return instr_ir; +} + +std::string print_as_op(Value *v, bool print_ty) { + std::string op_ir; + if (print_ty) { + op_ir += v->type_->print(); + op_ir += " "; + } + + if (dynamic_cast(v)) { + op_ir += "@" + v->name_; + } else if (dynamic_cast(v)) { + op_ir += "@" + v->name_; + } else if (dynamic_cast(v)) { + op_ir += v->print(); + } else { + op_ir += "%" + v->name_; + } + + return op_ir; +} + +std::string print_cmp_type(ICmpInst::ICmpOp op) { + switch (op) { + case ICmpInst::ICMP_SGE: + return "sge"; + break; + case ICmpInst::ICMP_SGT: + return "sgt"; + break; + case ICmpInst::ICMP_SLE: + return "sle"; + break; + case ICmpInst::ICMP_SLT: + return "slt"; + break; + case ICmpInst::ICMP_EQ: + return "eq"; + break; + case ICmpInst::ICMP_NE: + return "ne"; + break; + default: + break; + } + return "wrong cmpop"; +} + +std::string print_fcmp_type(FCmpInst::FCmpOp op) { + switch (op) { + case FCmpInst::FCMP_UGE: + return "uge"; + break; + case FCmpInst::FCMP_UGT: + return "ugt"; + break; + case FCmpInst::FCMP_ULE: + return "ule"; + break; + case FCmpInst::FCMP_ULT: + return "ult"; + break; + case FCmpInst::FCMP_UEQ: + return "ueq"; + break; + case FCmpInst::FCMP_UNE: + return "une"; + break; + default: + break; + } + return "wrong fcmpop"; +} + +void Function::set_instr_name() { + std::map seq; + for (auto arg : this->arguments_) { + if (!seq.count(arg)) { + auto seq_num = seq.size() + seq_cnt_; + if (arg->name_ == "") { + arg->name_ = "arg_" + std::to_string(seq_num); + seq.insert({arg, seq_num}); + } + } + } + for (auto bb : basic_blocks_) { + if (!seq.count(bb)) { + auto seq_num = seq.size() + seq_cnt_; + if (bb->name_.length() <= 6 || bb->name_.substr(0, 6) != "label_") { + bb->name_ = "label_" + std::to_string(seq_num); + seq.insert({bb, seq_num}); + } + } + for (auto instr : bb->instr_list_) { + if (instr->type_->tid_ != Type::VoidTyID && !seq.count(instr)) { + auto seq_num = seq.size() + seq_cnt_; + if (instr->name_ == "") { + instr->name_ = "v" + std::to_string(seq_num); + seq.insert({instr, seq_num}); + } + } + } + } + seq_cnt_ += seq.size(); +} diff --git a/compiler/src/ir/ir.h b/compiler/src/ir/ir.h new file mode 100644 index 0000000..3f33e7d --- /dev/null +++ b/compiler/src/ir/ir.h @@ -0,0 +1,961 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class Type; +class IntegerType; +class ArrayType; +class PointerType; +class FunctionType; +class Value; +class Constant; +class ConstantInt; +class ConstantFloat; +class ConstantArray; +class ConstantZero; +class Module; +class GlobalVariable; +class Function; +class BasicBlock; +class Argument; +class Instruction; +class BinaryInst; +class UnaryInst; +class ICmpInst; +class FCmpInst; +class CallInst; +class BranchInst; +class ReturnInst; +class GetElementPtrInst; +class StoreInst; +class LoadInst; +class AllocaInst; + +struct Use { + Value *val_; + unsigned int arg_no_; // 操作数的序号,如func(a,b)中a的序号为0,b的序号为1 + Use(Value *val, unsigned int no) : val_(val), arg_no_(no) {} +}; + + +class Type { +public: + enum TypeID { + VoidTyID, // Void + LabelTyID, // Labels, e.g., BasicBlock + IntegerTyID, // Integers, include 32 bits and 1 bit + FloatTyID, // Floats, only 32 bits + FunctionTyID, // Functions + ArrayTyID, // Arrays + PointerTyID, // Pointer + }; + explicit Type(TypeID tid) : tid_(tid) {} + ~Type() = default; + virtual std::string print(); + TypeID tid_; +}; + +class IntegerType : public Type { +public: + explicit IntegerType(unsigned num_bits) + : Type(Type::IntegerTyID), num_bits_(num_bits) {} + unsigned num_bits_; +}; + +//[2 x [3 x i32]]: num_elements_ = 2, contained_ = [3 x i32] +class ArrayType : public Type { +public: + ArrayType(Type *contained, unsigned num_elements) + : Type(Type::ArrayTyID), num_elements_(num_elements), + contained_(contained) {} + Type *contained_; // The element type of the array. + unsigned num_elements_; // Number of elements in the array. +}; + +//[2 x [3 x i32]]* +class PointerType : public Type { +public: + PointerType(Type *contained) + : Type(Type::PointerTyID), contained_(contained) {} + Type *contained_; // The element type of the ptr. +}; + +// declare i32 @putarray(i32, i32*) +class FunctionType : public Type { +public: + FunctionType(Type *result, std::vector params) + : Type(Type::FunctionTyID) { + result_ = result; + for (Type *p : params) { + args_.push_back(p); + } + } + Type *result_; + std::vector args_; +}; + + +class Value { +public: + explicit Value(Type *ty, const std::string &name = "") + : type_(ty), name_(name) {} + ~Value() = default; + virtual std::string print() = 0; + + void remove_use(Value *val) { + auto is_val = [val](const Use &use) { return use.val_ == val; }; + use_list_.remove_if(is_val); + } + + //****************************************************************** + std::list::iterator add_use(Value *val, unsigned arg_no) { + use_list_.emplace_back(Use(val, arg_no)); + std::list::iterator re = use_list_.end(); + return --re; + } + // 删除迭代器指出的use + void remove_use(std::list::iterator it) { use_list_.erase(it); } + // user的第i个操作数准备不再使用this,因此删除this与user相关的use联系 + bool remove_used(Instruction *user, unsigned int i); + + // Return if the value is a constant. + bool is_constant(); + + //****************************************************************** + + void replace_all_use_with(Value *new_val); + Type *type_; + std::string name_; + std::list + use_list_; // 所有引用该Value的Instruction的集合,以及该Value在该Instruction的第几个操作数位置被引用 +}; + + +// 常量都是无名的(name=="") +class Constant : public Value { +public: + Constant(Type *ty, const std::string &name = "") : Value(ty, name) {} + ~Constant() = default; +}; + +// i32 -23 +class ConstantInt : public Constant { +public: + ConstantInt(Type *ty, int val) : Constant(ty, ""), value_(val) {} + virtual std::string print() override; + int value_; +}; + +// float 0x4057C21FC0000000 +// float -3.300000e+04 +class ConstantFloat : public Constant { +public: + ConstantFloat(Type *ty, float val) : Constant(ty, ""), value_(val) {} + virtual std::string print() override; + float value_; + std::string print32(); +}; + +//[3 x i32] [i32 42, i32 11, i32 74] +class ConstantArray : public Constant { +public: + ConstantArray(ArrayType *ty, const std::vector &val) + : Constant(ty, "") { + this->const_array.assign(val.begin(), val.end()); + } + ~ConstantArray() = default; + virtual std::string print() override; + std::vector const_array; +}; + +// i32 zeroinitializer +//[2 x [100 x float]] zeroinitializer +// 注意zeroinitializer是有类型的! +class ConstantZero : public Constant { +public: + ConstantZero(Type *ty) : Constant(ty, "") {} + virtual std::string print() override; +}; + + +class Module { +public: + explicit Module() { + void_ty_ = new Type(Type::VoidTyID); + label_ty_ = new Type(Type::LabelTyID); + int1_ty_ = new IntegerType(1); + int32_ty_ = new IntegerType(32); + float32_ty_ = new Type(Type::FloatTyID); + } + ~Module() { + delete void_ty_; + delete label_ty_; + delete int1_ty_; + delete int32_ty_; + delete float32_ty_; + } + virtual std::string print(); + void add_global_variable(GlobalVariable *g) { global_list_.push_back(g); } + void add_function(Function *f) { function_list_.push_back(f); } + PointerType *get_pointer_type(Type *contained) { + if (!pointer_map_.count(contained)) { + pointer_map_[contained] = new PointerType(contained); + } + return pointer_map_[contained]; + } + ArrayType *get_array_type(Type *contained, unsigned num_elements) { + if (!array_map_.count({contained, num_elements})) { + array_map_[{contained, num_elements}] = + new ArrayType(contained, num_elements); + } + return array_map_[{contained, num_elements}]; + } + + Function *getMainFunc(); + + std::vector global_list_; + std::vector function_list_; + + IntegerType *int1_ty_; + IntegerType *int32_ty_; + Type *float32_ty_; + Type *label_ty_; + Type *void_ty_; + std::map pointer_map_; + std::map, ArrayType *> array_map_; +}; + +//-----------------------------------------------GlobalVariable----------------------------------------------- +//@c = global [4 x i32] [i32 6, i32 7, i32 8, i32 9] +//@a = constant [5 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4] +class GlobalVariable : public Value { +public: + GlobalVariable(std::string name, Module *m, Type *ty, bool is_const, + Constant *init = nullptr) + : Value(m->get_pointer_type(ty), name), is_const_(is_const), + init_val_(init) { + m->add_global_variable(this); + } + virtual std::string print() override; + bool is_const_; + Constant *init_val_; +}; + +- + +//Argument的构造函数只由Function的构造函数调用,不单独调用 +class Argument : public Value { +public: + explicit Argument(Type *ty, const std::string &name = "", + Function *f = nullptr, unsigned arg_no = 0) + : Value(ty, name), parent_(f), arg_no_(arg_no) {} + ~Argument() {} + virtual std::string print() override; + Function *parent_; + unsigned arg_no_; // argument No. +}; + +class Function : public Value { +public: + Function(FunctionType *ty, const std::string &name, Module *parent) + : Value(ty, name), parent_(parent), seq_cnt_(0) { + parent->add_function(this); + size_t num_args = ty->args_.size(); + use_ret_cnt = 0; + for (size_t i = 0; i < num_args; i++) { + arguments_.push_back(new Argument(ty->args_[i], "", this, i)); + } + } + ~Function(); + virtual std::string print() override; + void add_basic_block(BasicBlock *bb) { basic_blocks_.push_back(bb); } + Type *get_return_type() const { + return static_cast(type_)->result_; + } + bool is_declaration() { return basic_blocks_.empty(); } + void set_instr_name(); + void remove_bb(BasicBlock *bb); + BasicBlock *getRetBB(); + + std::vector basic_blocks_; // basic blocks + std::vector arguments_; // argument + Module *parent_; + unsigned seq_cnt_; + std::vector> vreg_set_; + int use_ret_cnt; // 程序中真正使用返回值的次数 +}; + +// BasicBlock一定是LabelTyID +class BasicBlock : public Value { +public: + explicit BasicBlock(Module *m, const std::string &name, Function *parent) + : Value(m->label_ty_, name), parent_(parent) { + parent_->add_basic_block(this); + } + bool add_instruction(Instruction *instr); // 尾部插入指令,返回成功与否 + bool add_instruction_front(Instruction *instr); // 头部插入指令,返回成功与否 + bool add_instruction_before_terminator( + Instruction *instr); // 插入到BB倒数第二条指令,即br前 + bool add_instruction_before_inst( + Instruction *new_inst, + Instruction * + inst); // 将新指令插入到原来指令前,返回成功与否,需要保证原指令在bb内 + void add_pre_basic_block(BasicBlock *bb) { pre_bbs_.push_back(bb); } + void add_succ_basic_block(BasicBlock *bb) { succ_bbs_.push_back(bb); } + void remove_pre_basic_block(BasicBlock *bb) { + // pre_bbs_.remove(bb); + pre_bbs_.erase(std::remove(pre_bbs_.begin(), pre_bbs_.end(), bb), + pre_bbs_.end()); + } + void remove_succ_basic_block(BasicBlock *bb) { + // succ_bbs_.remove(bb); + succ_bbs_.erase(std::remove(succ_bbs_.begin(), succ_bbs_.end(), bb), + succ_bbs_.end()); + } + int isDominate( + BasicBlock + *bb2) { // 返回1表示支配bb2,返回0表示不支配,返回-1输入的块出错 + if (!bb2 || this->parent_ != bb2->parent_) + return -1; + while (bb2->name_ != "label_entry") { + if (bb2->idom_ == this) + return 1; + bb2 = bb2->idom_; + } + return 0; + } + // Returns the terminator instruction if the block is well formed or null + // if the block is not well formed. + Instruction *get_terminator(); + bool delete_instr( + Instruction *instr); // 返回false则说明指令不能重复删除或者不属于这个bb, + bool remove_instr( + Instruction * + instr); // 从bb移出一个指令,但是不删指令的use关系,因为还要插入其他bb + virtual std::string print() override; + + //********************使用list替换vector--------- + std::list instr_list_; + //********************使用list替换vector--------- + + Function *parent_; + /****************api about cfg****************/ + std::vector pre_bbs_; + std::vector succ_bbs_; + /****************api about dominate tree****************/ + std::set dom_frontier_; + std::set rdom_frontier_; + std::set rdoms_; + BasicBlock *idom_; + std::set live_in; + std::set live_out; +}; + + +class Instruction : public Value { +public: + enum OpID { + // Terminator Instructions + Ret = 11, + Br, + // Standard unary operators + FNeg, + // Standard binary operators + Add, + Sub, + Mul, + SDiv, + SRem, + UDiv, + URem, + // Float binary opeartors + FAdd, + FSub, + FMul, + FDiv, + // Logical operators + Shl, + LShr, + AShr, + And, + Or, + Xor, + // Memory operators + Alloca, + Load, + Store, + GetElementPtr, + // Cast operators + ZExt, + FPtoSI, + SItoFP, + BitCast, + // Other operators + ICmp, + FCmp, + PHI, + Call, + }; + // 创建指令并插入基本块(ty是指令返回值类型) + // If before set to true, then use add_instruction_front() instead of + // add_instruction() + Instruction(Type *ty, OpID id, unsigned num_ops, BasicBlock *parent, + bool before = false) + : Value(ty, ""), op_id_(id), num_ops_(num_ops), parent_(parent) { + operands_.resize( + num_ops_, + nullptr); // 此句不能删去!否则operands_为空时无法用set_operand设置操作数,而只能用push_back设置操作数! + use_pos_.resize(num_ops_); + if (!before) + parent_->add_instruction(this); + else + parent_->add_instruction_front(this); + } + // 仅创建指令,不插入基本块(ty是指令返回值类型) + Instruction(Type *ty, OpID id, unsigned num_ops) + : Value(ty, ""), op_id_(id), num_ops_(num_ops), parent_(nullptr) { + operands_.resize(num_ops_, nullptr); + use_pos_.resize(num_ops_); + } + Value *get_operand(unsigned i) const { return operands_[i]; } + + //*************************** + void set_operand(unsigned i, Value *v) { + operands_[i] = v; + use_pos_[i] = v->add_use(this, i); + } + void add_operand(Value *v) { // 添加指令操作数,用于phi指令 + operands_.push_back(v); + use_pos_.emplace_back(v->add_use(this, num_ops_)); + num_ops_++; + } + void + remove_use_of_ops() { // 删除此指令所有操作数的uselist中,与此指令相关的use + for (int i = 0; i < operands_.size(); i++) { + operands_[i]->remove_use(use_pos_[i]); + } + } + // 删除phi指令中的一对操作数 + void remove_operands(int index1, int index2) { + for (int i = index1; i <= index2; i++) { + operands_[i]->remove_use(use_pos_[i]); + } + // 后面操作数的位置要做相应修改 + for (int i = index2 + 1; i < operands_.size(); i++) { + for (auto &use : operands_[i]->use_list_) { + if (use.val_ == this) { + use.arg_no_ -= index2 - index1 + 1; + break; + } + } + } + operands_.erase(operands_.begin() + index1, operands_.begin() + index2 + 1); + use_pos_.erase(use_pos_.begin() + index1, use_pos_.begin() + index2 + 1); + // std::cout<type_->tid_ == Type::VoidTyID)); + } + + bool is_phi() { return op_id_ == PHI; } + bool is_store() { return op_id_ == Store; } + bool is_alloca() { return op_id_ == Alloca; } + bool is_ret() { return op_id_ == Ret; } + bool is_load() { return op_id_ == Load; } + bool is_br() { return op_id_ == Br; } + + bool is_add() { return op_id_ == Add; } + bool is_sub() { return op_id_ == Sub; } + bool is_mul() { return op_id_ == Mul; } + bool is_div() { return op_id_ == SDiv; } + bool is_rem() { return op_id_ == SRem; } + + bool is_fadd() { return op_id_ == FAdd; } + bool is_fsub() { return op_id_ == FSub; } + bool is_fmul() { return op_id_ == FMul; } + bool is_fdiv() { return op_id_ == FDiv; } + + bool is_cmp() { return op_id_ == ICmp; } + bool is_fcmp() { return op_id_ == FCmp; } + + bool is_call() { return op_id_ == Call; } + bool is_gep() { return op_id_ == GetElementPtr; } + bool is_zext() { return op_id_ == ZExt; } + bool is_fptosi() { return op_id_ == FPtoSI; } + bool is_sitofp() { return op_id_ == SItoFP; } + + bool is_int_binary() { + return (is_add() || is_sub() || is_mul() || is_div() || is_rem()) && + (num_ops_ == 2); + } + + bool is_float_binary() { + return (is_fadd() || is_fsub() || is_fmul() || is_fdiv()) && + (num_ops_ == 2); + } + + bool is_binary() { return is_int_binary() || is_float_binary(); } + + bool isTerminator() { return is_br() || is_ret(); } + + + virtual std::string print() = 0; + BasicBlock *parent_; + OpID op_id_; + unsigned num_ops_; + std::vector operands_; // operands of this value + std::vector::iterator> + use_pos_; // 与操作数数组一一对应,是对应的操作数的uselist里面,与当前指令相关的use的迭代器 + std::vector::iterator> + pos_in_bb; // 在bb的指令list的位置迭代器,最多只能有一个 +}; + + +class BinaryInst : public Instruction { +public: + BinaryInst(Type *ty, OpID op, Value *v1, Value *v2, BasicBlock *bb) + : Instruction(ty, op, 2, bb) { + set_operand(0, v1); + set_operand(1, v2); + } + // 只创建,不加入基本块末尾 + BinaryInst(Type *ty, OpID op, Value *v1, Value *v2, BasicBlock *bb, bool flag) + : Instruction(ty, op, 2) { + set_operand(0, v1); + set_operand(1, v2); + this->parent_ = bb; + } + virtual std::string print() override; +}; + + +class UnaryInst : public Instruction { +public: + UnaryInst(Type *ty, OpID op, Value *val, BasicBlock *bb) + : Instruction(ty, op, 1, bb) { + set_operand(0, val); + } + virtual std::string print() override; +}; + +//%18 = icmp ne i32 %12, %17 +class ICmpInst : public Instruction { +public: + enum ICmpOp { + ICMP_EQ = 32, ///< equal + ICMP_NE = 33, ///< not equal + ICMP_UGT = 34, ///< unsigned greater than + ICMP_UGE = 35, ///< unsigned greater or equal + ICMP_ULT = 36, ///< unsigned less than + ICMP_ULE = 37, ///< unsigned less or equal + ICMP_SGT = 38, ///< signed greater than + ICMP_SGE = 39, ///< signed greater or equal + ICMP_SLT = 40, ///< signed less than + ICMP_SLE = 41 ///< signed less or equal + }; + static const std::map ICmpOpName; + ICmpInst(ICmpOp op, Value *v1, Value *v2, BasicBlock *bb) + : Instruction(bb->parent_->parent_->int1_ty_, Instruction::ICmp, 2, bb), + icmp_op_(op) { + set_operand(0, v1); + set_operand(1, v2); + } + virtual std::string print() override; + ICmpOp icmp_op_; +}; + +//%5 = fcmp olt float %4, 0.000000e+00 +class FCmpInst : public Instruction { +public: + enum FCmpOp { + FCMP_FALSE = 10, // Always false (always folded) + FCMP_OEQ = 11, // True if ordered and equal + FCMP_OGT = 12, // True if ordered and greater than + FCMP_OGE = 13, // True if ordered and greater than or equal + FCMP_OLT = 14, // True if ordered and less than + FCMP_OLE = 15, // True if ordered and less than or equal + FCMP_ONE = 16, // True if ordered and operands are unequal + FCMP_ORD = 17, // True if ordered (no nans) + FCMP_UNO = 18, // True if unordered: isnan(X) | isnan(Y) + FCMP_UEQ = 19, // True if unordered or equal + FCMP_UGT = 20, // True if unordered or greater than + FCMP_UGE = 21, // True if unordered, greater than, or equal + FCMP_ULT = 22, // True if unordered or less than + FCMP_ULE = 23, // True if unordered, less than, or equal + FCMP_UNE = 24, // True if unordered or not equal + FCMP_TRUE = 25 // Always true (always folded) + }; + static const std::map FCmpOpName; + FCmpInst(FCmpOp op, Value *v1, Value *v2, BasicBlock *bb) + : Instruction(bb->parent_->parent_->int1_ty_, Instruction::FCmp, 2, bb), + fcmp_op_(op) { + set_operand(0, v1); + set_operand(1, v2); + } + virtual std::string print() override; + FCmpOp fcmp_op_; +}; + +//%111 = call i32 @QuickSort(i32* %108, i32 %109, i32 %110) +class CallInst : public Instruction { +public: + CallInst(Function *func, std::vector args, BasicBlock *bb) + : Instruction(static_cast(func->type_)->result_, + Instruction::Call, args.size() + 1, bb) { + int num_ops = args.size() + 1; + for (int i = 0; i < num_ops - 1; i++) { + set_operand(i, args[i]); + } + set_operand(num_ops - 1, func); + } + virtual std::string print() override; +}; + +// br的返回值类型一定是VoidTyID +class BranchInst : public Instruction { +public: + + BranchInst(Value *cond, BasicBlock *if_true, BasicBlock *if_false, + BasicBlock *bb) + : Instruction(if_true->parent_->parent_->void_ty_, Instruction::Br, 3, + bb) { + if_true->add_pre_basic_block(bb); + if_false->add_pre_basic_block(bb); + bb->add_succ_basic_block(if_false); + bb->add_succ_basic_block(if_true); + set_operand(0, cond); + set_operand(1, if_true); + set_operand(2, if_false); + } + // br label %31 + BranchInst(BasicBlock *if_true, BasicBlock *bb) + : Instruction(if_true->parent_->parent_->void_ty_, Instruction::Br, 1, + bb) { + if_true->add_pre_basic_block(bb); + bb->add_succ_basic_block(if_true); + set_operand(0, if_true); + } + virtual std::string print() override; +}; + + +// ret的返回值类型一定是VoidTyID +class ReturnInst : public Instruction { +public: + ReturnInst(Value *val, BasicBlock *bb) + : Instruction(bb->parent_->parent_->void_ty_, Instruction::Ret, 1, bb) { + set_operand(0, val); + } + ReturnInst(Value *val, BasicBlock *bb, bool flag) + : Instruction(bb->parent_->parent_->void_ty_, Instruction::Ret, 1) { + set_operand(0, val); + this->parent_ = bb; + } + ReturnInst(BasicBlock *bb) + : Instruction(bb->parent_->parent_->void_ty_, Instruction::Ret, 0, bb) {} + virtual std::string print() override; +}; + +//%1 = getelementptr [5 x [4 x i32]], [5 x [4 x i32]]* @a, i32 0, i32 2, i32 3 +class GetElementPtrInst : public Instruction { +public: + GetElementPtrInst(Value *ptr, std::vector idxs, BasicBlock *bb) + : Instruction(bb->parent_->parent_->get_pointer_type( + get_GEP_return_type(ptr, idxs.size())), + Instruction::GetElementPtr, idxs.size() + 1, bb) { + set_operand(0, ptr); + for (size_t i = 0; i < idxs.size(); i++) { + set_operand(i + 1, idxs[i]); + } + } + Type *get_GEP_return_type(Value *ptr, size_t idxs_size) { + Type *ty = + static_cast(ptr->type_)->contained_; //[5 x [4 x i32]] + if (ty->tid_ == Type::ArrayTyID) { + ArrayType *arr_ty = static_cast(ty); + for (size_t i = 1; i < idxs_size; i++) { + ty = arr_ty->contained_; //[4 x i32], i32 + if (ty->tid_ == Type::ArrayTyID) { + arr_ty = static_cast(ty); + } + } + } + return ty; + } + virtual std::string print() override; +}; + + +// store的返回值类型一定是VoidTyID +class StoreInst : public Instruction { +public: + StoreInst(Value *val, Value *ptr, BasicBlock *bb) + : Instruction(bb->parent_->parent_->void_ty_, Instruction::Store, 2, bb) { + assert(val->type_ == static_cast(ptr->type_)->contained_); + set_operand(0, val); + set_operand(1, ptr); + } + + // 创建store指令,不插入到基本块中,但是设定parent + StoreInst(Value *val, Value *ptr, BasicBlock *bb, bool) + : Instruction(bb->parent_->parent_->void_ty_, Instruction::Store, 2) { + assert(val->type_ == static_cast(ptr->type_)->contained_); + set_operand(0, val); + set_operand(1, ptr); + this->parent_ = bb; + } + + virtual std::string print() override; +}; + +// = load , * +class LoadInst : public Instruction { +public: + LoadInst(Value *ptr, BasicBlock *bb) + : Instruction(static_cast(ptr->type_)->contained_, + Instruction::Load, 1, bb) { + set_operand(0, ptr); + } + virtual std::string print() override; +}; + +//%8 = alloca i32 +class AllocaInst : public Instruction { +public: + AllocaInst(Type *ty, BasicBlock *bb) + : Instruction(bb->parent_->parent_->get_pointer_type(ty), + Instruction::Alloca, 0, bb, true), + alloca_ty_(ty) {} + + // 创建指令,不插入到最后,但是会设定parent + AllocaInst(Type *ty, BasicBlock *bb, bool) + : Instruction(bb->parent_->parent_->get_pointer_type(ty), + Instruction::Alloca, 0), + alloca_ty_(ty) { + this->parent_ = bb; + } + + virtual std::string print() override; + Type *alloca_ty_; +}; + +class ZextInst : public Instruction { +public: + ZextInst(OpID op, Value *val, Type *ty, BasicBlock *bb) + : Instruction(ty, op, 1, bb), dest_ty_(ty) { + set_operand(0, val); + } + virtual std::string print() override; + Type *dest_ty_; +}; + +class FpToSiInst : public Instruction { +public: + FpToSiInst(OpID op, Value *val, Type *ty, BasicBlock *bb) + : Instruction(ty, op, 1, bb), dest_ty_(ty) { + set_operand(0, val); + } + virtual std::string print() override; + Type *dest_ty_; +}; + +class SiToFpInst : public Instruction { +public: + SiToFpInst(OpID op, Value *val, Type *ty, BasicBlock *bb) + : Instruction(ty, op, 1, bb), dest_ty_(ty) { + set_operand(0, val); + } + virtual std::string print() override; + Type *dest_ty_; +}; + +//%3 = bitcast [4 x [2 x i32]]* %2 to i32* +class Bitcast : public Instruction { +public: + Bitcast(OpID op, Value *val, Type *ty, BasicBlock *bb) + : Instruction(ty, op, 1, bb), dest_ty_(ty) { + set_operand(0, val); + } + virtual std::string print() override; + Type *dest_ty_; +}; + +//%4 = phi i32 [ 1, %2 ], [ %6, %5 ] +class PhiInst : public Instruction { +public: + PhiInst(OpID op, std::vector vals, std::vector val_bbs, + Type *ty, BasicBlock *bb) + : Instruction(ty, op, 2 * vals.size()) { + for (int i = 0; i < vals.size(); i++) { + set_operand(2 * i, vals[i]); + set_operand(2 * i + 1, val_bbs[i]); + } + this->parent_ = bb; + } + static PhiInst *create_phi(Type *ty, BasicBlock *bb) { + std::vector vals; + std::vector val_bbs; + return new PhiInst(Instruction::PHI, vals, val_bbs, ty, bb); + } + void add_phi_pair_operand(Value *val, Value *pre_bb) { + this->add_operand(val); + this->add_operand(pre_bb); + } + virtual std::string print() override; + + Value *l_val_; +}; + + +class IRStmtBuilder { +public: + BasicBlock *BB_; + Module *m_; + + IRStmtBuilder(BasicBlock *bb, Module *m) : BB_(bb), m_(m){}; + ~IRStmtBuilder() = default; + Module *get_module() { return m_; } + BasicBlock *get_insert_block() { return this->BB_; } + void set_insert_point(BasicBlock *bb) { + this->BB_ = bb; + } // 在某个基本块中插入指令 + BinaryInst *create_iadd(Value *v1, Value *v2) { + return new BinaryInst(this->m_->int32_ty_, Instruction::Add, v1, v2, + this->BB_); + } // 创建加法指令(以及其他算术指令) + BinaryInst *create_isub(Value *v1, Value *v2) { + return new BinaryInst(this->m_->int32_ty_, Instruction::Sub, v1, v2, + this->BB_); + } + BinaryInst *create_imul(Value *v1, Value *v2) { + return new BinaryInst(this->m_->int32_ty_, Instruction::Mul, v1, v2, + this->BB_); + } + BinaryInst *create_isdiv(Value *v1, Value *v2) { + return new BinaryInst(this->m_->int32_ty_, Instruction::SDiv, v1, v2, + this->BB_); + } + BinaryInst *create_isrem(Value *v1, Value *v2) { + return new BinaryInst(this->m_->int32_ty_, Instruction::SRem, v1, v2, + this->BB_); + } + + ICmpInst *create_icmp_eq(Value *v1, Value *v2) { + return new ICmpInst(ICmpInst::ICMP_EQ, v1, v2, this->BB_); + } + ICmpInst *create_icmp_ne(Value *v1, Value *v2) { + return new ICmpInst(ICmpInst::ICMP_NE, v1, v2, this->BB_); + } + ICmpInst *create_icmp_gt(Value *v1, Value *v2) { + return new ICmpInst(ICmpInst::ICMP_SGT, v1, v2, this->BB_); + } + ICmpInst *create_icmp_ge(Value *v1, Value *v2) { + return new ICmpInst(ICmpInst::ICMP_SGE, v1, v2, this->BB_); + } + ICmpInst *create_icmp_lt(Value *v1, Value *v2) { + return new ICmpInst(ICmpInst::ICMP_SLT, v1, v2, this->BB_); + } + ICmpInst *create_icmp_le(Value *v1, Value *v2) { + return new ICmpInst(ICmpInst::ICMP_SLE, v1, v2, this->BB_); + } + + BinaryInst *create_fadd(Value *v1, Value *v2) { + return new BinaryInst(this->m_->float32_ty_, Instruction::FAdd, v1, v2, + this->BB_); + } + BinaryInst *create_fsub(Value *v1, Value *v2) { + return new BinaryInst(this->m_->float32_ty_, Instruction::FSub, v1, v2, + this->BB_); + } + BinaryInst *create_fmul(Value *v1, Value *v2) { + return new BinaryInst(this->m_->float32_ty_, Instruction::FMul, v1, v2, + this->BB_); + } + BinaryInst *create_fdiv(Value *v1, Value *v2) { + return new BinaryInst(this->m_->float32_ty_, Instruction::FDiv, v1, v2, + this->BB_); + } + + FCmpInst *create_fcmp_eq(Value *v1, Value *v2) { + return new FCmpInst(FCmpInst::FCMP_UEQ, v1, v2, this->BB_); + } + FCmpInst *create_fcmp_ne(Value *v1, Value *v2) { + return new FCmpInst(FCmpInst::FCMP_UNE, v1, v2, this->BB_); + } + FCmpInst *create_fcmp_gt(Value *v1, Value *v2) { + return new FCmpInst(FCmpInst::FCMP_UGT, v1, v2, this->BB_); + } + FCmpInst *create_fcmp_ge(Value *v1, Value *v2) { + return new FCmpInst(FCmpInst::FCMP_UGE, v1, v2, this->BB_); + } + FCmpInst *create_fcmp_lt(Value *v1, Value *v2) { + return new FCmpInst(FCmpInst::FCMP_ULT, v1, v2, this->BB_); + } + FCmpInst *create_fcmp_le(Value *v1, Value *v2) { + return new FCmpInst(FCmpInst::FCMP_ULE, v1, v2, this->BB_); + } + + CallInst *create_call(Value *func, std::vector args) { +#ifdef DEBUG + assert(dynamic_cast(func) && "func must be Function * type"); +#endif + return new CallInst(static_cast(func), args, this->BB_); + } + + BranchInst *create_br(BasicBlock *if_true) { + return new BranchInst(if_true, this->BB_); + } + BranchInst *create_cond_br(Value *cond, BasicBlock *if_true, + BasicBlock *if_false) { + return new BranchInst(cond, if_true, if_false, this->BB_); + } + + ReturnInst *create_ret(Value *val) { return new ReturnInst(val, this->BB_); } + ReturnInst *create_void_ret() { return new ReturnInst(this->BB_); } + + GetElementPtrInst *create_gep(Value *ptr, std::vector idxs) { + return new GetElementPtrInst(ptr, idxs, this->BB_); + } + + StoreInst *create_store(Value *val, Value *ptr) { + return new StoreInst(val, ptr, this->BB_); + } + LoadInst *create_load(Type *ty, Value *ptr) { + return new LoadInst(ptr, this->BB_); + } + LoadInst *create_load(Value *ptr) { +#ifdef DEBUG + assert(ptr->get_type()->is_pointer_type() && "ptr must be pointer type"); +#endif + + return new LoadInst(ptr, this->BB_); + } + + AllocaInst *create_alloca(Type *ty) { return new AllocaInst(ty, this->BB_); } + ZextInst *create_zext(Value *val, Type *ty) { + return new ZextInst(Instruction::ZExt, val, ty, this->BB_); + } + FpToSiInst *create_fptosi(Value *val, Type *ty) { + return new FpToSiInst(Instruction::FPtoSI, val, ty, this->BB_); + } + SiToFpInst *create_sitofp(Value *val, Type *ty) { + return new SiToFpInst(Instruction::SItoFP, val, ty, this->BB_); + } + Bitcast *create_bitcast(Value *val, Type *ty) { + return new Bitcast(Instruction::BitCast, val, ty, this->BB_); + } +}; diff --git a/compiler/src/main.cpp b/compiler/src/main.cpp new file mode 100644 index 0000000..1b6d25c --- /dev/null +++ b/compiler/src/main.cpp @@ -0,0 +1,105 @@ +#include "CombineInstr.h" +#include "ConstSpread.h" +#include "LoopInvariant.h" +#include "SimplifyJump.h" +#include "ast.h" +#include "backend.h" +#include "define.h" +#include "genIR.h" +#include "DeleteDeadCode.h" +#include "opt.h" +#include +#include +#include +#include + +extern unique_ptr root; +extern int yyparse(); +extern FILE *yyin; + +int main(int argc, char **argv) { + // Assert the number of arguments + assert(argc >= 2); + + char *filename = nullptr; + int print_ir = false; + int print_asm = false; + + std::string output = "-"; + + int opt; + bool isO2 = false; + while ((opt = getopt(argc, argv, "Sco:O::")) != -1) { + switch (opt) { + case 'S': + print_asm = true; + print_ir = false; + break; + case 'c': + print_ir = true; + print_asm = false; + break; + case 'o': + output = optarg; + break; + case 'O': + isO2 = true; + break; + default: + break; + } + } + filename = argv[optind]; + + yyin = fopen(filename, "r"); + if (yyin == nullptr) { + std::cout << "yyin open" << filename << "failed" << std::endl; + return -1; + } + + // Frontend parser + yyparse(); + + // Generate IR from AST + GenIR genIR; + root->accept(genIR); + std::unique_ptr m = genIR.getModule(); + + // Run IR optimization + if (isO2) { + std::vector Opt; + Opt.push_back(new DeadCodeDeletion(m.get())); + Opt.push_back(new ConstSpread(m.get())); + Opt.push_back(new CombineInstr(m.get())); + Opt.push_back(new DomainTree(m.get())); + Opt.push_back(new SimplifyJump(m.get())); + Opt.push_back(new LoopInvariant(m.get())); + Opt.push_back(new SimplifyJump(m.get())); + for (auto x : Opt) + x->execute(); + } + + // Open output file + std::ofstream fout; + std::ostream *out; + if (output == "-") { + out = &std::cout; + } else { + fout.open(output); + out = &fout; + } + + // Print IR result + const std::string IR = m->print(); + if (print_ir) { + *out << IR << std::endl; + } + + // Generate assembly file + if (print_asm) { + auto builder = new RiscvBuilder(); + const std::string RiscvCode = builder->buildRISCV(m.get()); + *out << RiscvCode << std::endl; + } + return 0; +} diff --git a/compiler/src/opt/BasicOperation.cpp b/compiler/src/opt/BasicOperation.cpp new file mode 100644 index 0000000..7fe0af3 --- /dev/null +++ b/compiler/src/opt/BasicOperation.cpp @@ -0,0 +1,54 @@ +#include "BasicOperation.h" + +void deleteUse(Value *opnd, Instruction *inst) { + for (auto it = opnd->use_list_.begin(); it != opnd->use_list_.end(); ++it) + if (it->val_ == inst) { + opnd->use_list_.erase(it); + return; + } +} + +void SolvePhi(BasicBlock *bb, BasicBlock *suc) { + std::vector uselessPhi; + for (auto instr : suc->instr_list_) { + if (instr->op_id_ == Instruction::PHI) { + for (int i = 1; i < instr->num_ops_; i = i + 2) + if (instr->get_operand(i) == bb) { + instr->remove_operands(i - 1, i); + break; + } + if (instr->parent_->pre_bbs_.size() == 1) { + Value *only = instr->get_operand(0); + instr->replace_all_use_with(only); + uselessPhi.push_back(instr); + } + } + } + for (auto instr : uselessPhi) + suc->delete_instr(instr); +} + +void dfsGraph(BasicBlock *bb, std::set &vis) { + if (!bb) + return; + vis.insert(bb); + for (auto suc : bb->succ_bbs_) { + if (vis.find(suc) == vis.end()) + dfsGraph(suc, vis); + } +} + +void DeleteUnusedBB(Function *func) { + std::set vis; + for (auto bb : func->basic_blocks_) + if (bb->name_ == "label_entry") { + dfsGraph(bb, vis); + break; + } + for (auto bb : func->basic_blocks_) + if (vis.find(bb) == vis.end()) { + bb->parent_->remove_bb(bb); + for (auto suc : bb->succ_bbs_) + SolvePhi(bb, suc); + } +} diff --git a/compiler/src/opt/BasicOperation.h b/compiler/src/opt/BasicOperation.h new file mode 100644 index 0000000..3ee2696 --- /dev/null +++ b/compiler/src/opt/BasicOperation.h @@ -0,0 +1,15 @@ +#ifndef BASICOPERATION +#define BASICOPERATION +#include "../ir/ir.h" +#include +#include +#include +#include +#include "opt.h" + +void deleteUse(Value* opnd,Instruction *inst); +void dfsGraph(BasicBlock *bb, std::set &vis); +void SolvePhi(BasicBlock *bb, BasicBlock *succ_bb); +void DeleteUnusedBB(Function *func); + +#endif // !BASICOPERATION \ No newline at end of file diff --git a/compiler/src/opt/CMakeLists.txt b/compiler/src/opt/CMakeLists.txt new file mode 100644 index 0000000..1091000 --- /dev/null +++ b/compiler/src/opt/CMakeLists.txt @@ -0,0 +1,6 @@ +set(SOURCE_FILES ConstSpread.cpp BasicOperation.cpp LoopInvariant.cpp CombineInstr.cpp SimplifyJump.cpp opt.cpp DeleteDeadCode.cpp) + +add_library(opt ${SOURCE_FILES}) + +target_link_libraries(opt PRIVATE ir) +target_include_directories(opt PRIVATE ${CMAKE_SOURCE_DIR}/src/ir) \ No newline at end of file diff --git a/compiler/src/opt/CombineInstr.cpp b/compiler/src/opt/CombineInstr.cpp new file mode 100644 index 0000000..cf4e276 --- /dev/null +++ b/compiler/src/opt/CombineInstr.cpp @@ -0,0 +1,95 @@ +#include "CombineInstr.h" +#include + +void CombineInstr::execute() { + for (auto foo : m->function_list_) + if (!foo->basic_blocks_.empty()) + for (BasicBlock *bb : foo->basic_blocks_) + checkBlock(bb); +} + +void CombineInstr::checkBlock(BasicBlock *bb) { + bool change = true; + while (change) { + change = false; + for (auto instr : bb->instr_list_) { + if (instr->op_id_ != Instruction::Add && + instr->op_id_ != Instruction::Sub) + continue; + if (instr->use_list_.size() != 1 || instr->use_list_.back().arg_no_ != 0) + continue; + Instruction *nextInstr = + dynamic_cast(instr->use_list_.back().val_); + if (nextInstr == nullptr || instr->op_id_ != nextInstr->op_id_ || + instr->parent_ != nextInstr->parent_) + continue; + std::unordered_map Optime; + Instruction *StartInstr = instr, *EndInstr = nullptr; + Instruction *invalidStart = nullptr, *invalidEnd = nullptr; // 无效指令 + bool isRepeat = false; + Value *dupOp = nullptr, *Candi0 = instr->get_operand(0), + *Candi1 = instr->get_operand(1); + Optime[Candi0]++; + Optime[Candi1]++; + Optime[nextInstr->get_operand(1)]++; + // 迭代过程 + instr = nextInstr; + while (instr->use_list_.size() == 1 && + instr->use_list_.back().arg_no_ == 0) { + nextInstr = dynamic_cast(instr->use_list_.back().val_); + if (nextInstr == nullptr || instr->op_id_ != nextInstr->op_id_ || + instr->parent_ != nextInstr->parent_) + break; + if (!isRepeat) { + if (Optime.find(nextInstr->get_operand(1)) != Optime.end()) + isRepeat = true; + EndInstr = instr; + invalidStart = nextInstr; + } else if (Optime.find(nextInstr->get_operand(1)) == Optime.end()) + break; + Optime[nextInstr->get_operand(1)]++; + instr = nextInstr; + } + invalidEnd = instr; + // 尝试合并:1相同,很多0;0相同,很多1都可以合并 + unsigned int dupTime = 0; + if (Optime[Candi1] == 1 && Optime[Candi0] > 1) { + dupOp = Candi0; + dupTime = Optime[Candi0]; + } else if (Optime[Candi0] == 1 && Optime[Candi1] > 1) { + dupOp = Candi1; + dupTime = Optime[Candi1]; + } else + continue; + for (auto p : Optime) { + if (p.second == 1) { + if (p.first != dupOp) { + dupTime = 0; + break; + } + } else { + if (dupTime != p.second) { + dupTime = 0; + break; + } + } + } + if (!dupTime) + continue; + ConstantInt *dupTimeConst = + new ConstantInt(instr->parent_->parent_->parent_->int32_ty_, dupTime); + Instruction *toMulInst = new BinaryInst( + bb->parent_->parent_->int32_ty_, Instruction::Mul, + static_cast(EndInstr), dupTimeConst, bb, true); + toMulInst->name_ = invalidStart->name_; + bb->add_instruction_before_inst(toMulInst, invalidStart); + invalidEnd->replace_all_use_with(toMulInst); + for (Instruction *ins = invalidStart; ins != nextInstr;) { + bb->delete_instr(ins); + ins = dynamic_cast(ins->use_list_.back().val_); + } + change = true; + break; + } + } +} \ No newline at end of file diff --git a/compiler/src/opt/CombineInstr.h b/compiler/src/opt/CombineInstr.h new file mode 100644 index 0000000..6b1dc2e --- /dev/null +++ b/compiler/src/opt/CombineInstr.h @@ -0,0 +1,13 @@ +#ifndef COMBINEINSTRH +#define COMBINEINSTRH +#include "opt.h" + +class CombineInstr : public Optimization { + +public: + CombineInstr(Module *m) : Optimization(m) {} + void execute(); + void checkBlock(BasicBlock *bb); +}; + +#endif // !COMBINEINSTRH \ No newline at end of file diff --git a/compiler/src/opt/ConstSpread.cpp b/compiler/src/opt/ConstSpread.cpp new file mode 100644 index 0000000..7aa59a2 --- /dev/null +++ b/compiler/src/opt/ConstSpread.cpp @@ -0,0 +1,354 @@ +#include "ConstSpread.h" + +ConstantInt *ConstSpread::CalcInt(Instruction::OpID op, ConstantInt *v1, + ConstantInt *v2) { + int a = v1->value_, b = v2->value_; + switch (op) { + case Instruction::Add: + return new ConstantInt(m->int32_ty_, a + b); + case Instruction::Sub: + return new ConstantInt(m->int32_ty_, a - b); + case Instruction::Mul: + return new ConstantInt(m->int32_ty_, a * b); + case Instruction::SDiv: + return new ConstantInt(m->int32_ty_, a / b); + case Instruction::SRem: + return new ConstantInt(m->int32_ty_, a % b); + case Instruction::Shl: + return new ConstantInt(m->int32_ty_, a << b); + case Instruction::LShr: + return new ConstantInt(m->int32_ty_, (unsigned)a >> b); + case Instruction::AShr: + return new ConstantInt(m->int32_ty_, a >> b); + case Instruction::And: + return new ConstantInt(m->int32_ty_, a & b); + case Instruction::Or: + return new ConstantInt(m->int32_ty_, a | b); + case Instruction::Xor: + return new ConstantInt(m->int32_ty_, a ^ b); + default: + return nullptr; + } +} + +ConstantFloat *ConstSpread::CalcFloat(Instruction::OpID op, ConstantFloat *v1, + ConstantFloat *v2) { + float a = v1->value_, b = v2->value_; + switch (op) { + case Instruction::FAdd: + return new ConstantFloat(m->float32_ty_, a + b); + case Instruction::FSub: + return new ConstantFloat(m->float32_ty_, a - b); + case Instruction::FMul: + return new ConstantFloat(m->float32_ty_, a * b); + case Instruction::FDiv: + return new ConstantFloat(m->float32_ty_, a / b); + default: + return nullptr; + } +} + +ConstantInt *ConstSpread::CalcICMP(ICmpInst::ICmpOp op, ConstantInt *v1, + ConstantInt *v2) { + int lhs = v1->value_; + int rhs = v2->value_; + switch (op) { + case ICmpInst::ICMP_EQ: + return new ConstantInt(m->int1_ty_, lhs == rhs); + case ICmpInst::ICMP_NE: + return new ConstantInt(m->int1_ty_, lhs != rhs); + case ICmpInst::ICMP_SGT: + return new ConstantInt(m->int1_ty_, lhs > rhs); + case ICmpInst::ICMP_SGE: + return new ConstantInt(m->int1_ty_, lhs >= rhs); + case ICmpInst::ICMP_SLE: + return new ConstantInt(m->int1_ty_, lhs <= rhs); + case ICmpInst::ICMP_SLT: + return new ConstantInt(m->int1_ty_, lhs < rhs); + case ICmpInst::ICMP_UGE: + return new ConstantInt(m->int1_ty_, (unsigned)lhs >= (unsigned)rhs); + case ICmpInst::ICMP_ULE: + return new ConstantInt(m->int1_ty_, (unsigned)lhs <= (unsigned)rhs); + case ICmpInst::ICMP_ULT: + return new ConstantInt(m->int1_ty_, (unsigned)lhs < (unsigned)rhs); + case ICmpInst::ICMP_UGT: + return new ConstantInt(m->int1_ty_, (unsigned)lhs > (unsigned)rhs); + default: + return nullptr; + } +} +ConstantInt *ConstSpread::CalcFCMP(FCmpInst::FCmpOp op, ConstantFloat *v1, + ConstantFloat *v2) { + float lhs = v1->value_; + float rhs = v2->value_; + switch (op) { + case FCmpInst::FCMP_UEQ: + return new ConstantInt(m->int1_ty_, lhs == rhs); + case FCmpInst::FCMP_UNE: + return new ConstantInt(m->int1_ty_, lhs != rhs); + case FCmpInst::FCMP_UGT: + return new ConstantInt(m->int1_ty_, lhs > rhs); + case FCmpInst::FCMP_UGE: + return new ConstantInt(m->int1_ty_, lhs >= rhs); + case FCmpInst::FCMP_ULE: + return new ConstantInt(m->int1_ty_, lhs <= rhs); + case FCmpInst::FCMP_ULT: + return new ConstantInt(m->int1_ty_, lhs < rhs); + case FCmpInst::FCMP_FALSE: + return new ConstantInt(m->int1_ty_, 0); + case FCmpInst::FCMP_TRUE: + return new ConstantInt(m->int1_ty_, 1); + case FCmpInst::FCMP_OEQ: + return new ConstantInt(m->int1_ty_, lhs == rhs); + case FCmpInst::FCMP_ONE: + return new ConstantInt(m->int1_ty_, lhs != rhs); + case FCmpInst::FCMP_OGE: + return new ConstantInt(m->int1_ty_, lhs >= rhs); + case FCmpInst::FCMP_OGT: + return new ConstantInt(m->int1_ty_, lhs > rhs); + case FCmpInst::FCMP_OLE: + return new ConstantInt(m->int1_ty_, lhs <= rhs); + case FCmpInst::FCMP_OLT: + return new ConstantInt(m->int1_ty_, lhs < rhs); + default: + return nullptr; + } +} + +void ConstSpread::execute() { + assert(m != nullptr); + for (Function *foo : m->function_list_) { + if (foo->basic_blocks_.size()) { + bool change = true; + while (change) { + change = false; + change |= SpreadingConst(foo); + change |= BranchProcess(foo); + DeleteUnusedBB(foo); + } + } + } +} + +bool ConstSpread::SpreadingConst(Function *func) { + uselessInstr.clear(); + for (auto bb : func->basic_blocks_) { + ConstIntMap.clear(); + ConstFloatMap.clear(); + for (auto instr : bb->instr_list_) { + ConstantInt *testConstInta = nullptr, *testConstIntb = nullptr; + ConstantFloat *testConstFloata = nullptr, *testConstFloatb = nullptr; + switch (instr->op_id_) { + case Instruction::Add: + case Instruction::Sub: + case Instruction::Mul: + case Instruction::SDiv: + case Instruction::UDiv: + case Instruction::SRem: + case Instruction::URem: + case Instruction::And: + case Instruction::Or: + case Instruction::Xor: + case Instruction::Shl: + case Instruction::AShr: + case Instruction::LShr: + testConstInta = dynamic_cast(instr->get_operand(0)); + testConstIntb = dynamic_cast(instr->get_operand(1)); + if (testConstInta && testConstIntb) { + auto intRes = + this->CalcInt(instr->op_id_, testConstInta, testConstIntb); + if (intRes) { + instr->replace_all_use_with(intRes); + uselessInstr[instr] = bb; + } + } + break; + case Instruction::ICmp: + testConstInta = dynamic_cast(instr->get_operand(0)); + testConstIntb = dynamic_cast(instr->get_operand(1)); + if (testConstInta && testConstIntb) { + auto res = this->CalcICMP(dynamic_cast(instr)->icmp_op_, + testConstInta, testConstIntb); + if (res) { + instr->replace_all_use_with(res); + uselessInstr[instr] = bb; + } + } + break; + case Instruction::FCmp: + testConstFloata = dynamic_cast(instr->get_operand(0)); + testConstFloatb = dynamic_cast(instr->get_operand(1)); + if (testConstFloata && testConstFloatb) { + auto res = this->CalcFCMP(dynamic_cast(instr)->fcmp_op_, + testConstFloata, testConstFloatb); + if (res) { + instr->replace_all_use_with(res); + uselessInstr[instr] = bb; + } + } + break; + case Instruction::FAdd: + case Instruction::FSub: + case Instruction::FMul: + case Instruction::FDiv: + testConstFloata = dynamic_cast(instr->get_operand(0)); + testConstFloatb = dynamic_cast(instr->get_operand(1)); + if (testConstFloata && testConstFloatb) { + auto floaRes = + this->CalcFloat(instr->op_id_, testConstFloata, testConstFloatb); + if (floaRes) { + instr->replace_all_use_with(floaRes); + uselessInstr[instr] = bb; + } + } + break; + case Instruction::FNeg: + testConstFloata = dynamic_cast(instr->get_operand(0)); + if (testConstFloata) { + instr->replace_all_use_with( + new ConstantFloat(m->float32_ty_, -testConstFloata->value_)); + uselessInstr[instr] = bb; + } + break; + case Instruction::FPtoSI: + testConstFloata = dynamic_cast(instr->get_operand(0)); + if (testConstFloata) { + instr->replace_all_use_with( + new ConstantInt(m->int32_ty_, testConstFloata->value_)); + uselessInstr[instr] = bb; + } + break; + case Instruction::SItoFP: + testConstInta = dynamic_cast(instr->get_operand(0)); + if (testConstInta) { + instr->replace_all_use_with( + new ConstantFloat(m->float32_ty_, testConstInta->value_)); + uselessInstr[instr] = bb; + } + break; + case Instruction::ZExt: + testConstInta = dynamic_cast(instr->get_operand(0)); + if (testConstInta) { + instr->replace_all_use_with( + new ConstantInt(m->int32_ty_, testConstInta->value_)); + uselessInstr[instr] = bb; + } + break; + case Instruction::Call: + ConstIntMap.clear(); + ConstFloatMap.clear(); + case Instruction::Load: { + auto globalVar = dynamic_cast(instr->get_operand(0)); + if (globalVar) { + auto iterInt = ConstIntMap.find(globalVar); + auto iterFloat = ConstFloatMap.find(globalVar); + if (iterInt != ConstIntMap.end()) { + instr->replace_all_use_with(iterInt->second); + uselessInstr[instr] = bb; + } else if (iterFloat != ConstFloatMap.end()) { + instr->replace_all_use_with(iterFloat->second); + uselessInstr[instr] = bb; + } + } else if (dynamic_cast(instr->get_operand(0))) { + auto pos = dynamic_cast(instr->get_operand(0)); + if (pos->alloca_ty_->tid_ == Type::IntegerTyID) { + auto iterInt = ConstIntMap.find(pos); + if (iterInt != ConstIntMap.end()) { + instr->replace_all_use_with(iterInt->second); + uselessInstr[instr] = bb; + } + } else if (pos->alloca_ty_->tid_ == Type::FloatTyID) { + auto iterFloat = ConstFloatMap.find(pos); + if (iterFloat != ConstFloatMap.end()) { + instr->replace_all_use_with(iterFloat->second); + uselessInstr[instr] = bb; + } + } + } + } break; + case Instruction::Store: { + // std::cout << "EVER STORE\n"; + auto storePos = instr->get_operand(1); + auto storeValInt = dynamic_cast(instr->get_operand(0)); + auto storeValFloat = + dynamic_cast(instr->get_operand(0)); + if (storeValInt) { + auto iter1 = ConstIntMap.find(storePos); + if (iter1 != ConstIntMap.end()) { + if (iter1->second->value_ == storeValInt->value_) + uselessInstr[instr] = bb; + else + iter1->second = storeValInt; + } else + ConstIntMap[storePos] = storeValInt; + } else if (storeValFloat) { + auto iter = ConstFloatMap.find(storePos); + if (iter != ConstFloatMap.end()) { + if (iter->second->value_ == storeValInt->value_) + uselessInstr[instr] = bb; + else + iter->second = storeValFloat; + } else + ConstFloatMap[storePos] = storeValFloat; + } else { + // 非常量存储,则该地址数据不再是常量 + auto iterInt = ConstIntMap.find(storePos); + auto iterFloat = ConstFloatMap.find(storePos); + if (iterInt != ConstIntMap.end()) + ConstIntMap.erase(iterInt); + if (iterFloat != ConstFloatMap.end()) + ConstFloatMap.erase(iterFloat); + } + } break; + default: + break; + } + } + } + if (!uselessInstr.empty()) { + for (auto [instr, bb] : uselessInstr) + bb->delete_instr(instr); + return true; + } + return false; +} + +bool ConstSpread::BranchProcess(Function *func) { + bool change = false; + for (auto bb : func->basic_blocks_) { + auto br = bb->get_terminator(); + if (!br) + continue; + if (br->op_id_ == Instruction::Br && + dynamic_cast(br)->num_ops_ == 3) { + auto cond = dynamic_cast(br->get_operand(0)); + auto truebb = br->get_operand(1); + auto falsebb = br->get_operand(2); + if (!cond) + continue; + change = true; + if (cond->value_ == 0) { + bb->delete_instr(br); + for (auto succ_bb : bb->succ_bbs_) { + succ_bb->remove_pre_basic_block(bb); + if (succ_bb != falsebb) { + SolvePhi(bb, succ_bb); + } + } + bb->succ_bbs_.clear(); + new BranchInst(dynamic_cast(falsebb), bb); + } else { + bb->delete_instr(br); + for (auto succ_bb : bb->succ_bbs_) { + succ_bb->remove_pre_basic_block(bb); + if (succ_bb != truebb) { + SolvePhi(bb, succ_bb); + } + } + bb->succ_bbs_.clear(); + new BranchInst(dynamic_cast(truebb), bb); + } + } + } + return change; +} \ No newline at end of file diff --git a/compiler/src/opt/ConstSpread.h b/compiler/src/opt/ConstSpread.h new file mode 100644 index 0000000..c3ff9ff --- /dev/null +++ b/compiler/src/opt/ConstSpread.h @@ -0,0 +1,23 @@ +#ifndef CONSTSPREAD +#define CONSTSPREAD +#include "../ir/ir.h" +#include "BasicOperation.h" +#include "opt.h" + +class ConstSpread : public Optimization { +public: + ConstSpread(Module *m_) : Optimization(m_) {} + void execute(); + ConstantInt *CalcInt(Instruction::OpID op, ConstantInt *v1, ConstantInt *v2); + ConstantFloat *CalcFloat(Instruction::OpID op, ConstantFloat *v1, + ConstantFloat *v2); + ConstantInt *CalcICMP(ICmpInst::ICmpOp op, ConstantInt *v1, ConstantInt *v2); + ConstantInt *CalcFCMP(FCmpInst::FCmpOp op, ConstantFloat *v1, ConstantFloat *v2); + bool SpreadingConst(Function *func); + bool BranchProcess(Function *func); + std::map ConstIntMap; + std::map ConstFloatMap; + std::map uselessInstr; +}; + +#endif // !CONSTSPREAD \ No newline at end of file diff --git a/compiler/src/opt/DeleteDeadCode.cpp b/compiler/src/opt/DeleteDeadCode.cpp new file mode 100644 index 0000000..71c4b99 --- /dev/null +++ b/compiler/src/opt/DeleteDeadCode.cpp @@ -0,0 +1,180 @@ +#include "DeleteDeadCode.h" +#include "ConstSpread.h" + +std::set OptFunc = {"getint", "getfloat", + "getch", "getarray", + "getfarray", "putint", + "putfloat", "putch", + "putarray", "putfarray", + "_sysy_starttime", "_sysy_stoptime", + "memcpy", "memclr", + "memset", "llvm.memset.p0.i32", + "__aeabi_memcpy4", "__aeabi_memclr4", + "__aeabi_memset4"}; + +void DeadCodeDeletion::initFuncPtrArg() { + for (auto foo : m->function_list_) { + if (foo->basic_blocks_.empty()) + continue; + for (auto arg : foo->arguments_) + if (arg->type_->tid_ == Type::PointerTyID) { + if (!funcPtrArgs.count(foo)) + funcPtrArgs[foo] = {}; + funcPtrArgs[foo].insert(arg); + } + } +} +void DeadCodeDeletion::Init(Function *foo) { + storePos.clear(); + for (auto bb : foo->basic_blocks_) { + for (auto ins : bb->instr_list_) { + if (ins->op_id_ == Instruction::GetElementPtr) { + } else if (ins->op_id_ == Instruction::Store) { + if (!storePos.count(ins->get_operand(1))) { + storePos.insert({ins->get_operand(1), {}}); + } + storePos[ins->get_operand(1)].push_back(ins); + if (dynamic_cast(ins->get_operand(1))) + OptFunc.insert(foo->name_); + if (dynamic_cast(ins->get_operand(1))) + OptFunc.insert(foo->name_); + if (funcPtrArgs[foo].count(ins->get_operand(1))) + OptFunc.insert(foo->name_); + } else if (ins->op_id_ == Instruction::Call) { + auto f = ins->get_operand(ins->operands_.size() - 1); + if (OptFunc.count(f->name_)) + OptFunc.insert(foo->name_); + } + } + } +} +bool DeadCodeDeletion::checkOpt(Function *foo, Instruction *ins) { + if (ins->op_id_ == Instruction::Ret) { + exitBlock = ins->parent_; + return true; + } else if (ins->op_id_ == Instruction::Call) { + auto f = ins->get_operand(ins->operands_.size() - 1); + return OptFunc.count(f->name_); + } else if (ins->op_id_ == Instruction::Store) { + if (dynamic_cast(ins->get_operand(1))) + return true; + if (dynamic_cast(ins->get_operand(1))) + return true; + if (funcPtrArgs[foo].count(ins->get_operand(1))) + return true; + return false; + } + return false; +} + +void DeadCodeDeletion::findInstr(Function *foo) { + std::list workList; + for (auto bb : foo->basic_blocks_) { + for (auto ins : bb->instr_list_) { + if (checkOpt(foo, ins)) { + uselessInstr.insert(ins); + workList.push_back(ins); + } + } + } + while (!workList.empty()) { + auto ins = dynamic_cast(workList.back()); + workList.pop_back(); + if (ins == nullptr) { + continue; + } + for (auto operand : ins->operands_) { + auto temp = dynamic_cast(operand); + if (!temp) + continue; + if (uselessInstr.insert(temp).second) { + workList.push_back(temp); + } + } + if (ins->op_id_ == Instruction::PHI) { + for (int i = 1; i < ins->operands_.size(); i += 2) { + auto bb = dynamic_cast(ins->get_operand(i)); + auto br = bb->get_terminator(); + if (uselessInstr.insert(br).second) { + workList.push_back(br); + } + } + } + if (storePos.count(ins)) { + for (auto curInstr : storePos[ins]) { + if (uselessInstr.insert(dynamic_cast(curInstr)).second) { + workList.push_back(curInstr); + } + } + storePos.erase(ins); + } + if (uselessBlock.insert(ins->parent_).second) { + for (auto RFrontier : ins->parent_->rdom_frontier_) { + auto t = RFrontier->get_terminator(); + if (uselessInstr.insert(t).second) { + workList.push_back(t); + } + } + } + } +} +void DeadCodeDeletion::deleteInstr(Function *foo) { + int deleteCnt = 0, changeCnt = 0; + for (auto bb : foo->basic_blocks_) { + std::vector ins2Del; + for (auto ins : bb->instr_list_) { + if (!uselessInstr.count(ins)) { + if (ins->op_id_ != Instruction::Br) { + ins2Del.push_back(ins); + } else { + if (ins->operands_.size() == 3) { + changeCnt++; + auto trueBB = dynamic_cast(ins->get_operand(1)); + auto falseBB = dynamic_cast(ins->get_operand(2)); + trueBB->remove_pre_basic_block(bb); + falseBB->remove_pre_basic_block(bb); + bb->remove_succ_basic_block(trueBB); + bb->remove_succ_basic_block(falseBB); + BasicBlock *temp = exitBlock; + std::vector rdoms(bb->rdoms_.begin(), + bb->rdoms_.end()); + std::sort(rdoms.begin(), rdoms.end(), + [=](BasicBlock *x, BasicBlock *y) -> bool { + return x->rdoms_.count(y); + }); + for (auto rdbb : rdoms) { + if (rdbb != bb && uselessBlock.count(rdbb)) { + temp = rdbb; + break; + } + } + ins->remove_operands(0, 2); + ins->num_ops_ = 1; + ins->operands_.resize(1); + ins->use_pos_.resize(1); + ins->set_operand(0, temp); + bb->add_succ_basic_block(temp); + temp->add_pre_basic_block(bb); + } + } + } + } + deleteCnt += ins2Del.size(); + for (auto ins : ins2Del) { + bb->delete_instr(ins); + } + } +} + +void DeadCodeDeletion::execute() { + ReverseDomainTree reverseDomainTree(m); + reverseDomainTree.execute(); + initFuncPtrArg(); + for (auto foo : m->function_list_) + if (!foo->basic_blocks_.empty()) { + Init(foo); + findInstr(foo); + deleteInstr(foo); + DeleteUnusedBB(foo); + } +} diff --git a/compiler/src/opt/DeleteDeadCode.h b/compiler/src/opt/DeleteDeadCode.h new file mode 100644 index 0000000..fe0e1fb --- /dev/null +++ b/compiler/src/opt/DeleteDeadCode.h @@ -0,0 +1,25 @@ +#ifndef DELETEDEADCODEH +#define DELETEDEADCODEH + +#include "opt.h" + +extern std::set sysLibFunc; + +class DeadCodeDeletion : public Optimization { + std::map> funcPtrArgs; + std::map> storePos; + BasicBlock *exitBlock; + std::set uselessInstr; + std::set uselessBlock; + +public: + DeadCodeDeletion(Module *m) : Optimization(m), exitBlock(nullptr) {} + void execute(); + void initFuncPtrArg(); + void Init(Function *foo); + bool checkOpt(Function *foo, Instruction *instr); + void findInstr(Function *foo); + void deleteInstr(Function *foo); +}; + +#endif // !DELETEDEADCODEH \ No newline at end of file diff --git a/compiler/src/opt/LoopInvariant.cpp b/compiler/src/opt/LoopInvariant.cpp new file mode 100644 index 0000000..5230177 --- /dev/null +++ b/compiler/src/opt/LoopInvariant.cpp @@ -0,0 +1,149 @@ +#include "LoopInvariant.h" + +void LoopInvariant::execute() { + searchLoop(); + while (!loopStack.empty()) { + auto loop = loopStack.top(); + loopStack.pop(); + std::set assignVals; + std::set visInstr; // 标记下这条语句是不是被操作过 + std::vector invarInstrs; // 存在不变量的语句集合 + std::map instrPos; + for (auto bb : *loop) + for (Instruction *inst : bb->instr_list_) + // 赋值语句做操作 + if (inst->is_binary() || inst->is_cmp() || inst->is_fcmp() || + inst->is_call() || inst->is_phi() || inst->is_zext() || + inst->is_fptosi() || inst->is_sitofp() || inst->is_gep() || + inst->is_load()) + assignVals.insert(inst); + bool changed = true; + while (changed) { + changed = false; + for (auto bb : *loop) { + for (auto instr : bb->instr_list_) { + if (visInstr.find(instr) != visInstr.end()) + continue; + if (!instr->is_gep() && !instr->is_alloca() && !instr->is_br() && + !instr->is_ret() && !instr->is_phi() && !instr->is_store() && + !instr->is_load() && + !(instr->is_call() && + instr->get_operand(instr->num_ops_ - 1)->print() != "rand")) { + bool move = true; + // 一个操作数不是不变量就不能动 + for (unsigned int i = 0; i < instr->num_ops_; i++) + if (assignVals.find(instr->get_operand(i)) != assignVals.end()) + move = false; + if (move) { + instrPos[instr] = bb; + invarInstrs.push_back(instr); + assignVals.erase(instr); + visInstr.insert(instr); + changed = true; + } + } + } + } + } + auto enter = entryPos[loop]; + for (auto prev : enter->pre_bbs_) + if (loop->find(prev) == loop->end()) + for (auto inst : invarInstrs) + prev->add_instruction_before_terminator(inst); + } +} + +void LoopInvariant::searchLoop() { + for (auto foo : m->function_list_) { + if (foo->basic_blocks_.empty()) + continue; + std::set nodes; + std::set entry; + std::set *> SCCs; + std::map nodeMap; + for (auto bb : foo->basic_blocks_) { + auto cur = new node(bb, -1, -1, 0); + nodeMap[bb] = cur; + nodes.insert(cur); + } + for (auto bb : foo->basic_blocks_) { + auto BlockNode = nodeMap[bb]; + for (auto suc : bb->succ_bbs_) + BlockNode->suc.insert(nodeMap[suc]); + for (auto pre : bb->succ_bbs_) + BlockNode->pre.insert(nodeMap[pre]); + } + + while (LoopInvariant::searchSCC(nodes, SCCs)) { + for (auto SCC : SCCs) { + node *enter = nullptr; + for (auto curBlock : *SCC) + for (auto pre : curBlock->pre) + if (SCC->find(pre) == SCC->end()) + enter = curBlock; + else if (entry.find(pre) != entry.end()) + enter = pre; + + auto curLoop = new std::set; + for (auto curBlock : *SCC) + curLoop->insert(curBlock->bb); + entryPos[curLoop] = enter->bb; + loopStack.push(curLoop); + entry.insert(enter); + nodes.erase(enter); + for (auto pre : enter->pre) + pre->suc.erase(enter); + for (auto suc : enter->suc) + suc->pre.erase(enter); + } + for (auto SCC : SCCs) + SCC->clear(); + SCCs.clear(); + for (auto NodeBlock : nodes) + NodeBlock = new node(nullptr, -1, -1, false); + } + for (auto node : nodes) + delete node; + } +} + +bool LoopInvariant::searchSCC(std::set &basicBlocks, + std::set *> &SCCs) { + ind = 0; + while (!tarjanStack.empty()) + tarjanStack.pop(); + for (auto cur : basicBlocks) + if (cur->dfn == -1) + tarjan(cur, SCCs); + return SCCs.size() != 0; +} + +void LoopInvariant::tarjan(node *cur, std::set *> &SCCs) { + cur->dfn = cur->low = ++ind; + cur->inStack = true; + tarjanStack.push(cur); + for (auto succ : cur->suc) + if (succ->dfn == -1) { + tarjan(succ, SCCs); + if (succ->low < cur->low) + cur->low = succ->low; + } else if (succ->inStack && succ->low < cur->low) + cur->low = succ->low; + // 找到low=dfn的,出现强连通分量 + if (cur->dfn == cur->low) { + if (cur == tarjanStack.top()) { + tarjanStack.pop(); + cur->inStack = false; + return; + } + auto SCC = new std::set; + node *tp = nullptr; + do { + tp = tarjanStack.top(); + SCC->insert(tp); + tarjanStack.pop(); + tp->inStack = false; + } while (tp != cur); + SCCs.insert(SCC); + } +} \ No newline at end of file diff --git a/compiler/src/opt/LoopInvariant.h b/compiler/src/opt/LoopInvariant.h new file mode 100644 index 0000000..dec8635 --- /dev/null +++ b/compiler/src/opt/LoopInvariant.h @@ -0,0 +1,31 @@ +#ifndef LOOPH +#define LOOPH + +#include "BasicOperation.h" + +struct node { + BasicBlock *bb; + std::set pre; + std::set suc; + int dfn, low; + bool inStack; + node() = default; + node(BasicBlock *bb_, int dfn_, int low_, bool inStack_) + : bb(bb_), dfn(dfn_), low(low_), inStack(inStack_) {} +}; + +class LoopInvariant : public Optimization { + int ind; + std::stack tarjanStack; + std::stack *> loopStack; + std::map *, BasicBlock *> entryPos; + +public: + LoopInvariant(Module *m) : Optimization(m) {} + void execute(); + void searchLoop(); + bool searchSCC(std::set &basicBlock, std::set *> &SCCs); + void tarjan(node *pos, std::set *> &SCCs); +}; + +#endif // !LOOPH \ No newline at end of file diff --git a/compiler/src/opt/SimplifyJump.cpp b/compiler/src/opt/SimplifyJump.cpp new file mode 100644 index 0000000..7fee0ba --- /dev/null +++ b/compiler/src/opt/SimplifyJump.cpp @@ -0,0 +1,120 @@ +#include "SimplifyJump.h" + +void SimplifyJump::execute() { + for (auto foo : m->function_list_) + if (foo->basic_blocks_.empty()) { + deleteUnReachableBlock(foo); + mergePreBlock(foo); + deleteUselessPhi(foo); + deleteUselessJump(foo); + } +} + +void SimplifyJump::deleteUselessBlock(Function *foo, + std::vector &uselessBlock) { + for (auto bb : uselessBlock) + foo->remove_bb(bb); +} + +bool SimplifyJump::checkUselessJump(BasicBlock *bb) { + auto JumpPos = bb->get_terminator()->get_operand(0); + for (auto preBB : bb->pre_bbs_) { + auto br = preBB->get_terminator(); + if (br->operands_.size() == 1) + continue; + auto trueBB = br->get_operand(1); + auto falseBB = br->get_operand(2); + if (trueBB == JumpPos || falseBB == JumpPos) + return false; + } + return true; +} + +void SimplifyJump::deleteUnReachableBlock(Function *foo) { + std::vector uselessBlock; + for (int i = 2; i < foo->basic_blocks_.size(); i++) { + auto curbb = foo->basic_blocks_[i]; + if (curbb->pre_bbs_.empty()) { + uselessBlock.push_back(curbb); + // 发现无用块后需要提前进行phi合流处理 + for (auto use : curbb->use_list_) { + auto instr = dynamic_cast(use.val_); + if (instr != nullptr) + instr->remove_operands(use.arg_no_ - 1, use.arg_no_); + } + } + } +} + +void SimplifyJump::mergePreBlock(Function *foo) { + std::vector uselessBlock; + for (int i = 2; i < foo->basic_blocks_.size(); i++) { + auto bb = foo->basic_blocks_[i]; + if (bb->pre_bbs_.size() == 1) { + auto preBlock = *bb->pre_bbs_.begin(); + auto preBr = preBlock->get_terminator(); + if (preBlock->succ_bbs_.size() != 1) + continue; + preBlock->delete_instr(preBr); + for (auto instr : bb->instr_list_) { + preBlock->add_instruction(instr); + bb->remove_instr(instr); + } + preBlock->remove_succ_basic_block(bb); + for (auto suc : bb->succ_bbs_) { + preBlock->add_succ_basic_block(suc); + suc->remove_pre_basic_block(bb); + suc->add_pre_basic_block(preBlock); + } + bb->replace_all_use_with(preBlock); + uselessBlock.push_back(bb); + } + } + deleteUselessBlock(foo, uselessBlock); +} + +void SimplifyJump::deleteUselessPhi(Function *foo) { + for (auto bb : foo->basic_blocks_) + if (bb->pre_bbs_.size() == 1) + for (auto instr : bb->instr_list_) + if (instr->is_phi()) { + instr->replace_all_use_with(instr->get_operand(0)); + bb->delete_instr(instr); + } +} + +void SimplifyJump::deleteUselessJump(Function *foo) { + std::vector uselessBlock; + for (int i = 2; i < foo->basic_blocks_.size(); i++) { + BasicBlock *curbb = foo->basic_blocks_[i]; + if (curbb->instr_list_.size() != 1) + continue; + auto branchInstr = curbb->get_terminator(); + if (branchInstr->operands_.size() != 1 || branchInstr->is_ret()) + continue; + if (!checkUselessJump(curbb)) + continue; + uselessBlock.push_back(curbb); + auto JumpTarget = dynamic_cast(branchInstr->get_operand(0)); + for (auto instr : JumpTarget->instr_list_) + if (instr->is_phi()) { + for (int i = 1; i < instr->operands_.size(); i += 2) { + if (instr->get_operand(i) == curbb) { + auto val = instr->get_operand(i - 1); + instr->remove_operands(i - 1, i); + for (auto preBB : curbb->pre_bbs_) { + instr->add_operand(val); + instr->add_operand(preBB); + } + break; + } + } + } + curbb->replace_all_use_with(JumpTarget); + for (auto preBB : curbb->pre_bbs_) { + preBB->add_succ_basic_block(JumpTarget); + JumpTarget->add_pre_basic_block(preBB); + } + } + deleteUselessBlock(foo, uselessBlock); +} \ No newline at end of file diff --git a/compiler/src/opt/SimplifyJump.h b/compiler/src/opt/SimplifyJump.h new file mode 100644 index 0000000..a477ce5 --- /dev/null +++ b/compiler/src/opt/SimplifyJump.h @@ -0,0 +1,19 @@ +#ifndef SIMPLIFYJUMPH +#define SIMPLIFYJUMPH +#include "opt.h" + +class SimplifyJump : public Optimization { + +public: + SimplifyJump(Module *m) : Optimization(m) {} + void execute(); + void deleteUselessBlock(Function *foo, + std::vector &uselessBlock); + bool checkUselessJump(BasicBlock *bb); + void deleteUselessPhi(Function *foo); + void deleteUselessJump(Function *foo); + void mergePreBlock(Function *foo); + void deleteUnReachableBlock(Function *foo); +}; + +#endif // !SIMPLIFYJUMPH \ No newline at end of file diff --git a/compiler/src/opt/opt.cpp b/compiler/src/opt/opt.cpp new file mode 100644 index 0000000..96f5880 --- /dev/null +++ b/compiler/src/opt/opt.cpp @@ -0,0 +1,206 @@ +#include "opt.h" +#include +#include + +void DomainTree::execute() { + for (auto foo : m->function_list_) + if (!foo->basic_blocks_.empty()) { + getBlockDom(foo); + getBlockDomFront(foo); + } +} + +bool DomainTree::isLoopEdge(BasicBlock *a, BasicBlock *b) { + return TraverseInd[a] > TraverseInd[b]; +} + +std::vector DomainTree::postTraverse(BasicBlock *bb) { + std::set vis; + std::vector ans; + std::function dfs = [&](BasicBlock *place) { + vis.insert(place); + for (auto child : place->succ_bbs_) + if (vis.find(child) == vis.end()) + dfs(child); + ans.push_back(place); + }; + dfs(bb); + return ans; +} + +void DomainTree::getReversePostTraverse(Function *f) { + doms.clear(); + reversePostTraverse.clear(); + TraverseInd.clear(); + auto entryBlock = *f->basic_blocks_.begin(); + auto seq = postTraverse(entryBlock); + std::reverse(reversePostTraverse.begin(), reversePostTraverse.end()); + for (int i = 0; i < seq.size(); i++) + TraverseInd[seq[i]] = i; + reversePostTraverse = seq; +} + +void DomainTree::getBlockDom(Function *f) { + getReversePostTraverse(f); + auto root = *f->basic_blocks_.begin(); + auto root_id = TraverseInd[root]; + doms.resize(root_id + 1, nullptr); + doms.back() = root; + bool change = true; + while (change) { + change = false; + for (auto bb : reversePostTraverse) + if (bb != root) { + auto preds = bb->pre_bbs_; + BasicBlock *curDom = nullptr; + for (auto pred_bb : preds) + if (doms[TraverseInd[pred_bb]] != nullptr) { + curDom = pred_bb; + break; + } + for (auto pred_bb : preds) + if (doms[TraverseInd[pred_bb]] != nullptr) + curDom = intersect(pred_bb, curDom); + if (doms[TraverseInd[bb]] != curDom) { + doms[TraverseInd[bb]] = curDom; + change = true; + } + } + } + for (auto bb : reversePostTraverse) + bb->idom_ = doms[TraverseInd[bb]]; +} + +void DomainTree::getBlockDomFront(Function *foo) { + for (auto b : foo->basic_blocks_) { + auto b_pred = b->pre_bbs_; + if (b_pred.size() >= 2) { + for (auto pred : b_pred) { + auto runner = pred; + while (runner != doms[TraverseInd[b]]) { + runner->dom_frontier_.insert(b); + runner = doms[TraverseInd[runner]]; + } + } + } + } +} + +BasicBlock *DomainTree::intersect(BasicBlock *b1, BasicBlock *b2) { + auto head1 = b1; + auto head2 = b2; + while (head1 != head2) { + while (TraverseInd[head1] < TraverseInd[head2]) + head1 = doms[TraverseInd[head1]]; + while (TraverseInd[head2] < TraverseInd[head1]) + head2 = doms[TraverseInd[head2]]; + } + return head1; +} + +void ReverseDomainTree::execute() { + for (auto f : m->function_list_) + if (!f->basic_blocks_.empty()) { + for (auto bb : f->basic_blocks_) { + bb->rdoms_.clear(); + bb->rdom_frontier_.clear(); + } + getBlockDomR(f); + getBlockDomFrontR(f); + getBlockRdoms(f); + } +} + +void ReverseDomainTree::getPostTraverse(BasicBlock *bb, + std::set &visited) { + visited.insert(bb); + for (auto parent : bb->pre_bbs_) + if (visited.find(parent) == visited.end()) + getPostTraverse(parent, visited); + reverseTraverseInd[bb] = reverseTraverse.size(); + reverseTraverse.push_back(bb); +} + +void ReverseDomainTree::getReversePostTraverse(Function *f) { + reverseDomainBlock.clear(); + reverseTraverse.clear(); + reverseTraverseInd.clear(); + for (auto bb : f->basic_blocks_) { + auto terminate_instr = bb->get_terminator(); + if (terminate_instr->op_id_ == Instruction::Ret) { + exitBlock = bb; + break; + } + } + assert(exitBlock != nullptr); + std::set visited = {}; + getPostTraverse(exitBlock, visited); + reverse(reverseTraverse.begin(), reverseTraverse.end()); +} + +void ReverseDomainTree::getBlockDomR(Function *f) { + getReversePostTraverse(f); + auto root = exitBlock; + auto root_id = reverseTraverseInd[root]; + for (int i = 0; i < root_id; i++) + reverseDomainBlock.push_back(nullptr); + reverseDomainBlock.push_back(root); + bool change = true; + while (change) { + change = false; + for (auto bb : reverseTraverse) { + if (bb != root) { + BasicBlock *new_irdom = nullptr; + for (auto rpred_bb : bb->succ_bbs_) + if (reverseDomainBlock[reverseTraverseInd[rpred_bb]] != nullptr) { + new_irdom = rpred_bb; + break; + } + for (auto rpred_bb : bb->succ_bbs_) + if (reverseDomainBlock[reverseTraverseInd[rpred_bb]] != nullptr) + new_irdom = intersect(rpred_bb, new_irdom); + if (reverseDomainBlock[reverseTraverseInd[bb]] != new_irdom) { + reverseDomainBlock[reverseTraverseInd[bb]] = new_irdom; + change = true; + } + } + } + } +} +void ReverseDomainTree::getBlockRdoms(Function *f) { + for (auto bb : f->basic_blocks_) { + if (bb == exitBlock) + continue; + auto current = bb; + while (current != exitBlock) { + bb->rdoms_.insert(current); + current = reverseDomainBlock[reverseTraverseInd[current]]; + } + } +} +void ReverseDomainTree::getBlockDomFrontR(Function *f) { + for (auto bb_iter = f->basic_blocks_.rbegin(); + bb_iter != f->basic_blocks_.rend(); bb_iter++) { + auto bb = *bb_iter; + if (bb->succ_bbs_.size() >= 2) { + for (auto rpred : bb->succ_bbs_) { + auto runner = rpred; + while (runner != reverseDomainBlock[reverseTraverseInd[bb]]) { + runner->rdom_frontier_.insert(bb); + runner = reverseDomainBlock[reverseTraverseInd[runner]]; + } + } + } + } +} +BasicBlock *ReverseDomainTree::intersect(BasicBlock *b1, BasicBlock *b2) { + auto head1 = b1; + auto head2 = b2; + while (head1 != head2) { + while (reverseTraverseInd[head1] < reverseTraverseInd[head2]) + head1 = reverseDomainBlock[reverseTraverseInd[head1]]; + while (reverseTraverseInd[head2] < reverseTraverseInd[head1]) + head2 = reverseDomainBlock[reverseTraverseInd[head2]]; + } + return head1; +} diff --git a/compiler/src/opt/opt.h b/compiler/src/opt/opt.h new file mode 100644 index 0000000..b43b4bb --- /dev/null +++ b/compiler/src/opt/opt.h @@ -0,0 +1,46 @@ +// Currently a dummy file +#ifndef OPTH +#define OPTH + +#include "ir.h" + +class Optimization { +public: + Module *m; + explicit Optimization(Module *m_) : m(m_) {} + virtual void execute() = 0; +}; + +class DomainTree : public Optimization { + std::vector reversePostTraverse; + std::map TraverseInd; + std::vector doms; + +public: + DomainTree(Module *m) : Optimization(m) {} + void execute(); + void getReversePostTraverse(Function *foo); + std::vector postTraverse(BasicBlock *bb); + void getBlockDom(Function *foo); + void getBlockDomFront(Function *foo); + BasicBlock *intersect(BasicBlock *b1, BasicBlock *b2); + bool isLoopEdge(BasicBlock *a, BasicBlock *b); +}; + +class ReverseDomainTree : public Optimization { + std::map reverseTraverseInd; + std::vector reverseDomainBlock; + std::vector reverseTraverse; + BasicBlock *exitBlock; + +public: + ReverseDomainTree(Module *m) : Optimization(m), exitBlock(nullptr) {} + void execute(); + BasicBlock *intersect(BasicBlock *b1, BasicBlock *b2); + void getReversePostTraverse(Function *foo); + void getBlockDomR(Function *foo); + void getBlockRdoms(Function *foo); + void getBlockDomFrontR(Function *foo); + void getPostTraverse(BasicBlock *bb, std::set &visited); +}; +#endif // !OPTH \ No newline at end of file diff --git a/compiler/src/parser/CMakeLists.txt b/compiler/src/parser/CMakeLists.txt new file mode 100644 index 0000000..8363290 --- /dev/null +++ b/compiler/src/parser/CMakeLists.txt @@ -0,0 +1,22 @@ +# Require Flex & Bison as generator +# find_package(FLEX 2.5.4 REQUIRED) +# find_package(BISON 2.4.1 REQUIRED) + +set(PARSER_DIR "${CMAKE_CURRENT_SOURCE_DIR}") +# set(PARSER_DIR "${CMAKE_CURRENT_BINARY_DIR}") + +set(LEXER_CPP "${PARSER_DIR}/lexer.cpp") +set(LEXER_DEF "${PARSER_DIR}/lexer.hpp") +set(PARSER_CPP "${PARSER_DIR}/parser.cpp") +set(PARSER_DEF "${PARSER_DIR}/parser.hpp") + +# Generate tokenizer & parser via Flex & Bison +# flex_target(LEXER "lexer.l" "${LEXER_CPP}" DEFINES_FILE "${LEXER_DEF}") +# bison_target(PARSER "parser.y" "${PARSER_CPP}" DEFINES_FILE "${PARSER_DEF}") +# add_flex_bison_dependency(LEXER PARSER) + +set(SOURCE_FILES "${LEXER_CPP}" "${PARSER_CPP}" "ast.cpp") +set(PARSER_INCLUDE ${PARSER_DIR} ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE) + +add_library(parser STATIC ${SOURCE_FILES}) +target_include_directories(parser PRIVATE "${PARSER_INCLUDE}") diff --git a/compiler/src/parser/Makefile b/compiler/src/parser/Makefile new file mode 100644 index 0000000..a78efa3 --- /dev/null +++ b/compiler/src/parser/Makefile @@ -0,0 +1,10 @@ +objs=parser.o lexer.o ast.o display.o +CC=gcc + +parser:$(objs) + $(CC) -o parser ${objs} +%.o:%.c + $(CC) -c $< +.PHONY: clean +clean: + rm parser $(objs) parser.c lexer.c parser.h -f diff --git a/compiler/src/parser/ast.c b/compiler/src/parser/ast.c new file mode 100644 index 0000000..33b6aa1 --- /dev/null +++ b/compiler/src/parser/ast.c @@ -0,0 +1,716 @@ +#include "ast.h" +#include "parser.h" //由bison根据parser.y生成 +#define DEBUG 1 +#define INTEGER_MINVALUE -2147483647 +#define ARRAYCALL 123456789 +int LEV=0; +int main_flag = 0; +int call_flag = 0; +int main_call = 0; +int current_offset = 0; +char break_label[30]; +char continue_label[30]; +char case_temp[30]; +char case_label[30]; +char array_name[30]; +char struct_name[33]; +int struct_width = 0; +int struct_flag = 0; +int array_index = 0; +int struct_var_flag = 0; +int rtn, flag = 0; +int rtn2, op; +int return_flag = 0; +struct ASTNode* left; +struct ASTNode* right; +char tokens[200][200]; //用来存储TAC语句信息 +const char spState[8][20] = { "FUNCTION", "GOTO", "CALL", "PARAM", "LABEL", "ARG", "RETURN","BLOCK" }; + +int varlen = 0; //记录当前变量长度 +int lineNo = 0; //记录行号 +ASTNode *new_node(node_type type, ASTNode *left, ASTNode *mid, ASTNode *right, int int_val, float float_val, char *symbol, node_type d_type) { + ASTNode *n = (ASTNode *)malloc(sizeof(ASTNode)); + n->type = type; + n->left = left; + n->mid = mid; + n->right = right; + n->int_val = int_val; + n->float_val = float_val; + n->symbol = symbol; + n->d_type = d_type; + return n; +} + +int targetNum[1024]; +int currentNum[1024]; +int currentLayer = 0; +int handle_next_display = 1; + +int getBranchNum(ASTNode* T) +{ + int count = 0; + if(T->left) + { + count++; + } + if(T->right) + { + count++; + } + if(T->mid) + { + count++; + } + return count; +} + +void printVarType(node_type type) +{ + switch(type) + { + case Int: + printf("int"); + break; + case Float: + printf("float"); + break; + case Void: + printf("void"); + break; + default: + printf("unknown"); + break; + } +} + +void gapProcess() +{ + for(int i=1;i<=currentLayer;i++) + { + if(i < currentLayer) + { + if(currentNum[i] <= targetNum[i]) + { + printf("| "); + } + else + { + printf(" "); + } + } + else + { + if(currentNum[i] < targetNum[i]) + { + printf("|--"); + } + else + { + printf("`--"); + } + } + } +} + +void gapManage(ASTNode* T, int reverse) +{ + currentNum[currentLayer]++; + currentLayer++; + targetNum[currentLayer] = getBranchNum(T); + currentNum[currentLayer] = 1; + if(reverse) + { + nextDisplayReverse(T); + } + else + { + nextDisplay(T); + } + currentLayer--; +} + +void sameGapManage(ASTNode* T, int reverse) +{ + currentNum[currentLayer]++; + currentLayer++; + targetNum[currentLayer] = getBranchNum(T) - 1; + currentNum[currentLayer] = 1; + if(T->right) + { + display(T->right); + } + currentLayer--; + display(T->left); +} + +void nextDisplay(ASTNode* T) +{ + if(T->left) + { + display(T->left); + } + if(T->mid) + { + display(T->mid); + } + if(T->right) + { + display(T->right); + } +} + +void nextDisplayReverse(ASTNode* T) +{ + if(T->right) + { + display(T->right); + } + if(T->mid) + { + display(T->mid); + } + if(T->left) + { + display(T->left); + } +} + +void print_root(ASTNode* T) +{ + gapProcess(); + printf("CompUnit\n"); + gapManage(T, 1); +} + +void print_comp_unit(ASTNode* T) +{ + if(T->left) + { + currentNum[currentLayer]--; + } + handle_next_display = 0; + switch(T->right->type) + { + case ConstDecl: + print_const_decl(T->right); + break; + case VarDecl: + print_var_decl(T->right); + break; + case FuncDef: + print_func_def(T->right); + break; + } + if(T->left) + { + currentLayer++; + targetNum[currentLayer] = getBranchNum(T) - 1; + currentNum[currentLayer] = 1; + currentLayer--; + display(T->left); + } +} + +void print_const_decl(ASTNode* T) +{ + gapProcess(); + printf("ConstDecl "); + printVarType(T->d_type); + printf("\n"); + gapManage(T, 1); +} + +void print_const_def(ASTNode* T) +{ + if(T->left) + { + currentNum[currentLayer]--; + } + gapProcess(); + printf("ConstDef %s\n", T->symbol); + if(T->left) + { + sameGapManage(T, 1); + } + else + { + gapManage(T, 1); + } +} + +void print_const_exp_array(ASTNode* T) +{ + gapProcess(); + printf("ConstExpArray\n"); + gapManage(T, 1); +} + +void print_const_init_val(ASTNode* T) +{ + if(T->right && T->right->type == ConstExp) + { + handle_next_display = 0; + print_const_exp(T->right); + return; + } + gapProcess(); + printf("ConstInitVal {}\n"); + gapManage(T, 1); +} + +void print_const_exp(ASTNode* T) +{ + if(T->int_val == 0) + { + handle_next_display = 0; + print_mul_exp(T->right); + return; + } + gapProcess(); + printf("ConstExp "); + if(T->d_type == PLUS) + { + printf("+\n"); + } + else + { + printf("-\n"); + } + gapManage(T, 1); +} + +void print_var_decl(ASTNode* T) +{ + gapProcess(); + printf("VarDecl "); + printVarType(T->d_type); + printf("\n"); + gapManage(T, 1); +} + +void print_var_def(ASTNode* T) +{ + if(T->left) + { + currentNum[currentLayer]--; + } + gapProcess(); + printf("VarDef %s\n", T->symbol); + if(T->left) + { + sameGapManage(T, 1); + } + else + { + gapManage(T, 1); + } +} + +void print_init_val(ASTNode* T) +{ + if(T->int_val == Exp) + { + handle_next_display = 0; + print_exp(T->right); + } + else + { + if(T->right) + { + handle_next_display = 0; + print_init_vals(T->right); + } + else + { + gapProcess(); + printf("Null Init Vals\n"); + gapManage(T, 1); + } + } +} + +void print_init_vals(ASTNode* T) +{ + if(T->left) + { + currentNum[currentLayer]--; + } + gapProcess(); + printf("InitVals\n"); + if(T->left) + { + sameGapManage(T, 1); + } + else + { + gapManage(T, 1); + } +} + +void print_func_def(ASTNode* T) +{ + gapProcess(); + printf("FuncDef "); + printVarType(T->d_type); + printf(" %s\n", T->symbol); + gapManage(T, 0); +} + +void print_func_f_param(ASTNode* T) +{ + if(T->left) + { + currentNum[currentLayer]--; + } + gapProcess(); + printf("FuncFParams "); + printVarType(T->d_type); + printf(" %s\n", T->symbol); + if(T->left) + { + sameGapManage(T, 1); + } + else + { + gapManage(T, 1); + } +} + +void print_block(ASTNode* T) +{ + gapProcess(); + printf("Block\n"); + gapManage(T, 1); +} + +void print_block_item(ASTNode* T) +{ + if(T->left) + { + currentNum[currentLayer]--; + } + handle_next_display = 0; + switch(T->right->type) + { + case ConstDecl: + print_const_decl(T->right); + break; + case VarDecl: + print_var_decl(T->right); + break; + case Stmt: + print_stmt(T->right); + break; + } + if(T->left) + { + currentLayer++; + targetNum[currentLayer] = getBranchNum(T) - 1; + currentNum[currentLayer] = 1; + currentLayer--; + display(T->left); + } +} + +void print_stmt(ASTNode* T) +{ + switch (T->int_val) + { + case BlankStmt: + gapProcess(); + printf("Blank Statement\n"); + gapManage(T, 0); + break; + case ExpStmt: + handle_next_display = 0; + print_exp(T->right); + break; + case AssignStmt: + gapProcess(); + printf("Assign Statement\n"); + gapManage(T, 0); + break; + case Block: + handle_next_display = 0; + print_block(T->right); + break; + case IfStmt: + gapProcess(); + printf("If Statement\n"); + gapManage(T, 0); + break; + case IfElseStmt: + gapProcess(); + printf("If-Else Statement\n"); + gapManage(T, 0); + break; + case WhileStmt: + gapProcess(); + printf("While Statement\n"); + gapManage(T, 0); + break; + case BreakStmt: + gapProcess(); + printf("Break Statement\n"); + gapManage(T, 0); + break; + case ContinueStmt: + gapProcess(); + printf("Continue Statement\n"); + gapManage(T, 0); + break; + case BlankReturnStmt: + gapProcess(); + printf("Blank Return Statement\n"); + gapManage(T, 0); + break; + case ReturnStmt: + gapProcess(); + printf("Return Statement\n"); + gapManage(T, 0); + break; + default: + gapProcess(); + printf("Unknown Statement\n"); + gapManage(T, 0); + break; + } +} + +void print_exp(ASTNode* T) +{ + handle_next_display = 0; + print_add_exp(T->right); +} + +void print_add_exp(ASTNode* T) +{ + if(T->int_val == MUL) + { + handle_next_display = 0; + print_mul_exp(T->right); + return; + } + gapProcess(); + printf("AddExp"); + if(T->int_val == PLUS) + { + printf(" +\n"); + } + else + { + printf(" -\n"); + } + gapManage(T, 1); +} + +void print_mul_exp(ASTNode* T) +{ + if(T->int_val == UnaryExp) + { + handle_next_display = 0; + print_unary_exp(T->right); + return; + } + gapProcess(); + printf("MulExp"); + if(T->int_val == MUL) + { + printf(" *\n"); + } + else if(T->int_val == DIV) + { + printf(" /\n"); + } + else if(T->int_val == MOD) + { + printf(" %%\n"); + } + gapManage(T, 1); +} + +void print_unary_exp(ASTNode* T) +{ + if(T->int_val == PrimaryExp) + { + handle_next_display = 0; + print_primary_exp(T->right); + return; + } + gapProcess(); + printf("UnaryExp "); + switch (T->int_val) + { + case FuncRParams: + printf("%s()\n", T->symbol); + break; + case Plus: + printf("+\n"); + break; + case Minus: + printf("-\n"); + break; + case NOT: + printf("NOT\n"); + break; + default: + break; + } + gapManage(T, 1); +} + +void print_func_r_params(ASTNode* T) +{ + if(T->left) + { + currentNum[currentLayer]--; + } + gapProcess(); + printf("FuncRParams\n"); + if(T->left) + { + sameGapManage(T, 1); + } + else + { + gapManage(T, 1); + } +} + +void print_primary_exp(ASTNode* T) +{ + if(T->d_type == NonType) + { + handle_next_display = 0; + if(T->int_val == Exp) + { + print_exp(T->right); + } + else + { + print_lv_al(T->right); + } + return; + } + gapProcess(); + printf("PrimaryExp "); + if(T->d_type == Int) + { + printf("IntLiteral %d\n", T->int_val); + } + else + { + printf("FloatLiteral %f\n", T->float_val); + } + gapManage(T, 1); +} + +void print_lv_al(ASTNode* T) +{ + gapProcess(); + //printVarType(T->d_type); + printf("LVal %s\n", T->symbol); + gapManage(T, 1); +} + +void print_cond(ASTNode* T) +{ + if(T->int_val == 0 && T->right->type == Cond) + { + handle_next_display = 0; + print_cond(T->right); + return; + } + gapProcess(); + printf("Cond"); + if(T->int_val == OR) + { + printf(" OR\n"); + } + else + { + printf("\n"); + } + gapManage(T, 1); +} + +void print_l_and_exp(ASTNode* T) +{ + if(T->int_val == 0) + { + handle_next_display = 0; + print_eq_exp(T->right); + return; + } + gapProcess(); + printf("LAndExp AND\n"); + gapManage(T, 1); +} + +void print_eq_exp(ASTNode* T) +{ + if(T->int_val == 0) + { + handle_next_display = 0; + print_rel_exp(T->right); + return; + } + gapProcess(); + printf("EqExp"); + if(T->int_val == EQ) + { + printf(" ==\n"); + } + else if(T->int_val == NE) + { + printf(" !=\n"); + } + else + { + printf("\n"); + } + gapManage(T, 1); +} + +void print_rel_exp(ASTNode* T) +{ + if(T->int_val == 0) + { + handle_next_display = 0; + print_add_exp(T->right); + return; + } + gapProcess(); + printf("RelExp "); + if(T->int_val == LT) + { + printf("<\n"); + } + else if(T->int_val == GT) + { + printf(">\n"); + } + else if(T->int_val == LE) + { + printf("<=\n"); + } + else + { + printf(">=\n"); + } + gapManage(T, 1); +} + +void print_exp_array(ASTNode* T) +{ + gapProcess(); + printf("ExpArray []\n"); + gapManage(T, 1); +} + +void print_unknown(ASTNode* T) +{ + currentNum[currentLayer]++; + gapProcess(); + printf("Unknown\n"); +} diff --git a/compiler/src/parser/ast.h b/compiler/src/parser/ast.h new file mode 100644 index 0000000..fa45aab --- /dev/null +++ b/compiler/src/parser/ast.h @@ -0,0 +1,116 @@ +#ifndef DEF_H +#define DEF_H + +#include "stdio.h" +#include "stdlib.h" +#include "string.h" +#include "math.h" +#include "stdarg.h" +#include "parser.h" //由bison根据parser.y生成 +#define MAXLENGTH 200 +#define DX 3*sizeof(int) /*活动记录控制信息需要的单元数,这个根据实际系统调整*/ +//以下语法树结点类型、三地址结点类型等定义仅供参考,实验时一定要根据自己的理解来定义 +extern int LEV; //层号 +#define BLOCK -2147483647 + +typedef enum node_type { + CompUnit, + ConstDecl, + VarDecl, + FuncDef, + ConstDef, + ConstInitVal, + VarDef, + InitVal, + FuncFParam, + ExpArray, + Exp, + Block, + BlockItem, + Stmt, + LVal, + PrimaryExp, + UnaryExp, + LOrExp, + FuncRParams, + MulExp, + RelExp, + EqExp, + LAndExp, + LNotExp, + Cond, + ConstExp, + ConstExpArray, + BlankStmt, //空语句 + ExpStmt, // 表达式语句 + AssignStmt, // 赋值语句 + IfStmt, // If语句 + IfElseStmt, // If-Else语句 + WhileStmt, // while语句 + BreakStmt, // break语句 + ContinueStmt, // continue语句 + BlankReturnStmt, //不带返回值的return语句 + AddExp, + ReturnStmt, // 带返回值的return语句 + NonType, + Float, + Int, + InitVals, + Void, + Plus, + Minus, + Root +} node_type; + +// AST节点(最多三个子节点:lef,mid,right,当只有两节点时,置mid为null): +typedef struct ASTNode { + node_type type; + struct ASTNode *left; + struct ASTNode *mid; + struct ASTNode *right; + int int_val; + float float_val; + char *symbol; + node_type d_type; +}ASTNode; + +ASTNode *new_node(node_type type, ASTNode *left, ASTNode *mid, ASTNode *right, int int_val, float float_val, char *symbol, node_type d_type); + +void display(ASTNode* T); +int getBranchNum(ASTNode* T); +void printVarType(node_type type); +void gapProcess(); +void nextDisplay(ASTNode* T); +void nextDisplayReverse(ASTNode* T); + +void print_root(ASTNode* T); +void print_comp_unit(ASTNode* T); +void print_const_decl(ASTNode* T); +void print_const_def(ASTNode* T); +void print_const_exp_array(ASTNode* T); +void print_const_init_val(ASTNode* T); +void print_const_exp(ASTNode* T); +void print_var_decl(ASTNode* T); +void print_var_def(ASTNode* T); +void print_init_val(ASTNode* T); +void print_init_vals(ASTNode* T); +void print_func_def(ASTNode* T); +void print_func_f_param(ASTNode* T); +void print_block(ASTNode* T); +void print_block_item(ASTNode* T); +void print_stmt(ASTNode* T); +void print_exp(ASTNode* T); +void print_add_exp(ASTNode* T); +void print_mul_exp(ASTNode* T); +void print_unary_exp(ASTNode* T); +void print_func_r_params(ASTNode* T); +void print_primary_exp(ASTNode* T); +void print_lv_al(ASTNode* T); +void print_cond(ASTNode* T); +void print_l_and_exp(ASTNode* T); +void print_eq_exp(ASTNode* T); +void print_rel_exp(ASTNode* T); +void print_exp_array(ASTNode* T); +void print_unknown(ASTNode* T); + +#endif \ No newline at end of file diff --git a/compiler/src/parser/ast.o b/compiler/src/parser/ast.o new file mode 100644 index 0000000..e86ffb7 Binary files /dev/null and b/compiler/src/parser/ast.o differ diff --git a/compiler/src/parser/cgen.sh b/compiler/src/parser/cgen.sh new file mode 100644 index 0000000..2fb039c --- /dev/null +++ b/compiler/src/parser/cgen.sh @@ -0,0 +1,2 @@ +bison -d parser.y -o parser.c +flex -o lexer.c lexer.l diff --git a/compiler/src/parser/define.h b/compiler/src/parser/define.h new file mode 100644 index 0000000..a485993 --- /dev/null +++ b/compiler/src/parser/define.h @@ -0,0 +1,15 @@ +#pragma once + +enum STYPE { SEMI, ASS, EXP, CONT, BRE, RET, BLK, SEL, ITER }; + +enum UOP { UOP_ADD, UOP_MINUS, UOP_NOT }; + +enum AOP { AOP_ADD, AOP_MINUS }; + +enum MOP { MOP_MUL, MOP_DIV, MOP_MOD }; + +enum ROP { ROP_GTE, ROP_LTE, ROP_GT, ROP_LT }; + +enum EOP { EOP_EQ, EOP_NEQ }; + +enum TYPE { TYPE_VOID, TYPE_INT, TYPE_FLOAT }; diff --git a/compiler/src/parser/display.c b/compiler/src/parser/display.c new file mode 100644 index 0000000..6d26424 --- /dev/null +++ b/compiler/src/parser/display.c @@ -0,0 +1,101 @@ +#include "ast.h" +extern int targetNum[1024]; +extern int currentNum[1024]; +extern int currentLayer; +extern int handle_next_display; + +void display(ASTNode* T) +{ + handle_next_display = 1; + switch (T->type) + { + case Root: + print_root(T); + break; + case CompUnit: + print_comp_unit(T); + break; + case ConstDecl: + print_const_decl(T); + break; + case ConstDef: + print_const_def(T); + break; + case ConstExpArray: + print_const_exp_array(T); + break; + case ConstInitVal: + print_const_init_val(T); + break; + case ConstExp: + print_const_exp(T); + break; + case VarDecl: + print_var_decl(T); + break; + case VarDef: + print_var_def(T); + break; + case InitVal: + print_init_val(T); + break; + case InitVals: + print_init_vals(T); + break; + case FuncDef: + print_func_def(T); + break; + case FuncFParam: + print_func_f_param(T); + break; + case Block: + print_block(T); + break; + case BlockItem: + print_block_item(T); + break; + case Stmt: + print_stmt(T); + break; + case Exp: + print_exp(T); + break; + case AddExp: + print_add_exp(T); + break; + case MulExp: + print_mul_exp(T); + break; + case UnaryExp: + print_unary_exp(T); + break; + case FuncRParams: + print_func_r_params(T); + break; + case PrimaryExp: + print_primary_exp(T); + break; + case LVal: + print_lv_al(T); + break; + case Cond: + print_cond(T); + break; + case LAndExp: + print_l_and_exp(T); + break; + case EqExp: + print_eq_exp(T); + break; + case RelExp: + print_rel_exp(T); + break; + case ExpArray: + print_exp_array(T); + break; + default: + handle_next_display = 0; + print_unknown(T); + break; + } +} \ No newline at end of file diff --git a/compiler/src/parser/display.o b/compiler/src/parser/display.o new file mode 100644 index 0000000..7d5222f Binary files /dev/null and b/compiler/src/parser/display.o differ diff --git a/compiler/src/parser/lexer.c b/compiler/src/parser/lexer.c new file mode 100644 index 0000000..725448f --- /dev/null +++ b/compiler/src/parser/lexer.c @@ -0,0 +1,2154 @@ +#line 2 "lexer.c" + +#line 4 "lexer.c" + +#define YY_INT_ALIGNED short int + +/* A lexical scanner generated by flex */ + +#define FLEX_SCANNER +#define YY_FLEX_MAJOR_VERSION 2 +#define YY_FLEX_MINOR_VERSION 6 +#define YY_FLEX_SUBMINOR_VERSION 4 +#if YY_FLEX_SUBMINOR_VERSION > 0 +#define FLEX_BETA +#endif + +/* First, we deal with platform-specific or compiler-specific issues. */ + +/* begin standard C headers. */ +#include +#include +#include +#include + +/* end standard C headers. */ + +/* flex integer type definitions */ + +#ifndef FLEXINT_H +#define FLEXINT_H + +/* C99 systems have . Non-C99 systems may or may not. */ + +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + +/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, + * if you want the limit (max/min) macros for int types. + */ +#ifndef __STDC_LIMIT_MACROS +#define __STDC_LIMIT_MACROS 1 +#endif + +#include +typedef int8_t flex_int8_t; +typedef uint8_t flex_uint8_t; +typedef int16_t flex_int16_t; +typedef uint16_t flex_uint16_t; +typedef int32_t flex_int32_t; +typedef uint32_t flex_uint32_t; +#else +typedef signed char flex_int8_t; +typedef short int flex_int16_t; +typedef int flex_int32_t; +typedef unsigned char flex_uint8_t; +typedef unsigned short int flex_uint16_t; +typedef unsigned int flex_uint32_t; + +/* Limits of integral types. */ +#ifndef INT8_MIN +#define INT8_MIN (-128) +#endif +#ifndef INT16_MIN +#define INT16_MIN (-32767-1) +#endif +#ifndef INT32_MIN +#define INT32_MIN (-2147483647-1) +#endif +#ifndef INT8_MAX +#define INT8_MAX (127) +#endif +#ifndef INT16_MAX +#define INT16_MAX (32767) +#endif +#ifndef INT32_MAX +#define INT32_MAX (2147483647) +#endif +#ifndef UINT8_MAX +#define UINT8_MAX (255U) +#endif +#ifndef UINT16_MAX +#define UINT16_MAX (65535U) +#endif +#ifndef UINT32_MAX +#define UINT32_MAX (4294967295U) +#endif + +#ifndef SIZE_MAX +#define SIZE_MAX (~(size_t)0) +#endif + +#endif /* ! C99 */ + +#endif /* ! FLEXINT_H */ + +/* begin standard C++ headers. */ + +/* TODO: this is always defined, so inline it */ +#define yyconst const + +#if defined(__GNUC__) && __GNUC__ >= 3 +#define yynoreturn __attribute__((__noreturn__)) +#else +#define yynoreturn +#endif + +/* Returned upon end-of-file. */ +#define YY_NULL 0 + +/* Promotes a possibly negative, possibly signed char to an + * integer in range [0..255] for use as an array index. + */ +#define YY_SC_TO_UI(c) ((YY_CHAR) (c)) + +/* Enter a start condition. This macro really ought to take a parameter, + * but we do it the disgusting crufty way forced on us by the ()-less + * definition of BEGIN. + */ +#define BEGIN (yy_start) = 1 + 2 * +/* Translate the current start state into a value that can be later handed + * to BEGIN to return to the state. The YYSTATE alias is for lex + * compatibility. + */ +#define YY_START (((yy_start) - 1) / 2) +#define YYSTATE YY_START +/* Action number for EOF rule of a given start state. */ +#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) +/* Special action meaning "start processing a new file". */ +#define YY_NEW_FILE yyrestart( yyin ) +#define YY_END_OF_BUFFER_CHAR 0 + +/* Size of default input buffer. */ +#ifndef YY_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k. + * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case. + * Ditto for the __ia64__ case accordingly. + */ +#define YY_BUF_SIZE 32768 +#else +#define YY_BUF_SIZE 16384 +#endif /* __ia64__ */ +#endif + +/* The state buf must be large enough to hold one state per character in the main buffer. + */ +#define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type)) + +#ifndef YY_TYPEDEF_YY_BUFFER_STATE +#define YY_TYPEDEF_YY_BUFFER_STATE +typedef struct yy_buffer_state *YY_BUFFER_STATE; +#endif + +#ifndef YY_TYPEDEF_YY_SIZE_T +#define YY_TYPEDEF_YY_SIZE_T +typedef size_t yy_size_t; +#endif + +extern int yyleng; + +extern FILE *yyin, *yyout; + +#define EOB_ACT_CONTINUE_SCAN 0 +#define EOB_ACT_END_OF_FILE 1 +#define EOB_ACT_LAST_MATCH 2 + + /* Note: We specifically omit the test for yy_rule_can_match_eol because it requires + * access to the local variable yy_act. Since yyless() is a macro, it would break + * existing scanners that call yyless() from OUTSIDE yylex. + * One obvious solution it to make yy_act a global. I tried that, and saw + * a 5% performance hit in a non-yylineno scanner, because yy_act is + * normally declared as a register variable-- so it is not worth it. + */ + #define YY_LESS_LINENO(n) \ + do { \ + int yyl;\ + for ( yyl = n; yyl < yyleng; ++yyl )\ + if ( yytext[yyl] == '\n' )\ + --yylineno;\ + }while(0) + #define YY_LINENO_REWIND_TO(dst) \ + do {\ + const char *p;\ + for ( p = yy_cp-1; p >= (dst); --p)\ + if ( *p == '\n' )\ + --yylineno;\ + }while(0) + +/* Return all but the first "n" matched characters back to the input stream. */ +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + *yy_cp = (yy_hold_char); \ + YY_RESTORE_YY_MORE_OFFSET \ + (yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ + YY_DO_BEFORE_ACTION; /* set up yytext again */ \ + } \ + while ( 0 ) +#define unput(c) yyunput( c, (yytext_ptr) ) + +#ifndef YY_STRUCT_YY_BUFFER_STATE +#define YY_STRUCT_YY_BUFFER_STATE +struct yy_buffer_state + { + FILE *yy_input_file; + + char *yy_ch_buf; /* input buffer */ + char *yy_buf_pos; /* current position in input buffer */ + + /* Size of input buffer in bytes, not including room for EOB + * characters. + */ + int yy_buf_size; + + /* Number of characters read into yy_ch_buf, not including EOB + * characters. + */ + int yy_n_chars; + + /* Whether we "own" the buffer - i.e., we know we created it, + * and can realloc() it to grow it, and should free() it to + * delete it. + */ + int yy_is_our_buffer; + + /* Whether this is an "interactive" input source; if so, and + * if we're using stdio for input, then we want to use getc() + * instead of fread(), to make sure we stop fetching input after + * each newline. + */ + int yy_is_interactive; + + /* Whether we're considered to be at the beginning of a line. + * If so, '^' rules will be active on the next match, otherwise + * not. + */ + int yy_at_bol; + + int yy_bs_lineno; /**< The line count. */ + int yy_bs_column; /**< The column count. */ + + /* Whether to try to fill the input buffer when we reach the + * end of it. + */ + int yy_fill_buffer; + + int yy_buffer_status; + +#define YY_BUFFER_NEW 0 +#define YY_BUFFER_NORMAL 1 + /* When an EOF's been seen but there's still some text to process + * then we mark the buffer as YY_EOF_PENDING, to indicate that we + * shouldn't try reading from the input source any more. We might + * still have a bunch of tokens to match, though, because of + * possible backing-up. + * + * When we actually see the EOF, we change the status to "new" + * (via yyrestart()), so that the user can continue scanning by + * just pointing yyin at a new input file. + */ +#define YY_BUFFER_EOF_PENDING 2 + + }; +#endif /* !YY_STRUCT_YY_BUFFER_STATE */ + +/* Stack of input buffers. */ +static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */ +static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */ +static YY_BUFFER_STATE * yy_buffer_stack = NULL; /**< Stack as an array. */ + +/* We provide macros for accessing buffer states in case in the + * future we want to put the buffer states in a more general + * "scanner state". + * + * Returns the top of the stack, or NULL. + */ +#define YY_CURRENT_BUFFER ( (yy_buffer_stack) \ + ? (yy_buffer_stack)[(yy_buffer_stack_top)] \ + : NULL) +/* Same as previous macro, but useful when we know that the buffer stack is not + * NULL or when we need an lvalue. For internal use only. + */ +#define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)] + +/* yy_hold_char holds the character lost when yytext is formed. */ +static char yy_hold_char; +static int yy_n_chars; /* number of characters read into yy_ch_buf */ +int yyleng; + +/* Points to current character in buffer. */ +static char *yy_c_buf_p = NULL; +static int yy_init = 0; /* whether we need to initialize */ +static int yy_start = 0; /* start state number */ + +/* Flag which is used to allow yywrap()'s to do buffer switches + * instead of setting up a fresh yyin. A bit of a hack ... + */ +static int yy_did_buffer_switch_on_eof; + +void yyrestart ( FILE *input_file ); +void yy_switch_to_buffer ( YY_BUFFER_STATE new_buffer ); +YY_BUFFER_STATE yy_create_buffer ( FILE *file, int size ); +void yy_delete_buffer ( YY_BUFFER_STATE b ); +void yy_flush_buffer ( YY_BUFFER_STATE b ); +void yypush_buffer_state ( YY_BUFFER_STATE new_buffer ); +void yypop_buffer_state ( void ); + +static void yyensure_buffer_stack ( void ); +static void yy_load_buffer_state ( void ); +static void yy_init_buffer ( YY_BUFFER_STATE b, FILE *file ); +#define YY_FLUSH_BUFFER yy_flush_buffer( YY_CURRENT_BUFFER ) + +YY_BUFFER_STATE yy_scan_buffer ( char *base, yy_size_t size ); +YY_BUFFER_STATE yy_scan_string ( const char *yy_str ); +YY_BUFFER_STATE yy_scan_bytes ( const char *bytes, int len ); + +void *yyalloc ( yy_size_t ); +void *yyrealloc ( void *, yy_size_t ); +void yyfree ( void * ); + +#define yy_new_buffer yy_create_buffer +#define yy_set_interactive(is_interactive) \ + { \ + if ( ! YY_CURRENT_BUFFER ){ \ + yyensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + yy_create_buffer( yyin, YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ + } +#define yy_set_bol(at_bol) \ + { \ + if ( ! YY_CURRENT_BUFFER ){\ + yyensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + yy_create_buffer( yyin, YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ + } +#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) + +/* Begin user sect3 */ + +#define yywrap() (/*CONSTCOND*/1) +#define YY_SKIP_YYWRAP +typedef flex_uint8_t YY_CHAR; + +FILE *yyin = NULL, *yyout = NULL; + +typedef int yy_state_type; + +extern int yylineno; +int yylineno = 1; + +extern char *yytext; +#ifdef yytext_ptr +#undef yytext_ptr +#endif +#define yytext_ptr yytext + +static yy_state_type yy_get_previous_state ( void ); +static yy_state_type yy_try_NUL_trans ( yy_state_type current_state ); +static int yy_get_next_buffer ( void ); +static void yynoreturn yy_fatal_error ( const char* msg ); + +/* Done after the current pattern has been matched and before the + * corresponding action - sets up yytext. + */ +#define YY_DO_BEFORE_ACTION \ + (yytext_ptr) = yy_bp; \ + yyleng = (int) (yy_cp - yy_bp); \ + (yy_hold_char) = *yy_cp; \ + *yy_cp = '\0'; \ + (yy_c_buf_p) = yy_cp; +#define YY_NUM_RULES 45 +#define YY_END_OF_BUFFER 46 +/* This struct is not used in this scanner, + but its presence is necessary. */ +struct yy_trans_info + { + flex_int32_t yy_verify; + flex_int32_t yy_nxt; + }; +static const flex_int16_t yy_accept[116] = + { 0, + 0, 0, 46, 44, 4, 3, 25, 29, 44, 15, + 16, 27, 23, 21, 24, 44, 28, 42, 42, 22, + 34, 26, 36, 39, 17, 18, 39, 39, 39, 39, + 39, 39, 39, 39, 19, 44, 20, 33, 30, 40, + 0, 1, 0, 43, 41, 41, 38, 41, 42, 41, + 41, 35, 32, 37, 39, 39, 39, 39, 39, 10, + 39, 39, 39, 39, 31, 40, 0, 0, 0, 1, + 41, 41, 41, 41, 41, 38, 38, 0, 40, 40, + 41, 40, 39, 39, 39, 39, 5, 39, 39, 39, + 2, 41, 40, 40, 40, 40, 39, 39, 39, 11, + + 39, 39, 7, 39, 13, 8, 39, 6, 39, 12, + 39, 9, 39, 14, 0 + } ; + +static const YY_CHAR yy_ec[256] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 2, 4, 1, 1, 1, 5, 6, 1, 7, + 8, 9, 10, 11, 12, 13, 14, 15, 16, 16, + 16, 16, 16, 16, 16, 17, 17, 1, 18, 19, + 20, 21, 1, 1, 22, 22, 22, 22, 22, 22, + 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + 23, 23, 23, 23, 23, 23, 23, 24, 23, 23, + 25, 1, 26, 1, 27, 1, 28, 29, 30, 31, + + 32, 33, 23, 34, 35, 23, 36, 37, 23, 38, + 39, 23, 23, 40, 41, 42, 43, 44, 45, 24, + 23, 23, 46, 47, 48, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 + } ; + +static const YY_CHAR yy_meta[49] = + { 0, + 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 3, 1, 4, 4, 4, 1, 1, 1, + 1, 4, 4, 4, 1, 1, 5, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 1, 1, 1 + } ; + +static const flex_int16_t yy_base[124] = + { 0, + 0, 0, 251, 252, 252, 252, 230, 252, 243, 252, + 252, 252, 252, 252, 252, 34, 43, 43, 48, 252, + 228, 227, 226, 0, 252, 252, 205, 205, 206, 191, + 33, 181, 173, 176, 252, 162, 252, 252, 252, 53, + 63, 0, 38, 66, 72, 77, 92, 84, 100, 113, + 121, 252, 252, 252, 0, 176, 169, 165, 165, 0, + 161, 158, 156, 155, 252, 252, 64, 97, 126, 0, + 126, 131, 136, 144, 173, 151, 0, 138, 159, 172, + 154, 178, 150, 53, 145, 137, 0, 119, 119, 108, + 252, 79, 181, 118, 186, 252, 91, 77, 83, 0, + + 68, 63, 0, 70, 0, 0, 46, 0, 38, 0, + 31, 0, 30, 0, 252, 212, 215, 216, 221, 226, + 229, 234, 237 + } ; + +static const flex_int16_t yy_def[124] = + { 0, + 115, 1, 115, 115, 115, 115, 115, 115, 115, 115, + 115, 115, 115, 115, 115, 115, 115, 116, 117, 115, + 115, 115, 115, 118, 115, 115, 118, 118, 118, 118, + 118, 118, 118, 118, 115, 115, 115, 115, 115, 115, + 119, 120, 115, 116, 121, 121, 121, 121, 117, 117, + 117, 115, 115, 115, 118, 118, 118, 118, 118, 118, + 118, 118, 118, 118, 115, 115, 119, 122, 119, 120, + 123, 121, 121, 121, 123, 123, 47, 115, 123, 121, + 115, 115, 118, 118, 118, 118, 118, 118, 118, 118, + 115, 79, 115, 123, 121, 115, 118, 118, 118, 118, + + 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, + 118, 118, 118, 118, 0, 115, 115, 115, 115, 115, + 115, 115, 115 + } ; + +static const flex_int16_t yy_nxt[301] = + { 0, + 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, + 23, 24, 24, 24, 25, 26, 24, 24, 27, 28, + 24, 29, 30, 24, 31, 24, 24, 24, 24, 32, + 24, 24, 24, 33, 34, 35, 36, 37, 40, 40, + 40, 41, 40, 40, 40, 43, 42, 44, 44, 45, + 43, 114, 49, 49, 49, 60, 47, 40, 40, 40, + 61, 68, 68, 113, 48, 112, 69, 69, 43, 51, + 44, 44, 45, 111, 43, 66, 71, 71, 72, 115, + 78, 75, 75, 98, 99, 78, 115, 48, 79, 79, + + 80, 110, 109, 74, 115, 68, 76, 76, 77, 108, + 91, 75, 43, 77, 49, 49, 49, 107, 106, 77, + 77, 77, 77, 77, 77, 115, 105, 81, 81, 81, + 115, 51, 78, 115, 68, 82, 82, 82, 43, 69, + 71, 71, 71, 43, 104, 71, 71, 72, 115, 103, + 75, 75, 93, 93, 93, 78, 115, 92, 79, 79, + 80, 102, 74, 115, 101, 76, 76, 76, 81, 81, + 81, 115, 76, 79, 79, 79, 100, 97, 76, 76, + 76, 76, 76, 76, 115, 115, 79, 79, 80, 90, + 89, 94, 82, 82, 82, 93, 93, 93, 115, 88, + + 75, 75, 87, 86, 95, 85, 84, 83, 65, 64, + 96, 63, 62, 96, 46, 46, 46, 50, 50, 55, + 55, 67, 67, 67, 67, 67, 70, 59, 70, 70, + 70, 73, 73, 73, 69, 69, 69, 69, 69, 75, + 75, 75, 58, 57, 56, 54, 53, 52, 39, 38, + 115, 3, 115, 115, 115, 115, 115, 115, 115, 115, + 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, + 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, + 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, + 115, 115, 115, 115, 115, 115, 115, 115, 115, 115 + + } ; + +static const flex_int16_t yy_chk[301] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 16, 16, + 16, 17, 43, 43, 43, 18, 17, 18, 18, 18, + 19, 113, 19, 19, 19, 31, 18, 40, 40, 40, + 31, 41, 67, 111, 18, 109, 41, 67, 44, 19, + 44, 44, 44, 107, 45, 40, 45, 45, 45, 46, + 92, 46, 46, 84, 84, 48, 48, 44, 48, 48, + + 48, 104, 102, 45, 47, 68, 47, 47, 47, 101, + 68, 92, 49, 47, 49, 49, 49, 99, 98, 47, + 47, 47, 47, 47, 47, 50, 97, 50, 50, 50, + 94, 49, 51, 51, 69, 51, 51, 51, 71, 69, + 71, 71, 71, 72, 90, 72, 72, 72, 73, 89, + 73, 73, 78, 78, 78, 74, 74, 71, 74, 74, + 74, 88, 72, 76, 86, 76, 76, 76, 81, 81, + 81, 79, 76, 79, 79, 79, 85, 83, 76, 76, + 76, 76, 76, 76, 80, 75, 80, 80, 80, 64, + 63, 79, 82, 82, 82, 93, 93, 93, 95, 62, + + 95, 95, 61, 59, 80, 58, 57, 56, 36, 34, + 82, 33, 32, 93, 116, 116, 116, 117, 117, 118, + 118, 119, 119, 119, 119, 119, 120, 30, 120, 120, + 120, 121, 121, 121, 122, 122, 122, 122, 122, 123, + 123, 123, 29, 28, 27, 23, 22, 21, 9, 7, + 3, 115, 115, 115, 115, 115, 115, 115, 115, 115, + 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, + 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, + 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, + 115, 115, 115, 115, 115, 115, 115, 115, 115, 115 + + } ; + +/* Table of booleans, true if rule could match eol. */ +static const flex_int32_t yy_rule_can_match_eol[46] = + { 0, +0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, }; + +static yy_state_type yy_last_accepting_state; +static char *yy_last_accepting_cpos; + +extern int yy_flex_debug; +int yy_flex_debug = 0; + +/* The intent behind this definition is that it'll catch + * any uses of REJECT which flex missed. + */ +#define REJECT reject_used_but_not_detected +#define yymore() yymore_used_but_not_detected +#define YY_MORE_ADJ 0 +#define YY_RESTORE_YY_MORE_OFFSET +char *yytext; +#line 1 "lexer.l" +#line 5 "lexer.l" +#include +#include +#include + +#include "ast.h" +#include "parser.h" + +#define TESTINFO(type,value) printf("LexToken(%s,%s,%d))\n",type,value,yylineno) +int line_cnt = 1; +#line 587 "lexer.c" +#line 588 "lexer.c" + +#define INITIAL 0 + +#ifndef YY_NO_UNISTD_H +/* Special case for "unistd.h", since it is non-ANSI. We include it way + * down here because we want the user's section 1 to have been scanned first. + * The user has a chance to override it with an option. + */ +#include +#endif + +#ifndef YY_EXTRA_TYPE +#define YY_EXTRA_TYPE void * +#endif + +static int yy_init_globals ( void ); + +/* Accessor methods to globals. + These are made visible to non-reentrant scanners for convenience. */ + +int yylex_destroy ( void ); + +int yyget_debug ( void ); + +void yyset_debug ( int debug_flag ); + +YY_EXTRA_TYPE yyget_extra ( void ); + +void yyset_extra ( YY_EXTRA_TYPE user_defined ); + +FILE *yyget_in ( void ); + +void yyset_in ( FILE * _in_str ); + +FILE *yyget_out ( void ); + +void yyset_out ( FILE * _out_str ); + + int yyget_leng ( void ); + +char *yyget_text ( void ); + +int yyget_lineno ( void ); + +void yyset_lineno ( int _line_number ); + +/* Macros after this point can all be overridden by user definitions in + * section 1. + */ + +#ifndef YY_SKIP_YYWRAP +#ifdef __cplusplus +extern "C" int yywrap ( void ); +#else +extern int yywrap ( void ); +#endif +#endif + +#ifndef YY_NO_UNPUT + + static void yyunput ( int c, char *buf_ptr ); + +#endif + +#ifndef yytext_ptr +static void yy_flex_strncpy ( char *, const char *, int ); +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen ( const char * ); +#endif + +#ifndef YY_NO_INPUT +#ifdef __cplusplus +static int yyinput ( void ); +#else +static int input ( void ); +#endif + +#endif + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k */ +#define YY_READ_BUF_SIZE 16384 +#else +#define YY_READ_BUF_SIZE 8192 +#endif /* __ia64__ */ +#endif + +/* Copy whatever the last rule matched to the standard output. */ +#ifndef ECHO +/* This used to be an fputs(), but since the string might contain NUL's, + * we now use fwrite(). + */ +#define ECHO do { if (fwrite( yytext, (size_t) yyleng, 1, yyout )) {} } while (0) +#endif + +/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, + * is returned in "result". + */ +#ifndef YY_INPUT +#define YY_INPUT(buf,result,max_size) \ + if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \ + { \ + int c = '*'; \ + int n; \ + for ( n = 0; n < max_size && \ + (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ + buf[n] = (char) c; \ + if ( c == '\n' ) \ + buf[n++] = (char) c; \ + if ( c == EOF && ferror( yyin ) ) \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + result = n; \ + } \ + else \ + { \ + errno=0; \ + while ( (result = (int) fread(buf, 1, (yy_size_t) max_size, yyin)) == 0 && ferror(yyin)) \ + { \ + if( errno != EINTR) \ + { \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + break; \ + } \ + errno=0; \ + clearerr(yyin); \ + } \ + }\ +\ + +#endif + +/* No semi-colon after return; correct usage is to write "yyterminate();" - + * we don't want an extra ';' after the "return" because that will cause + * some compilers to complain about unreachable statements. + */ +#ifndef yyterminate +#define yyterminate() return YY_NULL +#endif + +/* Number of entries by which start-condition stack grows. */ +#ifndef YY_START_STACK_INCR +#define YY_START_STACK_INCR 25 +#endif + +/* Report a fatal error. */ +#ifndef YY_FATAL_ERROR +#define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) +#endif + +/* end tables serialization structures and prototypes */ + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +#define YY_DECL_IS_OURS 1 + +extern int yylex (void); + +#define YY_DECL int yylex (void) +#endif /* !YY_DECL */ + +/* Code executed at the beginning of each rule, after yytext and yyleng + * have been set up. + */ +#ifndef YY_USER_ACTION +#define YY_USER_ACTION +#endif + +/* Code executed at the end of each rule. */ +#ifndef YY_BREAK +#define YY_BREAK /*LINTED*/break; +#endif + +#define YY_RULE_SETUP \ + YY_USER_ACTION + +/** The main scanner function which does all the work. + */ +YY_DECL +{ + yy_state_type yy_current_state; + char *yy_cp, *yy_bp; + int yy_act; + + if ( !(yy_init) ) + { + (yy_init) = 1; + +#ifdef YY_USER_INIT + YY_USER_INIT; +#endif + + if ( ! (yy_start) ) + (yy_start) = 1; /* first start state */ + + if ( ! yyin ) + yyin = stdin; + + if ( ! yyout ) + yyout = stdout; + + if ( ! YY_CURRENT_BUFFER ) { + yyensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + yy_create_buffer( yyin, YY_BUF_SIZE ); + } + + yy_load_buffer_state( ); + } + + { +#line 21 "lexer.l" + +#line 807 "lexer.c" + + while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */ + { + yy_cp = (yy_c_buf_p); + + /* Support of yytext. */ + *yy_cp = (yy_hold_char); + + /* yy_bp points to the position in yy_ch_buf of the start of + * the current run. + */ + yy_bp = yy_cp; + + yy_current_state = (yy_start); +yy_match: + do + { + YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)] ; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 116 ) + yy_c = yy_meta[yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; + ++yy_cp; + } + while ( yy_base[yy_current_state] != 252 ); + +yy_find_action: + yy_act = yy_accept[yy_current_state]; + if ( yy_act == 0 ) + { /* have to back up */ + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + yy_act = yy_accept[yy_current_state]; + } + + YY_DO_BEFORE_ACTION; + + if ( yy_act != YY_END_OF_BUFFER && yy_rule_can_match_eol[yy_act] ) + { + int yyl; + for ( yyl = 0; yyl < yyleng; ++yyl ) + if ( yytext[yyl] == '\n' ) + + yylineno++; +; + } + +do_action: /* This label is used only to access EOF actions. */ + + switch ( yy_act ) + { /* beginning of action switch */ + case 0: /* must back up */ + /* undo the effects of YY_DO_BEFORE_ACTION */ + *yy_cp = (yy_hold_char); + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + goto yy_find_action; + +case 1: +YY_RULE_SETUP +#line 22 "lexer.l" +{ } + YY_BREAK +case 2: +/* rule 2 can match eol */ +YY_RULE_SETUP +#line 23 "lexer.l" +{ + int len = strlen(yytext); + for (int i = 0; i < len; i++) + if(yytext[i] == '\n') + line_cnt++; +} + YY_BREAK +case 3: +/* rule 3 can match eol */ +YY_RULE_SETUP +#line 29 "lexer.l" +{ line_cnt++; } + YY_BREAK +case 4: +YY_RULE_SETUP +#line 30 "lexer.l" +{ } + YY_BREAK +case 5: +YY_RULE_SETUP +#line 31 "lexer.l" +{ TESTINFO("INT","int"); return INT; } + YY_BREAK +case 6: +YY_RULE_SETUP +#line 32 "lexer.l" +{ TESTINFO("FLOAT","float"); return FLOAT; } + YY_BREAK +case 7: +YY_RULE_SETUP +#line 33 "lexer.l" +{ TESTINFO("VOID","void");return VOID; } + YY_BREAK +case 8: +YY_RULE_SETUP +#line 34 "lexer.l" +{ TESTINFO("CONST","const"); return CONST; } + YY_BREAK +case 9: +YY_RULE_SETUP +#line 35 "lexer.l" +{ TESTINFO("RETURN","return"); return RETURN; } + YY_BREAK +case 10: +YY_RULE_SETUP +#line 36 "lexer.l" +{ TESTINFO("IF","if"); return IF; } + YY_BREAK +case 11: +YY_RULE_SETUP +#line 37 "lexer.l" +{ TESTINFO("ELSE","else"); return ELSE; } + YY_BREAK +case 12: +YY_RULE_SETUP +#line 38 "lexer.l" +{ TESTINFO("WHILE","while");return WHILE; } + YY_BREAK +case 13: +YY_RULE_SETUP +#line 39 "lexer.l" +{ TESTINFO("BREAK","break");return BREAK; } + YY_BREAK +case 14: +YY_RULE_SETUP +#line 40 "lexer.l" +{ TESTINFO("CONTINUE","continue"); return CONTINUE; } + YY_BREAK +case 15: +YY_RULE_SETUP +#line 41 "lexer.l" +{ TESTINFO("LP","()");return LP; } + YY_BREAK +case 16: +YY_RULE_SETUP +#line 42 "lexer.l" +{ TESTINFO("RP",")");return RP; } + YY_BREAK +case 17: +YY_RULE_SETUP +#line 43 "lexer.l" +{ TESTINFO("LB","[");return LB; } + YY_BREAK +case 18: +YY_RULE_SETUP +#line 44 "lexer.l" +{ TESTINFO("RB","]");return RB; } + YY_BREAK +case 19: +YY_RULE_SETUP +#line 45 "lexer.l" +{ TESTINFO("LC","{");return LC; } + YY_BREAK +case 20: +YY_RULE_SETUP +#line 46 "lexer.l" +{ TESTINFO("RC","}");return RC; } + YY_BREAK +case 21: +YY_RULE_SETUP +#line 47 "lexer.l" +{ TESTINFO("COMMA",",");return COMMA; } + YY_BREAK +case 22: +YY_RULE_SETUP +#line 48 "lexer.l" +{ TESTINFO("SEMI",";");return SEMICOLON; } + YY_BREAK +case 23: +YY_RULE_SETUP +#line 49 "lexer.l" +{ TESTINFO("PLUS","+");return PLUS; } + YY_BREAK +case 24: +YY_RULE_SETUP +#line 50 "lexer.l" +{ TESTINFO("MINUS","-");return MINUS; } + YY_BREAK +case 25: +YY_RULE_SETUP +#line 51 "lexer.l" +{ TESTINFO("NOT","!");return NOT; } + YY_BREAK +case 26: +YY_RULE_SETUP +#line 52 "lexer.l" +{ TESTINFO("ASSIGN","=");return ASSIGN; } + YY_BREAK +case 27: +YY_RULE_SETUP +#line 53 "lexer.l" +{ TESTINFO("MUL","*");return MUL; } + YY_BREAK +case 28: +YY_RULE_SETUP +#line 54 "lexer.l" +{ TESTINFO("DIV","/");return DIV; } + YY_BREAK +case 29: +YY_RULE_SETUP +#line 55 "lexer.l" +{ TESTINFO("MOD","%");return MOD; } + YY_BREAK +case 30: +YY_RULE_SETUP +#line 56 "lexer.l" +{ TESTINFO("AND","&&");return AND; } + YY_BREAK +case 31: +YY_RULE_SETUP +#line 57 "lexer.l" +{ TESTINFO("OR","||");return OR; } + YY_BREAK +case 32: +YY_RULE_SETUP +#line 58 "lexer.l" +{ TESTINFO("EQ","==");return EQ; } + YY_BREAK +case 33: +YY_RULE_SETUP +#line 59 "lexer.l" +{ TESTINFO("NE","!=");return NE; } + YY_BREAK +case 34: +YY_RULE_SETUP +#line 60 "lexer.l" +{ TESTINFO("LT","<");return LT; } + YY_BREAK +case 35: +YY_RULE_SETUP +#line 61 "lexer.l" +{ TESTINFO("LE","<=");return LE; } + YY_BREAK +case 36: +YY_RULE_SETUP +#line 62 "lexer.l" +{ TESTINFO("GT",">");return GT; } + YY_BREAK +case 37: +YY_RULE_SETUP +#line 63 "lexer.l" +{ TESTINFO("GE",">=");return GE; } + YY_BREAK +case 38: +YY_RULE_SETUP +#line 64 "lexer.l" +{ + int val = 0; + int len = strlen(yytext); + for (int i = 2; i < len; i++) { + val <<= 4; + if (isdigit(yytext[i])) + val += yytext[i] - '0'; + else + val += yytext[i] - 'a' + 10; + } + yylval.int_val = val; + TESTINFO("INT_LIT","int_lit"); + return INT_LIT; +} + YY_BREAK +case 39: +YY_RULE_SETUP +#line 79 "lexer.l" +{ + yylval.str_val = (char *)malloc(strlen(yytext) + 1); + strcpy(yylval.str_val, yytext); + yylval.str_val[strlen(yytext)] = '\0'; + TESTINFO("ID","id"); + return ID; +} + YY_BREAK +case 40: +YY_RULE_SETUP +#line 86 "lexer.l" +{ + yylval.float_val = atof(yytext); + TESTINFO("FLOAT_LIT","float_lit"); + return FLOAT_LIT; } + YY_BREAK +case 41: +YY_RULE_SETUP +#line 90 "lexer.l" +{ return LEX_ERR; } + YY_BREAK +case 42: +YY_RULE_SETUP +#line 91 "lexer.l" +{ yylval.int_val = atoi(yytext); return INT_LIT; } + YY_BREAK +case 43: +YY_RULE_SETUP +#line 92 "lexer.l" +{ + int val = 0; + int len = strlen(yytext); + for (int i = 1; i < len; i++) + val = (val << 3) + yytext[i] - '0'; + yylval.int_val = val; + TESTINFO("INT_LIT","int_lit"); + return INT_LIT; +} + YY_BREAK +case 44: +YY_RULE_SETUP +#line 101 "lexer.l" +{ } + YY_BREAK +case 45: +YY_RULE_SETUP +#line 102 "lexer.l" +ECHO; + YY_BREAK +#line 1136 "lexer.c" +case YY_STATE_EOF(INITIAL): + yyterminate(); + + case YY_END_OF_BUFFER: + { + /* Amount of text matched not including the EOB char. */ + int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1; + + /* Undo the effects of YY_DO_BEFORE_ACTION. */ + *yy_cp = (yy_hold_char); + YY_RESTORE_YY_MORE_OFFSET + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW ) + { + /* We're scanning a new file or input source. It's + * possible that this happened because the user + * just pointed yyin at a new source and called + * yylex(). If so, then we have to assure + * consistency between YY_CURRENT_BUFFER and our + * globals. Here is the right place to do so, because + * this is the first action (other than possibly a + * back-up) that will match for the new input source. + */ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; + } + + /* Note that here we test for yy_c_buf_p "<=" to the position + * of the first EOB in the buffer, since yy_c_buf_p will + * already have been incremented past the NUL character + * (since all states make transitions on EOB to the + * end-of-buffer state). Contrast this with the test + * in input(). + */ + if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + { /* This was really a NUL. */ + yy_state_type yy_next_state; + + (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + /* Okay, we're now positioned to make the NUL + * transition. We couldn't have + * yy_get_previous_state() go ahead and do it + * for us because it doesn't know how to deal + * with the possibility of jamming (and we don't + * want to build jamming into it because then it + * will run more slowly). + */ + + yy_next_state = yy_try_NUL_trans( yy_current_state ); + + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + + if ( yy_next_state ) + { + /* Consume the NUL. */ + yy_cp = ++(yy_c_buf_p); + yy_current_state = yy_next_state; + goto yy_match; + } + + else + { + yy_cp = (yy_c_buf_p); + goto yy_find_action; + } + } + + else switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_END_OF_FILE: + { + (yy_did_buffer_switch_on_eof) = 0; + + if ( yywrap( ) ) + { + /* Note: because we've taken care in + * yy_get_next_buffer() to have set up + * yytext, we can now set up + * yy_c_buf_p so that if some total + * hoser (like flex itself) wants to + * call the scanner after we return the + * YY_NULL, it'll still work - another + * YY_NULL will get returned. + */ + (yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ; + + yy_act = YY_STATE_EOF(YY_START); + goto do_action; + } + + else + { + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; + } + break; + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = + (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_match; + + case EOB_ACT_LAST_MATCH: + (yy_c_buf_p) = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_find_action; + } + break; + } + + default: + YY_FATAL_ERROR( + "fatal flex scanner internal error--no action found" ); + } /* end of action switch */ + } /* end of scanning one token */ + } /* end of user's declarations */ +} /* end of yylex */ + +/* yy_get_next_buffer - try to read in a new buffer + * + * Returns a code representing an action: + * EOB_ACT_LAST_MATCH - + * EOB_ACT_CONTINUE_SCAN - continue scanning from current position + * EOB_ACT_END_OF_FILE - end of file + */ +static int yy_get_next_buffer (void) +{ + char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; + char *source = (yytext_ptr); + int number_to_move, i; + int ret_val; + + if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] ) + YY_FATAL_ERROR( + "fatal flex scanner internal error--end of buffer missed" ); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 ) + { /* Don't try to fill the buffer, so this is an EOF. */ + if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 ) + { + /* We matched a single character, the EOB, so + * treat this as a final EOF. + */ + return EOB_ACT_END_OF_FILE; + } + + else + { + /* We matched some text prior to the EOB, first + * process it. + */ + return EOB_ACT_LAST_MATCH; + } + } + + /* Try to read more data. */ + + /* First move last chars to start of buffer. */ + number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr) - 1); + + for ( i = 0; i < number_to_move; ++i ) + *(dest++) = *(source++); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING ) + /* don't do the read, it's not guaranteed to return an EOF, + * just force an EOF + */ + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0; + + else + { + int num_to_read = + YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; + + while ( num_to_read <= 0 ) + { /* Not enough room in the buffer - grow it. */ + + /* just a shorter name for the current buffer */ + YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE; + + int yy_c_buf_p_offset = + (int) ((yy_c_buf_p) - b->yy_ch_buf); + + if ( b->yy_is_our_buffer ) + { + int new_size = b->yy_buf_size * 2; + + if ( new_size <= 0 ) + b->yy_buf_size += b->yy_buf_size / 8; + else + b->yy_buf_size *= 2; + + b->yy_ch_buf = (char *) + /* Include room in for 2 EOB chars. */ + yyrealloc( (void *) b->yy_ch_buf, + (yy_size_t) (b->yy_buf_size + 2) ); + } + else + /* Can't grow it, we don't own it. */ + b->yy_ch_buf = NULL; + + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( + "fatal error - scanner input buffer overflow" ); + + (yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset]; + + num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - + number_to_move - 1; + + } + + if ( num_to_read > YY_READ_BUF_SIZE ) + num_to_read = YY_READ_BUF_SIZE; + + /* Read in more data. */ + YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), + (yy_n_chars), num_to_read ); + + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + if ( (yy_n_chars) == 0 ) + { + if ( number_to_move == YY_MORE_ADJ ) + { + ret_val = EOB_ACT_END_OF_FILE; + yyrestart( yyin ); + } + + else + { + ret_val = EOB_ACT_LAST_MATCH; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = + YY_BUFFER_EOF_PENDING; + } + } + + else + ret_val = EOB_ACT_CONTINUE_SCAN; + + if (((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) { + /* Extend the array by 50%, plus the number we really need. */ + int new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1); + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc( + (void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf, (yy_size_t) new_size ); + if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" ); + /* "- 2" to take care of EOB's */ + YY_CURRENT_BUFFER_LVALUE->yy_buf_size = (int) (new_size - 2); + } + + (yy_n_chars) += number_to_move; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR; + + (yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; + + return ret_val; +} + +/* yy_get_previous_state - get the state just before the EOB char was reached */ + + static yy_state_type yy_get_previous_state (void) +{ + yy_state_type yy_current_state; + char *yy_cp; + + yy_current_state = (yy_start); + + for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp ) + { + YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 116 ) + yy_c = yy_meta[yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; + } + + return yy_current_state; +} + +/* yy_try_NUL_trans - try to make a transition on the NUL character + * + * synopsis + * next_state = yy_try_NUL_trans( current_state ); + */ + static yy_state_type yy_try_NUL_trans (yy_state_type yy_current_state ) +{ + int yy_is_jam; + char *yy_cp = (yy_c_buf_p); + + YY_CHAR yy_c = 1; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 116 ) + yy_c = yy_meta[yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; + yy_is_jam = (yy_current_state == 115); + + return yy_is_jam ? 0 : yy_current_state; +} + +#ifndef YY_NO_UNPUT + + static void yyunput (int c, char * yy_bp ) +{ + char *yy_cp; + + yy_cp = (yy_c_buf_p); + + /* undo effects of setting up yytext */ + *yy_cp = (yy_hold_char); + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + { /* need to shift things up to make room */ + /* +2 for EOB chars. */ + int number_to_move = (yy_n_chars) + 2; + char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[ + YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2]; + char *source = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]; + + while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) + *--dest = *--source; + + yy_cp += (int) (dest - source); + yy_bp += (int) (dest - source); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = + (yy_n_chars) = (int) YY_CURRENT_BUFFER_LVALUE->yy_buf_size; + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + YY_FATAL_ERROR( "flex scanner push-back overflow" ); + } + + *--yy_cp = (char) c; + + if ( c == '\n' ){ + --yylineno; + } + + (yytext_ptr) = yy_bp; + (yy_hold_char) = *yy_cp; + (yy_c_buf_p) = yy_cp; +} + +#endif + +#ifndef YY_NO_INPUT +#ifdef __cplusplus + static int yyinput (void) +#else + static int input (void) +#endif + +{ + int c; + + *(yy_c_buf_p) = (yy_hold_char); + + if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR ) + { + /* yy_c_buf_p now points to the character we want to return. + * If this occurs *before* the EOB characters, then it's a + * valid NUL; if not, then we've hit the end of the buffer. + */ + if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + /* This was really a NUL. */ + *(yy_c_buf_p) = '\0'; + + else + { /* need more input */ + int offset = (int) ((yy_c_buf_p) - (yytext_ptr)); + ++(yy_c_buf_p); + + switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_LAST_MATCH: + /* This happens because yy_g_n_b() + * sees that we've accumulated a + * token and flags that we need to + * try matching the token before + * proceeding. But for input(), + * there's no matching to consider. + * So convert the EOB_ACT_LAST_MATCH + * to EOB_ACT_END_OF_FILE. + */ + + /* Reset buffer status. */ + yyrestart( yyin ); + + /*FALLTHROUGH*/ + + case EOB_ACT_END_OF_FILE: + { + if ( yywrap( ) ) + return 0; + + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; +#ifdef __cplusplus + return yyinput(); +#else + return input(); +#endif + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = (yytext_ptr) + offset; + break; + } + } + } + + c = *(unsigned char *) (yy_c_buf_p); /* cast for 8-bit char's */ + *(yy_c_buf_p) = '\0'; /* preserve yytext */ + (yy_hold_char) = *++(yy_c_buf_p); + + if ( c == '\n' ) + + yylineno++; +; + + return c; +} +#endif /* ifndef YY_NO_INPUT */ + +/** Immediately switch to a different input stream. + * @param input_file A readable stream. + * + * @note This function does not reset the start condition to @c INITIAL . + */ + void yyrestart (FILE * input_file ) +{ + + if ( ! YY_CURRENT_BUFFER ){ + yyensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + yy_create_buffer( yyin, YY_BUF_SIZE ); + } + + yy_init_buffer( YY_CURRENT_BUFFER, input_file ); + yy_load_buffer_state( ); +} + +/** Switch to a different input buffer. + * @param new_buffer The new input buffer. + * + */ + void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ) +{ + + /* TODO. We should be able to replace this entire function body + * with + * yypop_buffer_state(); + * yypush_buffer_state(new_buffer); + */ + yyensure_buffer_stack (); + if ( YY_CURRENT_BUFFER == new_buffer ) + return; + + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + YY_CURRENT_BUFFER_LVALUE = new_buffer; + yy_load_buffer_state( ); + + /* We don't actually know whether we did this switch during + * EOF (yywrap()) processing, but the only time this flag + * is looked at is after yywrap() is called, so it's safe + * to go ahead and always set it. + */ + (yy_did_buffer_switch_on_eof) = 1; +} + +static void yy_load_buffer_state (void) +{ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + (yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; + yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file; + (yy_hold_char) = *(yy_c_buf_p); +} + +/** Allocate and initialize an input buffer state. + * @param file A readable stream. + * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. + * + * @return the allocated buffer state. + */ + YY_BUFFER_STATE yy_create_buffer (FILE * file, int size ) +{ + YY_BUFFER_STATE b; + + b = (YY_BUFFER_STATE) yyalloc( sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_buf_size = size; + + /* yy_ch_buf has to be 2 characters longer than the size given because + * we need to put in 2 end-of-buffer characters. + */ + b->yy_ch_buf = (char *) yyalloc( (yy_size_t) (b->yy_buf_size + 2) ); + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_is_our_buffer = 1; + + yy_init_buffer( b, file ); + + return b; +} + +/** Destroy the buffer. + * @param b a buffer created with yy_create_buffer() + * + */ + void yy_delete_buffer (YY_BUFFER_STATE b ) +{ + + if ( ! b ) + return; + + if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */ + YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; + + if ( b->yy_is_our_buffer ) + yyfree( (void *) b->yy_ch_buf ); + + yyfree( (void *) b ); +} + +/* Initializes or reinitializes a buffer. + * This function is sometimes called more than once on the same buffer, + * such as during a yyrestart() or at EOF. + */ + static void yy_init_buffer (YY_BUFFER_STATE b, FILE * file ) + +{ + int oerrno = errno; + + yy_flush_buffer( b ); + + b->yy_input_file = file; + b->yy_fill_buffer = 1; + + /* If b is the current buffer, then yy_init_buffer was _probably_ + * called from yyrestart() or through yy_get_next_buffer. + * In that case, we don't want to reset the lineno or column. + */ + if (b != YY_CURRENT_BUFFER){ + b->yy_bs_lineno = 1; + b->yy_bs_column = 0; + } + + b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; + + errno = oerrno; +} + +/** Discard all buffered characters. On the next scan, YY_INPUT will be called. + * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. + * + */ + void yy_flush_buffer (YY_BUFFER_STATE b ) +{ + if ( ! b ) + return; + + b->yy_n_chars = 0; + + /* We always need two end-of-buffer characters. The first causes + * a transition to the end-of-buffer state. The second causes + * a jam in that state. + */ + b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; + b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; + + b->yy_buf_pos = &b->yy_ch_buf[0]; + + b->yy_at_bol = 1; + b->yy_buffer_status = YY_BUFFER_NEW; + + if ( b == YY_CURRENT_BUFFER ) + yy_load_buffer_state( ); +} + +/** Pushes the new state onto the stack. The new state becomes + * the current state. This function will allocate the stack + * if necessary. + * @param new_buffer The new state. + * + */ +void yypush_buffer_state (YY_BUFFER_STATE new_buffer ) +{ + if (new_buffer == NULL) + return; + + yyensure_buffer_stack(); + + /* This block is copied from yy_switch_to_buffer. */ + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + /* Only push if top exists. Otherwise, replace top. */ + if (YY_CURRENT_BUFFER) + (yy_buffer_stack_top)++; + YY_CURRENT_BUFFER_LVALUE = new_buffer; + + /* copied from yy_switch_to_buffer. */ + yy_load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; +} + +/** Removes and deletes the top of the stack, if present. + * The next element becomes the new top. + * + */ +void yypop_buffer_state (void) +{ + if (!YY_CURRENT_BUFFER) + return; + + yy_delete_buffer(YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + if ((yy_buffer_stack_top) > 0) + --(yy_buffer_stack_top); + + if (YY_CURRENT_BUFFER) { + yy_load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; + } +} + +/* Allocates the stack if it does not exist. + * Guarantees space for at least one push. + */ +static void yyensure_buffer_stack (void) +{ + yy_size_t num_to_alloc; + + if (!(yy_buffer_stack)) { + + /* First allocation is just for 2 elements, since we don't know if this + * scanner will even need a stack. We use 2 instead of 1 to avoid an + * immediate realloc on the next call. + */ + num_to_alloc = 1; /* After all that talk, this was set to 1 anyways... */ + (yy_buffer_stack) = (struct yy_buffer_state**)yyalloc + (num_to_alloc * sizeof(struct yy_buffer_state*) + ); + if ( ! (yy_buffer_stack) ) + YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); + + memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*)); + + (yy_buffer_stack_max) = num_to_alloc; + (yy_buffer_stack_top) = 0; + return; + } + + if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){ + + /* Increase the buffer to prepare for a possible push. */ + yy_size_t grow_size = 8 /* arbitrary grow size */; + + num_to_alloc = (yy_buffer_stack_max) + grow_size; + (yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc + ((yy_buffer_stack), + num_to_alloc * sizeof(struct yy_buffer_state*) + ); + if ( ! (yy_buffer_stack) ) + YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); + + /* zero only the new slots.*/ + memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*)); + (yy_buffer_stack_max) = num_to_alloc; + } +} + +/** Setup the input buffer state to scan directly from a user-specified character buffer. + * @param base the character buffer + * @param size the size in bytes of the character buffer + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE yy_scan_buffer (char * base, yy_size_t size ) +{ + YY_BUFFER_STATE b; + + if ( size < 2 || + base[size-2] != YY_END_OF_BUFFER_CHAR || + base[size-1] != YY_END_OF_BUFFER_CHAR ) + /* They forgot to leave room for the EOB's. */ + return NULL; + + b = (YY_BUFFER_STATE) yyalloc( sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" ); + + b->yy_buf_size = (int) (size - 2); /* "- 2" to take care of EOB's */ + b->yy_buf_pos = b->yy_ch_buf = base; + b->yy_is_our_buffer = 0; + b->yy_input_file = NULL; + b->yy_n_chars = b->yy_buf_size; + b->yy_is_interactive = 0; + b->yy_at_bol = 1; + b->yy_fill_buffer = 0; + b->yy_buffer_status = YY_BUFFER_NEW; + + yy_switch_to_buffer( b ); + + return b; +} + +/** Setup the input buffer state to scan a string. The next call to yylex() will + * scan from a @e copy of @a str. + * @param yystr a NUL-terminated string to scan + * + * @return the newly allocated buffer state object. + * @note If you want to scan bytes that may contain NUL values, then use + * yy_scan_bytes() instead. + */ +YY_BUFFER_STATE yy_scan_string (const char * yystr ) +{ + + return yy_scan_bytes( yystr, (int) strlen(yystr) ); +} + +/** Setup the input buffer state to scan the given bytes. The next call to yylex() will + * scan from a @e copy of @a bytes. + * @param yybytes the byte buffer to scan + * @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes. + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE yy_scan_bytes (const char * yybytes, int _yybytes_len ) +{ + YY_BUFFER_STATE b; + char *buf; + yy_size_t n; + int i; + + /* Get memory for full buffer, including space for trailing EOB's. */ + n = (yy_size_t) (_yybytes_len + 2); + buf = (char *) yyalloc( n ); + if ( ! buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" ); + + for ( i = 0; i < _yybytes_len; ++i ) + buf[i] = yybytes[i]; + + buf[_yybytes_len] = buf[_yybytes_len+1] = YY_END_OF_BUFFER_CHAR; + + b = yy_scan_buffer( buf, n ); + if ( ! b ) + YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" ); + + /* It's okay to grow etc. this buffer, and we should throw it + * away when we're done. + */ + b->yy_is_our_buffer = 1; + + return b; +} + +#ifndef YY_EXIT_FAILURE +#define YY_EXIT_FAILURE 2 +#endif + +static void yynoreturn yy_fatal_error (const char* msg ) +{ + fprintf( stderr, "%s\n", msg ); + exit( YY_EXIT_FAILURE ); +} + +/* Redefine yyless() so it works in section 3 code. */ + +#undef yyless +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + yytext[yyleng] = (yy_hold_char); \ + (yy_c_buf_p) = yytext + yyless_macro_arg; \ + (yy_hold_char) = *(yy_c_buf_p); \ + *(yy_c_buf_p) = '\0'; \ + yyleng = yyless_macro_arg; \ + } \ + while ( 0 ) + +/* Accessor methods (get/set functions) to struct members. */ + +/** Get the current line number. + * + */ +int yyget_lineno (void) +{ + + return yylineno; +} + +/** Get the input stream. + * + */ +FILE *yyget_in (void) +{ + return yyin; +} + +/** Get the output stream. + * + */ +FILE *yyget_out (void) +{ + return yyout; +} + +/** Get the length of the current token. + * + */ +int yyget_leng (void) +{ + return yyleng; +} + +/** Get the current token. + * + */ + +char *yyget_text (void) +{ + return yytext; +} + +/** Set the current line number. + * @param _line_number line number + * + */ +void yyset_lineno (int _line_number ) +{ + + yylineno = _line_number; +} + +/** Set the input stream. This does not discard the current + * input buffer. + * @param _in_str A readable stream. + * + * @see yy_switch_to_buffer + */ +void yyset_in (FILE * _in_str ) +{ + yyin = _in_str ; +} + +void yyset_out (FILE * _out_str ) +{ + yyout = _out_str ; +} + +int yyget_debug (void) +{ + return yy_flex_debug; +} + +void yyset_debug (int _bdebug ) +{ + yy_flex_debug = _bdebug ; +} + +static int yy_init_globals (void) +{ + /* Initialization is the same as for the non-reentrant scanner. + * This function is called from yylex_destroy(), so don't allocate here. + */ + + /* We do not touch yylineno unless the option is enabled. */ + yylineno = 1; + + (yy_buffer_stack) = NULL; + (yy_buffer_stack_top) = 0; + (yy_buffer_stack_max) = 0; + (yy_c_buf_p) = NULL; + (yy_init) = 0; + (yy_start) = 0; + +/* Defined in main.c */ +#ifdef YY_STDINIT + yyin = stdin; + yyout = stdout; +#else + yyin = NULL; + yyout = NULL; +#endif + + /* For future reference: Set errno on error, since we are called by + * yylex_init() + */ + return 0; +} + +/* yylex_destroy is for both reentrant and non-reentrant scanners. */ +int yylex_destroy (void) +{ + + /* Pop the buffer stack, destroying each element. */ + while(YY_CURRENT_BUFFER){ + yy_delete_buffer( YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + yypop_buffer_state(); + } + + /* Destroy the stack itself. */ + yyfree((yy_buffer_stack) ); + (yy_buffer_stack) = NULL; + + /* Reset the globals. This is important in a non-reentrant scanner so the next time + * yylex() is called, initialization will occur. */ + yy_init_globals( ); + + return 0; +} + +/* + * Internal utility routines. + */ + +#ifndef yytext_ptr +static void yy_flex_strncpy (char* s1, const char * s2, int n ) +{ + + int i; + for ( i = 0; i < n; ++i ) + s1[i] = s2[i]; +} +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (const char * s ) +{ + int n; + for ( n = 0; s[n]; ++n ) + ; + + return n; +} +#endif + +void *yyalloc (yy_size_t size ) +{ + return malloc(size); +} + +void *yyrealloc (void * ptr, yy_size_t size ) +{ + + /* The cast to (char *) in the following accommodates both + * implementations that use char* generic pointers, and those + * that use void* generic pointers. It works with the latter + * because both ANSI C and C++ allow castless assignment from + * any pointer type to void*, and deal with argument conversions + * as though doing an assignment. + */ + return realloc(ptr, size); +} + +void yyfree (void * ptr ) +{ + free( (char *) ptr ); /* see yyrealloc() for (char *) cast */ +} + +#define YYTABLES_NAME "yytables" + +#line 102 "lexer.l" + + diff --git a/compiler/src/parser/lexer.l b/compiler/src/parser/lexer.l new file mode 100644 index 0000000..e40e1e7 --- /dev/null +++ b/compiler/src/parser/lexer.l @@ -0,0 +1,102 @@ +%option noyywrap +%option yylineno + +%{ +#include +#include +#include + +#include "ast.h" +#include "parser.h" + +#define TESTINFO(type,value) printf("LexToken(%s,%s,%d))\n",type,value,yylineno) +int line_cnt = 1; +%} + + +MultilineComment "/*"([^\*]|(\*)*[^\*/])*(\*)*"*/" +SingleLineComment "//"[^\n]+ +Lex_err [1-9][0-9]*[a-zA-Z]+[0-9]*|0[0-7]*[8-9a-zA-Z_]+[0-9a-zA-Z_]* + +%% +"//".* { } +"/*"([^\*]|(\*)*[^\*/])*(\*)*"*/" { + int len = strlen(yytext); + for (int i = 0; i < len; i++) + if(yytext[i] == '\n') + line_cnt++; +} +"\n" { line_cnt++; } +[ \t] { } +"int" { TESTINFO("INT","int"); return INT; } +"float" { TESTINFO("FLOAT","float"); return FLOAT; } +"void" { TESTINFO("VOID","void");return VOID; } +"const" { TESTINFO("CONST","const"); return CONST; } +"return" { TESTINFO("RETURN","return"); return RETURN; } +"if" { TESTINFO("IF","if"); return IF; } +"else" { TESTINFO("ELSE","else"); return ELSE; } +"while" { TESTINFO("WHILE","while");return WHILE; } +"break" { TESTINFO("BREAK","break");return BREAK; } +"continue" { TESTINFO("CONTINUE","continue"); return CONTINUE; } +"(" { TESTINFO("LP","()");return LP; } +")" { TESTINFO("RP",")");return RP; } +"[" { TESTINFO("LB","[");return LB; } +"]" { TESTINFO("RB","]");return RB; } +"{" { TESTINFO("LC","{");return LC; } +"}" { TESTINFO("RC","}");return RC; } +"," { TESTINFO("COMMA",",");return COMMA; } +";" { TESTINFO("SEMI",";");return SEMICOLON; } +"+" { TESTINFO("PLUS","+");return PLUS; } +"-" { TESTINFO("MINUS","-");return MINUS; } +"!" { TESTINFO("NOT","!");return NOT; } +"=" { TESTINFO("ASSIGN","=");return ASSIGN; } +"*" { TESTINFO("MUL","*");return MUL; } +"/" { TESTINFO("DIV","/");return DIV; } +"%" { TESTINFO("MOD","%");return MOD; } +"&&" { TESTINFO("AND","&&");return AND; } +"||" { TESTINFO("OR","||");return OR; } +"==" { TESTINFO("EQ","==");return EQ; } +"!=" { TESTINFO("NE","!=");return NE; } +"<" { TESTINFO("LT","<");return LT; } +"<=" { TESTINFO("LE","<=");return LE; } +">" { TESTINFO("GT",">");return GT; } +">=" { TESTINFO("GE",">=");return GE; } +0[xX][0-9a-fA-F]* { + int val = 0; + int len = strlen(yytext); + for (int i = 2; i < len; i++) { + val <<= 4; + if (isdigit(yytext[i])) + val += yytext[i] - '0'; + else + val += yytext[i] - 'a' + 10; + } + yylval.int_val = val; + TESTINFO("INT_LIT","int_lit"); + return INT_LIT; +} + +[a-zA-Z_][a-zA-Z0-9_]* { + yylval.str_val = (char *)malloc(strlen(yytext) + 1); + strcpy(yylval.str_val, yytext); + yylval.str_val[strlen(yytext)] = '\0'; + TESTINFO("ID","id"); + return ID; +} +[0-9]*\.[0-9]+f?|[0-9]+e-?[0-9]+f? { + yylval.float_val = atof(yytext); + TESTINFO("FLOAT_LIT","float_lit"); + return FLOAT_LIT; } +{Lex_err} { return LEX_ERR; } +[1-9][0-9]*|0 { yylval.int_val = atoi(yytext); return INT_LIT; } +0[0-7]+ { + int val = 0; + int len = strlen(yytext); + for (int i = 1; i < len; i++) + val = (val << 3) + yytext[i] - '0'; + yylval.int_val = val; + TESTINFO("INT_LIT","int_lit"); + return INT_LIT; +} +. { } +%% diff --git a/compiler/src/parser/lexer.o b/compiler/src/parser/lexer.o new file mode 100644 index 0000000..a864c1e Binary files /dev/null and b/compiler/src/parser/lexer.o differ diff --git a/compiler/src/parser/parser b/compiler/src/parser/parser new file mode 100755 index 0000000..2e44b2a Binary files /dev/null and b/compiler/src/parser/parser differ diff --git a/compiler/src/parser/parser.c b/compiler/src/parser/parser.c new file mode 100644 index 0000000..0973cbd --- /dev/null +++ b/compiler/src/parser/parser.c @@ -0,0 +1,2498 @@ +/* A Bison parser, made by GNU Bison 3.5.1. */ + +/* Bison implementation for Yacc-like parsers in C + + Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2020 Free Software Foundation, + Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + +/* C LALR(1) parser skeleton written by Richard Stallman, by + simplifying the original so-called "semantic" parser. */ + +/* All symbols defined below should begin with yy or YY, to avoid + infringing on user name space. This should be done even for local + variables, as they might otherwise be expanded by user macros. + There are some unavoidable exceptions within include files to + define necessary library symbols; they are noted "INFRINGES ON + USER NAME SPACE" below. */ + +/* Undocumented macros, especially those whose name start with YY_, + are private implementation details. Do not rely on them. */ + +/* Identify Bison output. */ +#define YYBISON 1 + +/* Bison version. */ +#define YYBISON_VERSION "3.5.1" + +/* Skeleton name. */ +#define YYSKELETON_NAME "yacc.c" + +/* Pure parsers. */ +#define YYPURE 0 + +/* Push parsers. */ +#define YYPUSH 0 + +/* Pull parsers. */ +#define YYPULL 1 + + + + +/* First part of user prologue. */ +#line 3 "parser.y" + +#include +#include +#include +#include + +#include "ast.h" + +ASTNode *root; + +extern FILE *yyin; +extern int line_cnt; +extern int yylineno; +extern char *yytext; +extern int yylex(); +extern int yyparse(); +//extern void yyerror(char *msg); +void yyerror(const char* fmt, ...); +int syntax_error = 0; +char filename[100]; + +#line 92 "parser.c" + +# ifndef YY_CAST +# ifdef __cplusplus +# define YY_CAST(Type, Val) static_cast (Val) +# define YY_REINTERPRET_CAST(Type, Val) reinterpret_cast (Val) +# else +# define YY_CAST(Type, Val) ((Type) (Val)) +# define YY_REINTERPRET_CAST(Type, Val) ((Type) (Val)) +# endif +# endif +# ifndef YY_NULLPTR +# if defined __cplusplus +# if 201103L <= __cplusplus +# define YY_NULLPTR nullptr +# else +# define YY_NULLPTR 0 +# endif +# else +# define YY_NULLPTR ((void*)0) +# endif +# endif + +/* Enabling verbose error messages. */ +#ifdef YYERROR_VERBOSE +# undef YYERROR_VERBOSE +# define YYERROR_VERBOSE 1 +#else +# define YYERROR_VERBOSE 1 +#endif + +/* Use api.header.include to #include this header + instead of duplicating it here. */ +#ifndef YY_YY_PARSER_H_INCLUDED +# define YY_YY_PARSER_H_INCLUDED +/* Debug traces. */ +#ifndef YYDEBUG +# define YYDEBUG 0 +#endif +#if YYDEBUG +extern int yydebug; +#endif + +/* Token type. */ +#ifndef YYTOKENTYPE +# define YYTOKENTYPE + enum yytokentype + { + ID = 258, + INT_LIT = 259, + FLOAT_LIT = 260, + INT = 261, + FLOAT = 262, + VOID = 263, + CONST = 264, + RETURN = 265, + IF = 266, + ELSE = 267, + WHILE = 268, + BREAK = 269, + CONTINUE = 270, + LP = 271, + RP = 272, + LB = 273, + RB = 274, + LC = 275, + RC = 276, + COMMA = 277, + SEMICOLON = 278, + MINUS = 279, + NOT = 280, + ASSIGN = 281, + PLUS = 282, + MUL = 283, + DIV = 284, + MOD = 285, + AND = 286, + OR = 287, + EQ = 288, + NE = 289, + LT = 290, + LE = 291, + GT = 292, + GE = 293, + LEX_ERR = 294, + THEN = 295 + }; +#endif + +/* Value type. */ +#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED +union YYSTYPE +{ +#line 25 "parser.y" + + int int_val; + float float_val; + char *str_val; + struct ASTNode *node_val; + +#line 192 "parser.c" + +}; +typedef union YYSTYPE YYSTYPE; +# define YYSTYPE_IS_TRIVIAL 1 +# define YYSTYPE_IS_DECLARED 1 +#endif + +/* Location type. */ +#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED +typedef struct YYLTYPE YYLTYPE; +struct YYLTYPE +{ + int first_line; + int first_column; + int last_line; + int last_column; +}; +# define YYLTYPE_IS_DECLARED 1 +# define YYLTYPE_IS_TRIVIAL 1 +#endif + + +extern YYSTYPE yylval; +extern YYLTYPE yylloc; +int yyparse (void); + +#endif /* !YY_YY_PARSER_H_INCLUDED */ + + + +#ifdef short +# undef short +#endif + +/* On compilers that do not define __PTRDIFF_MAX__ etc., make sure + and (if available) are included + so that the code can choose integer types of a good width. */ + +#ifndef __PTRDIFF_MAX__ +# include /* INFRINGES ON USER NAME SPACE */ +# if defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__ +# include /* INFRINGES ON USER NAME SPACE */ +# define YY_STDINT_H +# endif +#endif + +/* Narrow types that promote to a signed type and that can represent a + signed or unsigned integer of at least N bits. In tables they can + save space and decrease cache pressure. Promoting to a signed type + helps avoid bugs in integer arithmetic. */ + +#ifdef __INT_LEAST8_MAX__ +typedef __INT_LEAST8_TYPE__ yytype_int8; +#elif defined YY_STDINT_H +typedef int_least8_t yytype_int8; +#else +typedef signed char yytype_int8; +#endif + +#ifdef __INT_LEAST16_MAX__ +typedef __INT_LEAST16_TYPE__ yytype_int16; +#elif defined YY_STDINT_H +typedef int_least16_t yytype_int16; +#else +typedef short yytype_int16; +#endif + +#if defined __UINT_LEAST8_MAX__ && __UINT_LEAST8_MAX__ <= __INT_MAX__ +typedef __UINT_LEAST8_TYPE__ yytype_uint8; +#elif (!defined __UINT_LEAST8_MAX__ && defined YY_STDINT_H \ + && UINT_LEAST8_MAX <= INT_MAX) +typedef uint_least8_t yytype_uint8; +#elif !defined __UINT_LEAST8_MAX__ && UCHAR_MAX <= INT_MAX +typedef unsigned char yytype_uint8; +#else +typedef short yytype_uint8; +#endif + +#if defined __UINT_LEAST16_MAX__ && __UINT_LEAST16_MAX__ <= __INT_MAX__ +typedef __UINT_LEAST16_TYPE__ yytype_uint16; +#elif (!defined __UINT_LEAST16_MAX__ && defined YY_STDINT_H \ + && UINT_LEAST16_MAX <= INT_MAX) +typedef uint_least16_t yytype_uint16; +#elif !defined __UINT_LEAST16_MAX__ && USHRT_MAX <= INT_MAX +typedef unsigned short yytype_uint16; +#else +typedef int yytype_uint16; +#endif + +#ifndef YYPTRDIFF_T +# if defined __PTRDIFF_TYPE__ && defined __PTRDIFF_MAX__ +# define YYPTRDIFF_T __PTRDIFF_TYPE__ +# define YYPTRDIFF_MAXIMUM __PTRDIFF_MAX__ +# elif defined PTRDIFF_MAX +# ifndef ptrdiff_t +# include /* INFRINGES ON USER NAME SPACE */ +# endif +# define YYPTRDIFF_T ptrdiff_t +# define YYPTRDIFF_MAXIMUM PTRDIFF_MAX +# else +# define YYPTRDIFF_T long +# define YYPTRDIFF_MAXIMUM LONG_MAX +# endif +#endif + +#ifndef YYSIZE_T +# ifdef __SIZE_TYPE__ +# define YYSIZE_T __SIZE_TYPE__ +# elif defined size_t +# define YYSIZE_T size_t +# elif defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__ +# include /* INFRINGES ON USER NAME SPACE */ +# define YYSIZE_T size_t +# else +# define YYSIZE_T unsigned +# endif +#endif + +#define YYSIZE_MAXIMUM \ + YY_CAST (YYPTRDIFF_T, \ + (YYPTRDIFF_MAXIMUM < YY_CAST (YYSIZE_T, -1) \ + ? YYPTRDIFF_MAXIMUM \ + : YY_CAST (YYSIZE_T, -1))) + +#define YYSIZEOF(X) YY_CAST (YYPTRDIFF_T, sizeof (X)) + +/* Stored state numbers (used for stacks). */ +typedef yytype_uint8 yy_state_t; + +/* State numbers in computations. */ +typedef int yy_state_fast_t; + +#ifndef YY_ +# if defined YYENABLE_NLS && YYENABLE_NLS +# if ENABLE_NLS +# include /* INFRINGES ON USER NAME SPACE */ +# define YY_(Msgid) dgettext ("bison-runtime", Msgid) +# endif +# endif +# ifndef YY_ +# define YY_(Msgid) Msgid +# endif +#endif + +#ifndef YY_ATTRIBUTE_PURE +# if defined __GNUC__ && 2 < __GNUC__ + (96 <= __GNUC_MINOR__) +# define YY_ATTRIBUTE_PURE __attribute__ ((__pure__)) +# else +# define YY_ATTRIBUTE_PURE +# endif +#endif + +#ifndef YY_ATTRIBUTE_UNUSED +# if defined __GNUC__ && 2 < __GNUC__ + (7 <= __GNUC_MINOR__) +# define YY_ATTRIBUTE_UNUSED __attribute__ ((__unused__)) +# else +# define YY_ATTRIBUTE_UNUSED +# endif +#endif + +/* Suppress unused-variable warnings by "using" E. */ +#if ! defined lint || defined __GNUC__ +# define YYUSE(E) ((void) (E)) +#else +# define YYUSE(E) /* empty */ +#endif + +#if defined __GNUC__ && ! defined __ICC && 407 <= __GNUC__ * 100 + __GNUC_MINOR__ +/* Suppress an incorrect diagnostic about yylval being uninitialized. */ +# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \ + _Pragma ("GCC diagnostic push") \ + _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"") \ + _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"") +# define YY_IGNORE_MAYBE_UNINITIALIZED_END \ + _Pragma ("GCC diagnostic pop") +#else +# define YY_INITIAL_VALUE(Value) Value +#endif +#ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN +# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN +# define YY_IGNORE_MAYBE_UNINITIALIZED_END +#endif +#ifndef YY_INITIAL_VALUE +# define YY_INITIAL_VALUE(Value) /* Nothing. */ +#endif + +#if defined __cplusplus && defined __GNUC__ && ! defined __ICC && 6 <= __GNUC__ +# define YY_IGNORE_USELESS_CAST_BEGIN \ + _Pragma ("GCC diagnostic push") \ + _Pragma ("GCC diagnostic ignored \"-Wuseless-cast\"") +# define YY_IGNORE_USELESS_CAST_END \ + _Pragma ("GCC diagnostic pop") +#endif +#ifndef YY_IGNORE_USELESS_CAST_BEGIN +# define YY_IGNORE_USELESS_CAST_BEGIN +# define YY_IGNORE_USELESS_CAST_END +#endif + + +#define YY_ASSERT(E) ((void) (0 && (E))) + +#if ! defined yyoverflow || YYERROR_VERBOSE + +/* The parser invokes alloca or malloc; define the necessary symbols. */ + +# ifdef YYSTACK_USE_ALLOCA +# if YYSTACK_USE_ALLOCA +# ifdef __GNUC__ +# define YYSTACK_ALLOC __builtin_alloca +# elif defined __BUILTIN_VA_ARG_INCR +# include /* INFRINGES ON USER NAME SPACE */ +# elif defined _AIX +# define YYSTACK_ALLOC __alloca +# elif defined _MSC_VER +# include /* INFRINGES ON USER NAME SPACE */ +# define alloca _alloca +# else +# define YYSTACK_ALLOC alloca +# if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS +# include /* INFRINGES ON USER NAME SPACE */ + /* Use EXIT_SUCCESS as a witness for stdlib.h. */ +# ifndef EXIT_SUCCESS +# define EXIT_SUCCESS 0 +# endif +# endif +# endif +# endif +# endif + +# ifdef YYSTACK_ALLOC + /* Pacify GCC's 'empty if-body' warning. */ +# define YYSTACK_FREE(Ptr) do { /* empty */; } while (0) +# ifndef YYSTACK_ALLOC_MAXIMUM + /* The OS might guarantee only one guard page at the bottom of the stack, + and a page size can be as small as 4096 bytes. So we cannot safely + invoke alloca (N) if N exceeds 4096. Use a slightly smaller number + to allow for a few compiler-allocated temporary stack slots. */ +# define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */ +# endif +# else +# define YYSTACK_ALLOC YYMALLOC +# define YYSTACK_FREE YYFREE +# ifndef YYSTACK_ALLOC_MAXIMUM +# define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM +# endif +# if (defined __cplusplus && ! defined EXIT_SUCCESS \ + && ! ((defined YYMALLOC || defined malloc) \ + && (defined YYFREE || defined free))) +# include /* INFRINGES ON USER NAME SPACE */ +# ifndef EXIT_SUCCESS +# define EXIT_SUCCESS 0 +# endif +# endif +# ifndef YYMALLOC +# define YYMALLOC malloc +# if ! defined malloc && ! defined EXIT_SUCCESS +void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */ +# endif +# endif +# ifndef YYFREE +# define YYFREE free +# if ! defined free && ! defined EXIT_SUCCESS +void free (void *); /* INFRINGES ON USER NAME SPACE */ +# endif +# endif +# endif +#endif /* ! defined yyoverflow || YYERROR_VERBOSE */ + + +#if (! defined yyoverflow \ + && (! defined __cplusplus \ + || (defined YYLTYPE_IS_TRIVIAL && YYLTYPE_IS_TRIVIAL \ + && defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL))) + +/* A type that is properly aligned for any stack member. */ +union yyalloc +{ + yy_state_t yyss_alloc; + YYSTYPE yyvs_alloc; + YYLTYPE yyls_alloc; +}; + +/* The size of the maximum gap between one aligned stack and the next. */ +# define YYSTACK_GAP_MAXIMUM (YYSIZEOF (union yyalloc) - 1) + +/* The size of an array large to enough to hold all stacks, each with + N elements. */ +# define YYSTACK_BYTES(N) \ + ((N) * (YYSIZEOF (yy_state_t) + YYSIZEOF (YYSTYPE) \ + + YYSIZEOF (YYLTYPE)) \ + + 2 * YYSTACK_GAP_MAXIMUM) + +# define YYCOPY_NEEDED 1 + +/* Relocate STACK from its old location to the new one. The + local variables YYSIZE and YYSTACKSIZE give the old and new number of + elements in the stack, and YYPTR gives the new location of the + stack. Advance YYPTR to a properly aligned location for the next + stack. */ +# define YYSTACK_RELOCATE(Stack_alloc, Stack) \ + do \ + { \ + YYPTRDIFF_T yynewbytes; \ + YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \ + Stack = &yyptr->Stack_alloc; \ + yynewbytes = yystacksize * YYSIZEOF (*Stack) + YYSTACK_GAP_MAXIMUM; \ + yyptr += yynewbytes / YYSIZEOF (*yyptr); \ + } \ + while (0) + +#endif + +#if defined YYCOPY_NEEDED && YYCOPY_NEEDED +/* Copy COUNT objects from SRC to DST. The source and destination do + not overlap. */ +# ifndef YYCOPY +# if defined __GNUC__ && 1 < __GNUC__ +# define YYCOPY(Dst, Src, Count) \ + __builtin_memcpy (Dst, Src, YY_CAST (YYSIZE_T, (Count)) * sizeof (*(Src))) +# else +# define YYCOPY(Dst, Src, Count) \ + do \ + { \ + YYPTRDIFF_T yyi; \ + for (yyi = 0; yyi < (Count); yyi++) \ + (Dst)[yyi] = (Src)[yyi]; \ + } \ + while (0) +# endif +# endif +#endif /* !YYCOPY_NEEDED */ + +/* YYFINAL -- State number of the termination state. */ +#define YYFINAL 17 +/* YYLAST -- Last index in YYTABLE. */ +#define YYLAST 301 + +/* YYNTOKENS -- Number of terminals. */ +#define YYNTOKENS 41 +/* YYNNTS -- Number of nonterminals. */ +#define YYNNTS 31 +/* YYNRULES -- Number of rules. */ +#define YYNRULES 100 +/* YYNSTATES -- Number of states. */ +#define YYNSTATES 207 + +#define YYUNDEFTOK 2 +#define YYMAXUTOK 295 + + +/* YYTRANSLATE(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM + as returned by yylex, with out-of-bounds checking. */ +#define YYTRANSLATE(YYX) \ + (0 <= (YYX) && (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK) + +/* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM + as returned by yylex. */ +static const yytype_int8 yytranslate[] = +{ + 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 1, 2, 3, 4, + 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, + 35, 36, 37, 38, 39, 40 +}; + +#if YYDEBUG + /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ +static const yytype_uint8 yyrline[] = +{ + 0, 51, 51, 52, 53, 54, 55, 56, 57, 59, + 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, + 70, 71, 73, 74, 75, 76, 77, 78, 79, 80, + 81, 82, 83, 85, 86, 87, 88, 89, 90, 91, + 92, 93, 94, 95, 96, 97, 98, 100, 101, 102, + 103, 104, 107, 108, 109, 110, 111, 112, 113, 114, + 115, 116, 117, 122, 123, 124, 125, 126, 127, 128, + 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, + 139, 140, 141, 142, 144, 145, 146, 147, 149, 150, + 152, 154, 155, 156, 158, 159, 160, 161, 162, 164, + 165 +}; +#endif + +#if YYDEBUG || YYERROR_VERBOSE || 1 +/* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. + First, the terminals, then, starting at YYNTOKENS, nonterminals. */ +static const char *const yytname[] = +{ + "$end", "error", "$undefined", "ID", "INT_LIT", "FLOAT_LIT", "INT", + "FLOAT", "VOID", "CONST", "RETURN", "IF", "ELSE", "WHILE", "BREAK", + "CONTINUE", "LP", "RP", "LB", "RB", "LC", "RC", "COMMA", "SEMICOLON", + "MINUS", "NOT", "ASSIGN", "PLUS", "MUL", "DIV", "MOD", "AND", "OR", "EQ", + "NE", "LT", "LE", "GT", "GE", "LEX_ERR", "THEN", "$accept", "Root", + "CompUnit", "ConstDecl", "ConstDef", "ConstExpArray", "ConstInitVal", + "ConstExp", "VarDecl", "VarDef", "InitVal", "InitVals", "FuncDef", + "FuncFParam", "Block", "BlockItem", "Stmt", "Exp", "AddExp", "MulExp", + "UnaryExp", "FuncRParams", "PrimaryExp", "LVal", "Cond", "LOrExp", + "LAndExp", "LNotExp", "EqExp", "RelExp", "ExpArray", YY_NULLPTR +}; +#endif + +# ifdef YYPRINT +/* YYTOKNUM[NUM] -- (External) token number corresponding to the + (internal) symbol number NUM (which must be that of a token). */ +static const yytype_int16 yytoknum[] = +{ + 0, 256, 257, 258, 259, 260, 261, 262, 263, 264, + 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, + 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, + 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, + 295 +}; +# endif + +#define YYPACT_NINF (-167) + +#define yypact_value_is_default(Yyn) \ + ((Yyn) == YYPACT_NINF) + +#define YYTABLE_NINF (-64) + +#define yytable_value_is_error(Yyn) \ + 0 + + /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing + STATE-NUM. */ +static const yytype_int16 yypact[] = +{ + 112, 57, 85, 110, 94, 9, -167, 112, 112, 112, + 28, 93, 117, 126, 131, 155, 155, -167, -167, -167, + -167, 64, 232, -5, -167, 98, -167, 100, 144, 153, + 164, 198, 207, 191, 195, 124, -167, -167, 232, 232, + 232, 232, 196, -8, 116, -167, -167, 214, 189, 191, + 201, 191, 208, 200, -167, -167, 80, 104, 150, -167, + 191, 203, 232, -167, 212, -167, 6, -167, -167, -167, + 144, 232, 232, 232, 232, 232, 144, -167, 70, 202, + -167, -167, 191, -167, 191, 218, 220, 165, 221, 165, + 214, 214, 228, 225, 230, 224, 226, -167, 150, 150, + -167, 229, 150, 231, 237, -167, -167, 236, 250, 249, + -167, 232, 232, -167, -167, -167, -167, -167, -167, -167, + 247, 251, 214, -167, -167, 107, 248, -167, 253, -167, + 253, -167, -167, 252, 257, 257, -167, -167, -167, -167, + -167, -167, -167, 232, 232, -167, 253, -167, -167, 189, + -167, -167, -167, 182, 155, 254, 256, -167, 261, 101, + 262, -167, 242, -167, 258, 163, 263, 260, -167, -167, + -167, -167, 218, -167, 165, 165, 232, 232, 232, 232, + 232, 175, 257, 232, 232, 232, 175, -167, 266, -167, + -167, 12, 273, -167, -167, -167, -167, 279, -167, -167, + -167, -167, -167, -167, -167, 175, -167 +}; + + /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM. + Performed when YYTABLE does not specify something else to do. Zero + means the default is an error. */ +static const yytype_int8 yydefact[] = +{ + 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, + 13, 0, 13, 0, 0, 0, 0, 1, 6, 7, + 8, 0, 0, 24, 22, 0, 23, 0, 13, 0, + 0, 0, 0, 0, 0, 99, 81, 82, 0, 0, + 0, 0, 0, 19, 67, 71, 80, 0, 0, 0, + 0, 0, 0, 0, 9, 10, 39, 40, 48, 33, + 0, 0, 0, 83, 0, 63, 64, 75, 76, 74, + 13, 0, 0, 0, 0, 0, 13, 26, 0, 25, + 28, 34, 0, 35, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 54, 48, 48, + 55, 0, 48, 0, 80, 36, 72, 77, 0, 0, + 79, 0, 0, 14, 21, 20, 68, 69, 70, 29, + 31, 0, 0, 37, 38, 0, 11, 15, 99, 43, + 99, 44, 61, 0, 0, 0, 59, 60, 49, 50, + 47, 51, 53, 0, 0, 73, 99, 66, 65, 0, + 30, 27, 16, 0, 0, 41, 42, 62, 0, 94, + 0, 84, 85, 87, 88, 91, 0, 0, 78, 100, + 32, 17, 0, 12, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 52, 0, 45, + 46, 94, 0, 95, 97, 96, 98, 56, 86, 89, + 92, 93, 58, 18, 90, 0, 57 +}; + + /* YYPGOTO[NTERM-NUM]. */ +static const yytype_int16 yypgoto[] = +{ + -167, -167, 160, -40, -11, -3, -111, 270, -30, 8, + 245, 145, -167, -23, 5, -6, -166, -35, -100, -22, + -33, 151, -167, -57, 161, 115, 118, -167, -153, 4, + -106 +}; + + /* YYDEFGOTO[NTERM-NUM]. */ +static const yytype_int16 yydefgoto[] = +{ + -1, 5, 6, 7, 29, 23, 126, 127, 8, 11, + 120, 121, 9, 34, 100, 101, 102, 103, 65, 66, + 44, 108, 45, 46, 160, 161, 162, 163, 164, 165, + 63 +}; + + /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If + positive, shift that token. If negative, reduce the rule whose + number is the opposite. If YYTABLE_NINF, syntax error. */ +static const yytype_int16 yytable[] = +{ + 43, 104, 50, 64, 52, 30, 67, 68, 69, 17, + 13, 147, 148, 80, 153, 197, 71, 47, 98, 72, + 202, 48, 155, 192, 156, 53, 107, 109, 99, -63, + 111, 200, 201, 112, 159, 159, 114, 115, 59, 206, + 169, 104, 104, 80, 21, 104, 22, 177, 178, 179, + 180, 116, 117, 118, 81, 77, 83, 133, 98, 98, + 10, 188, 98, 43, 129, 105, 131, 113, 99, 99, + 31, 32, 99, 35, 36, 37, 191, 159, 159, 159, + 159, 33, 159, 159, 159, 159, 38, 123, 12, 124, + 78, 119, 138, 139, 39, 40, 141, 41, 86, 13, + 15, 16, 87, 43, 31, 32, 31, 32, 167, 107, + 35, 36, 37, 14, 80, 49, 24, 51, 1, 2, + 3, 4, 88, 38, 104, 68, 89, 125, 152, 104, + 151, 39, 40, 25, 41, 22, 177, 178, 179, 180, + 61, 64, 62, 173, 73, 74, 75, 27, 104, 26, + 43, 189, 190, 35, 36, 37, 90, 91, 28, 4, + 92, 93, 22, 94, 95, 96, 38, 18, 19, 20, + 58, 31, 32, 97, 39, 40, 54, 41, 35, 36, + 37, 193, 194, 195, 196, 92, 93, 55, 94, 95, + 96, 38, 35, 36, 37, 58, 184, 185, 97, 39, + 40, 56, 41, 171, 172, 38, 35, 36, 37, 78, + 57, 58, 60, 39, 40, 70, 41, 76, 82, 38, + 106, 35, 36, 37, 122, 84, 85, 39, 40, 110, + 41, 35, 36, 37, 38, 35, 36, 37, 125, 128, + 130, 134, 39, 40, 38, 41, 135, 136, 38, 137, + 140, 132, 39, 40, 142, 41, 39, 40, 144, 41, + 35, 36, 37, 143, 35, 36, 37, 145, 146, 149, + 154, 62, 150, 38, 182, 157, 174, 176, 175, 181, + 186, 39, 158, 187, 41, 39, 40, 203, 41, 183, + 204, 205, 42, 79, 170, 168, 166, 198, 0, 0, + 0, 199 +}; + +static const yytype_int16 yycheck[] = +{ + 22, 58, 25, 38, 27, 16, 39, 40, 41, 0, + 2, 111, 112, 48, 125, 181, 24, 22, 58, 27, + 186, 26, 128, 176, 130, 28, 61, 62, 58, 17, + 24, 184, 185, 27, 134, 135, 71, 72, 33, 205, + 146, 98, 99, 78, 16, 102, 18, 35, 36, 37, + 38, 73, 74, 75, 49, 47, 51, 92, 98, 99, + 3, 172, 102, 85, 87, 60, 89, 70, 98, 99, + 6, 7, 102, 3, 4, 5, 176, 177, 178, 179, + 180, 17, 182, 183, 184, 185, 16, 82, 3, 84, + 20, 21, 98, 99, 24, 25, 102, 27, 18, 91, + 6, 7, 22, 125, 6, 7, 6, 7, 143, 144, + 3, 4, 5, 3, 149, 17, 23, 17, 6, 7, + 8, 9, 18, 16, 181, 158, 22, 20, 21, 186, + 122, 24, 25, 16, 27, 18, 35, 36, 37, 38, + 16, 176, 18, 154, 28, 29, 30, 16, 205, 23, + 172, 174, 175, 3, 4, 5, 6, 7, 3, 9, + 10, 11, 18, 13, 14, 15, 16, 7, 8, 9, + 20, 6, 7, 23, 24, 25, 23, 27, 3, 4, + 5, 177, 178, 179, 180, 10, 11, 23, 13, 14, + 15, 16, 3, 4, 5, 20, 33, 34, 23, 24, + 25, 3, 27, 21, 22, 16, 3, 4, 5, 20, + 3, 20, 17, 24, 25, 19, 27, 3, 17, 16, + 17, 3, 4, 5, 22, 17, 26, 24, 25, 17, + 27, 3, 4, 5, 16, 3, 4, 5, 20, 19, + 19, 16, 24, 25, 16, 27, 16, 23, 16, 23, + 21, 23, 24, 25, 23, 27, 24, 25, 22, 27, + 3, 4, 5, 26, 3, 4, 5, 17, 19, 22, + 22, 18, 21, 16, 32, 23, 22, 16, 22, 17, + 17, 24, 25, 23, 27, 24, 25, 21, 27, 31, + 17, 12, 22, 48, 149, 144, 135, 182, -1, -1, + -1, 183 +}; + + /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing + symbol of state STATE-NUM. */ +static const yytype_int8 yystos[] = +{ + 0, 6, 7, 8, 9, 42, 43, 44, 49, 53, + 3, 50, 3, 50, 3, 6, 7, 0, 43, 43, + 43, 16, 18, 46, 23, 16, 23, 16, 3, 45, + 45, 6, 7, 17, 54, 3, 4, 5, 16, 24, + 25, 27, 48, 60, 61, 63, 64, 22, 26, 17, + 54, 17, 54, 46, 23, 23, 3, 3, 20, 55, + 17, 16, 18, 71, 58, 59, 60, 61, 61, 61, + 19, 24, 27, 28, 29, 30, 3, 50, 20, 51, + 58, 55, 17, 55, 17, 26, 18, 22, 18, 22, + 6, 7, 10, 11, 13, 14, 15, 23, 44, 49, + 55, 56, 57, 58, 64, 55, 17, 58, 62, 58, + 17, 24, 27, 46, 58, 58, 60, 60, 60, 21, + 51, 52, 22, 55, 55, 20, 47, 48, 19, 54, + 19, 54, 23, 58, 16, 16, 23, 23, 56, 56, + 21, 56, 23, 26, 22, 17, 19, 59, 59, 22, + 21, 50, 21, 47, 22, 71, 71, 23, 25, 59, + 65, 66, 67, 68, 69, 70, 65, 58, 62, 71, + 52, 21, 22, 45, 22, 22, 16, 35, 36, 37, + 38, 17, 32, 31, 33, 34, 17, 23, 47, 54, + 54, 59, 69, 70, 70, 70, 70, 57, 66, 67, + 69, 69, 57, 21, 17, 12, 57 +}; + + /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ +static const yytype_int8 yyr1[] = +{ + 0, 41, 42, 43, 43, 43, 43, 43, 43, 44, + 44, 45, 45, 46, 46, 47, 47, 47, 47, 48, + 48, 48, 49, 49, 50, 50, 50, 50, 51, 51, + 51, 52, 52, 53, 53, 53, 53, 53, 53, 54, + 54, 54, 54, 54, 54, 54, 54, 55, 56, 56, + 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, + 57, 57, 57, 58, 59, 59, 59, 60, 60, 60, + 60, 61, 61, 61, 61, 61, 61, 62, 62, 63, + 63, 63, 63, 64, 65, 66, 66, 66, 67, 67, + 68, 69, 69, 69, 70, 70, 70, 70, 70, 71, + 71 +}; + + /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */ +static const yytype_int8 yyr2[] = +{ + 0, 2, 1, 1, 1, 1, 2, 2, 2, 4, + 4, 4, 6, 0, 4, 1, 2, 3, 5, 1, + 3, 3, 3, 3, 2, 4, 4, 6, 1, 2, + 3, 1, 3, 5, 5, 5, 6, 6, 6, 2, + 2, 5, 5, 4, 4, 7, 7, 3, 0, 2, + 2, 2, 4, 2, 1, 1, 5, 7, 5, 2, + 2, 2, 3, 1, 1, 3, 3, 1, 3, 3, + 3, 1, 3, 4, 2, 2, 2, 1, 3, 3, + 1, 1, 1, 2, 1, 1, 3, 1, 1, 3, + 4, 1, 3, 3, 1, 3, 3, 3, 3, 0, + 4 +}; + + +#define yyerrok (yyerrstatus = 0) +#define yyclearin (yychar = YYEMPTY) +#define YYEMPTY (-2) +#define YYEOF 0 + +#define YYACCEPT goto yyacceptlab +#define YYABORT goto yyabortlab +#define YYERROR goto yyerrorlab + + +#define YYRECOVERING() (!!yyerrstatus) + +#define YYBACKUP(Token, Value) \ + do \ + if (yychar == YYEMPTY) \ + { \ + yychar = (Token); \ + yylval = (Value); \ + YYPOPSTACK (yylen); \ + yystate = *yyssp; \ + goto yybackup; \ + } \ + else \ + { \ + yyerror (YY_("syntax error: cannot back up")); \ + YYERROR; \ + } \ + while (0) + +/* Error token number */ +#define YYTERROR 1 +#define YYERRCODE 256 + + +/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N]. + If N is 0, then set CURRENT to the empty location which ends + the previous symbol: RHS[0] (always defined). */ + +#ifndef YYLLOC_DEFAULT +# define YYLLOC_DEFAULT(Current, Rhs, N) \ + do \ + if (N) \ + { \ + (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \ + (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \ + (Current).last_line = YYRHSLOC (Rhs, N).last_line; \ + (Current).last_column = YYRHSLOC (Rhs, N).last_column; \ + } \ + else \ + { \ + (Current).first_line = (Current).last_line = \ + YYRHSLOC (Rhs, 0).last_line; \ + (Current).first_column = (Current).last_column = \ + YYRHSLOC (Rhs, 0).last_column; \ + } \ + while (0) +#endif + +#define YYRHSLOC(Rhs, K) ((Rhs)[K]) + + +/* Enable debugging if requested. */ +#if YYDEBUG + +# ifndef YYFPRINTF +# include /* INFRINGES ON USER NAME SPACE */ +# define YYFPRINTF fprintf +# endif + +# define YYDPRINTF(Args) \ +do { \ + if (yydebug) \ + YYFPRINTF Args; \ +} while (0) + + +/* YY_LOCATION_PRINT -- Print the location on the stream. + This macro was not mandated originally: define only if we know + we won't break user code: when these are the locations we know. */ + +#ifndef YY_LOCATION_PRINT +# if defined YYLTYPE_IS_TRIVIAL && YYLTYPE_IS_TRIVIAL + +/* Print *YYLOCP on YYO. Private, do not rely on its existence. */ + +YY_ATTRIBUTE_UNUSED +static int +yy_location_print_ (FILE *yyo, YYLTYPE const * const yylocp) +{ + int res = 0; + int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0; + if (0 <= yylocp->first_line) + { + res += YYFPRINTF (yyo, "%d", yylocp->first_line); + if (0 <= yylocp->first_column) + res += YYFPRINTF (yyo, ".%d", yylocp->first_column); + } + if (0 <= yylocp->last_line) + { + if (yylocp->first_line < yylocp->last_line) + { + res += YYFPRINTF (yyo, "-%d", yylocp->last_line); + if (0 <= end_col) + res += YYFPRINTF (yyo, ".%d", end_col); + } + else if (0 <= end_col && yylocp->first_column < end_col) + res += YYFPRINTF (yyo, "-%d", end_col); + } + return res; + } + +# define YY_LOCATION_PRINT(File, Loc) \ + yy_location_print_ (File, &(Loc)) + +# else +# define YY_LOCATION_PRINT(File, Loc) ((void) 0) +# endif +#endif + + +# define YY_SYMBOL_PRINT(Title, Type, Value, Location) \ +do { \ + if (yydebug) \ + { \ + YYFPRINTF (stderr, "%s ", Title); \ + yy_symbol_print (stderr, \ + Type, Value, Location); \ + YYFPRINTF (stderr, "\n"); \ + } \ +} while (0) + + +/*-----------------------------------. +| Print this symbol's value on YYO. | +`-----------------------------------*/ + +static void +yy_symbol_value_print (FILE *yyo, int yytype, YYSTYPE const * const yyvaluep, YYLTYPE const * const yylocationp) +{ + FILE *yyoutput = yyo; + YYUSE (yyoutput); + YYUSE (yylocationp); + if (!yyvaluep) + return; +# ifdef YYPRINT + if (yytype < YYNTOKENS) + YYPRINT (yyo, yytoknum[yytype], *yyvaluep); +# endif + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + YYUSE (yytype); + YY_IGNORE_MAYBE_UNINITIALIZED_END +} + + +/*---------------------------. +| Print this symbol on YYO. | +`---------------------------*/ + +static void +yy_symbol_print (FILE *yyo, int yytype, YYSTYPE const * const yyvaluep, YYLTYPE const * const yylocationp) +{ + YYFPRINTF (yyo, "%s %s (", + yytype < YYNTOKENS ? "token" : "nterm", yytname[yytype]); + + YY_LOCATION_PRINT (yyo, *yylocationp); + YYFPRINTF (yyo, ": "); + yy_symbol_value_print (yyo, yytype, yyvaluep, yylocationp); + YYFPRINTF (yyo, ")"); +} + +/*------------------------------------------------------------------. +| yy_stack_print -- Print the state stack from its BOTTOM up to its | +| TOP (included). | +`------------------------------------------------------------------*/ + +static void +yy_stack_print (yy_state_t *yybottom, yy_state_t *yytop) +{ + YYFPRINTF (stderr, "Stack now"); + for (; yybottom <= yytop; yybottom++) + { + int yybot = *yybottom; + YYFPRINTF (stderr, " %d", yybot); + } + YYFPRINTF (stderr, "\n"); +} + +# define YY_STACK_PRINT(Bottom, Top) \ +do { \ + if (yydebug) \ + yy_stack_print ((Bottom), (Top)); \ +} while (0) + + +/*------------------------------------------------. +| Report that the YYRULE is going to be reduced. | +`------------------------------------------------*/ + +static void +yy_reduce_print (yy_state_t *yyssp, YYSTYPE *yyvsp, YYLTYPE *yylsp, int yyrule) +{ + int yylno = yyrline[yyrule]; + int yynrhs = yyr2[yyrule]; + int yyi; + YYFPRINTF (stderr, "Reducing stack by rule %d (line %d):\n", + yyrule - 1, yylno); + /* The symbols being reduced. */ + for (yyi = 0; yyi < yynrhs; yyi++) + { + YYFPRINTF (stderr, " $%d = ", yyi + 1); + yy_symbol_print (stderr, + yystos[+yyssp[yyi + 1 - yynrhs]], + &yyvsp[(yyi + 1) - (yynrhs)] + , &(yylsp[(yyi + 1) - (yynrhs)]) ); + YYFPRINTF (stderr, "\n"); + } +} + +# define YY_REDUCE_PRINT(Rule) \ +do { \ + if (yydebug) \ + yy_reduce_print (yyssp, yyvsp, yylsp, Rule); \ +} while (0) + +/* Nonzero means print parse trace. It is left uninitialized so that + multiple parsers can coexist. */ +int yydebug; +#else /* !YYDEBUG */ +# define YYDPRINTF(Args) +# define YY_SYMBOL_PRINT(Title, Type, Value, Location) +# define YY_STACK_PRINT(Bottom, Top) +# define YY_REDUCE_PRINT(Rule) +#endif /* !YYDEBUG */ + + +/* YYINITDEPTH -- initial size of the parser's stacks. */ +#ifndef YYINITDEPTH +# define YYINITDEPTH 200 +#endif + +/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only + if the built-in stack extension method is used). + + Do not make this value too large; the results are undefined if + YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH) + evaluated with infinite-precision integer arithmetic. */ + +#ifndef YYMAXDEPTH +# define YYMAXDEPTH 10000 +#endif + + +#if YYERROR_VERBOSE + +# ifndef yystrlen +# if defined __GLIBC__ && defined _STRING_H +# define yystrlen(S) (YY_CAST (YYPTRDIFF_T, strlen (S))) +# else +/* Return the length of YYSTR. */ +static YYPTRDIFF_T +yystrlen (const char *yystr) +{ + YYPTRDIFF_T yylen; + for (yylen = 0; yystr[yylen]; yylen++) + continue; + return yylen; +} +# endif +# endif + +# ifndef yystpcpy +# if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE +# define yystpcpy stpcpy +# else +/* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in + YYDEST. */ +static char * +yystpcpy (char *yydest, const char *yysrc) +{ + char *yyd = yydest; + const char *yys = yysrc; + + while ((*yyd++ = *yys++) != '\0') + continue; + + return yyd - 1; +} +# endif +# endif + +# ifndef yytnamerr +/* Copy to YYRES the contents of YYSTR after stripping away unnecessary + quotes and backslashes, so that it's suitable for yyerror. The + heuristic is that double-quoting is unnecessary unless the string + contains an apostrophe, a comma, or backslash (other than + backslash-backslash). YYSTR is taken from yytname. If YYRES is + null, do not copy; instead, return the length of what the result + would have been. */ +static YYPTRDIFF_T +yytnamerr (char *yyres, const char *yystr) +{ + if (*yystr == '"') + { + YYPTRDIFF_T yyn = 0; + char const *yyp = yystr; + + for (;;) + switch (*++yyp) + { + case '\'': + case ',': + goto do_not_strip_quotes; + + case '\\': + if (*++yyp != '\\') + goto do_not_strip_quotes; + else + goto append; + + append: + default: + if (yyres) + yyres[yyn] = *yyp; + yyn++; + break; + + case '"': + if (yyres) + yyres[yyn] = '\0'; + return yyn; + } + do_not_strip_quotes: ; + } + + if (yyres) + return yystpcpy (yyres, yystr) - yyres; + else + return yystrlen (yystr); +} +# endif + +/* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message + about the unexpected token YYTOKEN for the state stack whose top is + YYSSP. + + Return 0 if *YYMSG was successfully written. Return 1 if *YYMSG is + not large enough to hold the message. In that case, also set + *YYMSG_ALLOC to the required number of bytes. Return 2 if the + required number of bytes is too large to store. */ +static int +yysyntax_error (YYPTRDIFF_T *yymsg_alloc, char **yymsg, + yy_state_t *yyssp, int yytoken) +{ + enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 }; + /* Internationalized format string. */ + const char *yyformat = YY_NULLPTR; + /* Arguments of yyformat: reported tokens (one for the "unexpected", + one per "expected"). */ + char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM]; + /* Actual size of YYARG. */ + int yycount = 0; + /* Cumulated lengths of YYARG. */ + YYPTRDIFF_T yysize = 0; + + /* There are many possibilities here to consider: + - If this state is a consistent state with a default action, then + the only way this function was invoked is if the default action + is an error action. In that case, don't check for expected + tokens because there are none. + - The only way there can be no lookahead present (in yychar) is if + this state is a consistent state with a default action. Thus, + detecting the absence of a lookahead is sufficient to determine + that there is no unexpected or expected token to report. In that + case, just report a simple "syntax error". + - Don't assume there isn't a lookahead just because this state is a + consistent state with a default action. There might have been a + previous inconsistent state, consistent state with a non-default + action, or user semantic action that manipulated yychar. + - Of course, the expected token list depends on states to have + correct lookahead information, and it depends on the parser not + to perform extra reductions after fetching a lookahead from the + scanner and before detecting a syntax error. Thus, state merging + (from LALR or IELR) and default reductions corrupt the expected + token list. However, the list is correct for canonical LR with + one exception: it will still contain any token that will not be + accepted due to an error action in a later state. + */ + if (yytoken != YYEMPTY) + { + int yyn = yypact[+*yyssp]; + YYPTRDIFF_T yysize0 = yytnamerr (YY_NULLPTR, yytname[yytoken]); + yysize = yysize0; + yyarg[yycount++] = yytname[yytoken]; + if (!yypact_value_is_default (yyn)) + { + /* Start YYX at -YYN if negative to avoid negative indexes in + YYCHECK. In other words, skip the first -YYN actions for + this state because they are default actions. */ + int yyxbegin = yyn < 0 ? -yyn : 0; + /* Stay within bounds of both yycheck and yytname. */ + int yychecklim = YYLAST - yyn + 1; + int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS; + int yyx; + + for (yyx = yyxbegin; yyx < yyxend; ++yyx) + if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR + && !yytable_value_is_error (yytable[yyx + yyn])) + { + if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM) + { + yycount = 1; + yysize = yysize0; + break; + } + yyarg[yycount++] = yytname[yyx]; + { + YYPTRDIFF_T yysize1 + = yysize + yytnamerr (YY_NULLPTR, yytname[yyx]); + if (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM) + yysize = yysize1; + else + return 2; + } + } + } + } + + switch (yycount) + { +# define YYCASE_(N, S) \ + case N: \ + yyformat = S; \ + break + default: /* Avoid compiler warnings. */ + YYCASE_(0, YY_("syntax error")); + YYCASE_(1, YY_("syntax error, unexpected %s")); + YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s")); + YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s")); + YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s")); + YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s")); +# undef YYCASE_ + } + + { + /* Don't count the "%s"s in the final size, but reserve room for + the terminator. */ + YYPTRDIFF_T yysize1 = yysize + (yystrlen (yyformat) - 2 * yycount) + 1; + if (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM) + yysize = yysize1; + else + return 2; + } + + if (*yymsg_alloc < yysize) + { + *yymsg_alloc = 2 * yysize; + if (! (yysize <= *yymsg_alloc + && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM)) + *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM; + return 1; + } + + /* Avoid sprintf, as that infringes on the user's name space. + Don't have undefined behavior even if the translation + produced a string with the wrong number of "%s"s. */ + { + char *yyp = *yymsg; + int yyi = 0; + while ((*yyp = *yyformat) != '\0') + if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount) + { + yyp += yytnamerr (yyp, yyarg[yyi++]); + yyformat += 2; + } + else + { + ++yyp; + ++yyformat; + } + } + return 0; +} +#endif /* YYERROR_VERBOSE */ + +/*-----------------------------------------------. +| Release the memory associated to this symbol. | +`-----------------------------------------------*/ + +static void +yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep, YYLTYPE *yylocationp) +{ + YYUSE (yyvaluep); + YYUSE (yylocationp); + if (!yymsg) + yymsg = "Deleting"; + YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp); + + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + YYUSE (yytype); + YY_IGNORE_MAYBE_UNINITIALIZED_END +} + + + + +/* The lookahead symbol. */ +int yychar; + +/* The semantic value of the lookahead symbol. */ +YYSTYPE yylval; +/* Location data for the lookahead symbol. */ +YYLTYPE yylloc +# if defined YYLTYPE_IS_TRIVIAL && YYLTYPE_IS_TRIVIAL + = { 1, 1, 1, 1 } +# endif +; +/* Number of syntax errors so far. */ +int yynerrs; + + +/*----------. +| yyparse. | +`----------*/ + +int +yyparse (void) +{ + yy_state_fast_t yystate; + /* Number of tokens to shift before error messages enabled. */ + int yyerrstatus; + + /* The stacks and their tools: + 'yyss': related to states. + 'yyvs': related to semantic values. + 'yyls': related to locations. + + Refer to the stacks through separate pointers, to allow yyoverflow + to reallocate them elsewhere. */ + + /* The state stack. */ + yy_state_t yyssa[YYINITDEPTH]; + yy_state_t *yyss; + yy_state_t *yyssp; + + /* The semantic value stack. */ + YYSTYPE yyvsa[YYINITDEPTH]; + YYSTYPE *yyvs; + YYSTYPE *yyvsp; + + /* The location stack. */ + YYLTYPE yylsa[YYINITDEPTH]; + YYLTYPE *yyls; + YYLTYPE *yylsp; + + /* The locations where the error started and ended. */ + YYLTYPE yyerror_range[3]; + + YYPTRDIFF_T yystacksize; + + int yyn; + int yyresult; + /* Lookahead token as an internal (translated) token number. */ + int yytoken = 0; + /* The variables used to return semantic value and location from the + action routines. */ + YYSTYPE yyval; + YYLTYPE yyloc; + +#if YYERROR_VERBOSE + /* Buffer for error messages, and its allocated size. */ + char yymsgbuf[128]; + char *yymsg = yymsgbuf; + YYPTRDIFF_T yymsg_alloc = sizeof yymsgbuf; +#endif + +#define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N), yylsp -= (N)) + + /* The number of symbols on the RHS of the reduced rule. + Keep to zero when no symbol should be popped. */ + int yylen = 0; + + yyssp = yyss = yyssa; + yyvsp = yyvs = yyvsa; + yylsp = yyls = yylsa; + yystacksize = YYINITDEPTH; + + YYDPRINTF ((stderr, "Starting parse\n")); + + yystate = 0; + yyerrstatus = 0; + yynerrs = 0; + yychar = YYEMPTY; /* Cause a token to be read. */ + yylsp[0] = yylloc; + goto yysetstate; + + +/*------------------------------------------------------------. +| yynewstate -- push a new state, which is found in yystate. | +`------------------------------------------------------------*/ +yynewstate: + /* In all cases, when you get here, the value and location stacks + have just been pushed. So pushing a state here evens the stacks. */ + yyssp++; + + +/*--------------------------------------------------------------------. +| yysetstate -- set current state (the top of the stack) to yystate. | +`--------------------------------------------------------------------*/ +yysetstate: + YYDPRINTF ((stderr, "Entering state %d\n", yystate)); + YY_ASSERT (0 <= yystate && yystate < YYNSTATES); + YY_IGNORE_USELESS_CAST_BEGIN + *yyssp = YY_CAST (yy_state_t, yystate); + YY_IGNORE_USELESS_CAST_END + + if (yyss + yystacksize - 1 <= yyssp) +#if !defined yyoverflow && !defined YYSTACK_RELOCATE + goto yyexhaustedlab; +#else + { + /* Get the current used size of the three stacks, in elements. */ + YYPTRDIFF_T yysize = yyssp - yyss + 1; + +# if defined yyoverflow + { + /* Give user a chance to reallocate the stack. Use copies of + these so that the &'s don't force the real ones into + memory. */ + yy_state_t *yyss1 = yyss; + YYSTYPE *yyvs1 = yyvs; + YYLTYPE *yyls1 = yyls; + + /* Each stack pointer address is followed by the size of the + data in use in that stack, in bytes. This used to be a + conditional around just the two extra args, but that might + be undefined if yyoverflow is a macro. */ + yyoverflow (YY_("memory exhausted"), + &yyss1, yysize * YYSIZEOF (*yyssp), + &yyvs1, yysize * YYSIZEOF (*yyvsp), + &yyls1, yysize * YYSIZEOF (*yylsp), + &yystacksize); + yyss = yyss1; + yyvs = yyvs1; + yyls = yyls1; + } +# else /* defined YYSTACK_RELOCATE */ + /* Extend the stack our own way. */ + if (YYMAXDEPTH <= yystacksize) + goto yyexhaustedlab; + yystacksize *= 2; + if (YYMAXDEPTH < yystacksize) + yystacksize = YYMAXDEPTH; + + { + yy_state_t *yyss1 = yyss; + union yyalloc *yyptr = + YY_CAST (union yyalloc *, + YYSTACK_ALLOC (YY_CAST (YYSIZE_T, YYSTACK_BYTES (yystacksize)))); + if (! yyptr) + goto yyexhaustedlab; + YYSTACK_RELOCATE (yyss_alloc, yyss); + YYSTACK_RELOCATE (yyvs_alloc, yyvs); + YYSTACK_RELOCATE (yyls_alloc, yyls); +# undef YYSTACK_RELOCATE + if (yyss1 != yyssa) + YYSTACK_FREE (yyss1); + } +# endif + + yyssp = yyss + yysize - 1; + yyvsp = yyvs + yysize - 1; + yylsp = yyls + yysize - 1; + + YY_IGNORE_USELESS_CAST_BEGIN + YYDPRINTF ((stderr, "Stack size increased to %ld\n", + YY_CAST (long, yystacksize))); + YY_IGNORE_USELESS_CAST_END + + if (yyss + yystacksize - 1 <= yyssp) + YYABORT; + } +#endif /* !defined yyoverflow && !defined YYSTACK_RELOCATE */ + + if (yystate == YYFINAL) + YYACCEPT; + + goto yybackup; + + +/*-----------. +| yybackup. | +`-----------*/ +yybackup: + /* Do appropriate processing given the current state. Read a + lookahead token if we need one and don't already have one. */ + + /* First try to decide what to do without reference to lookahead token. */ + yyn = yypact[yystate]; + if (yypact_value_is_default (yyn)) + goto yydefault; + + /* Not known => get a lookahead token if don't already have one. */ + + /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */ + if (yychar == YYEMPTY) + { + YYDPRINTF ((stderr, "Reading a token: ")); + yychar = yylex (); + } + + if (yychar <= YYEOF) + { + yychar = yytoken = YYEOF; + YYDPRINTF ((stderr, "Now at end of input.\n")); + } + else + { + yytoken = YYTRANSLATE (yychar); + YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc); + } + + /* If the proper action on seeing token YYTOKEN is to reduce or to + detect an error, take that action. */ + yyn += yytoken; + if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken) + goto yydefault; + yyn = yytable[yyn]; + if (yyn <= 0) + { + if (yytable_value_is_error (yyn)) + goto yyerrlab; + yyn = -yyn; + goto yyreduce; + } + + /* Count tokens shifted since error; after three, turn off error + status. */ + if (yyerrstatus) + yyerrstatus--; + + /* Shift the lookahead token. */ + YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc); + yystate = yyn; + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + *++yyvsp = yylval; + YY_IGNORE_MAYBE_UNINITIALIZED_END + *++yylsp = yylloc; + + /* Discard the shifted token. */ + yychar = YYEMPTY; + goto yynewstate; + + +/*-----------------------------------------------------------. +| yydefault -- do the default action for the current state. | +`-----------------------------------------------------------*/ +yydefault: + yyn = yydefact[yystate]; + if (yyn == 0) + goto yyerrlab; + goto yyreduce; + + +/*-----------------------------. +| yyreduce -- do a reduction. | +`-----------------------------*/ +yyreduce: + /* yyn is the number of a rule to reduce with. */ + yylen = yyr2[yyn]; + + /* If YYLEN is nonzero, implement the default value of the action: + '$$ = $1'. + + Otherwise, the following line sets YYVAL to garbage. + This behavior is undocumented and Bison + users should not rely upon it. Assigning to YYVAL + unconditionally makes the parser a bit smaller, and it avoids a + GCC warning that YYVAL may be used uninitialized. */ + yyval = yyvsp[1-yylen]; + + /* Default location. */ + YYLLOC_DEFAULT (yyloc, (yylsp - yylen), yylen); + yyerror_range[1] = yyloc; + YY_REDUCE_PRINT (yyn); + switch (yyn) + { + case 2: +#line 51 "parser.y" + { root = new_node(Root, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 1638 "parser.c" + break; + + case 3: +#line 52 "parser.y" + { (yyval.node_val) = new_node(CompUnit, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 1644 "parser.c" + break; + + case 4: +#line 53 "parser.y" + { (yyval.node_val) = new_node(CompUnit, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 1650 "parser.c" + break; + + case 5: +#line 54 "parser.y" + { (yyval.node_val) = new_node(CompUnit, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 1656 "parser.c" + break; + + case 6: +#line 55 "parser.y" + { (yyval.node_val) = new_node(CompUnit, (yyvsp[0].node_val), NULL, (yyvsp[-1].node_val), 0, 0, NULL, NonType); } +#line 1662 "parser.c" + break; + + case 7: +#line 56 "parser.y" + { (yyval.node_val) = new_node(CompUnit, (yyvsp[0].node_val), NULL, (yyvsp[-1].node_val), 0, 0, NULL, NonType); } +#line 1668 "parser.c" + break; + + case 8: +#line 57 "parser.y" + { (yyval.node_val) = new_node(CompUnit, (yyvsp[0].node_val), NULL, (yyvsp[-1].node_val), 0, 0, NULL, NonType); } +#line 1674 "parser.c" + break; + + case 9: +#line 59 "parser.y" + { (yyval.node_val) = new_node(ConstDecl, NULL, NULL, (yyvsp[-1].node_val), 0, 0, NULL, Int); } +#line 1680 "parser.c" + break; + + case 10: +#line 60 "parser.y" + { (yyval.node_val) = new_node(ConstDecl, NULL, NULL, (yyvsp[-1].node_val), 0, 0, NULL, Float); } +#line 1686 "parser.c" + break; + + case 11: +#line 61 "parser.y" + { (yyval.node_val) = new_node(ConstDef, NULL, (yyvsp[-2].node_val), (yyvsp[0].node_val), 0, 0, (yyvsp[-3].str_val), NonType); } +#line 1692 "parser.c" + break; + + case 12: +#line 62 "parser.y" + { (yyval.node_val) = new_node(ConstDef, (yyvsp[0].node_val), (yyvsp[-4].node_val), (yyvsp[-2].node_val), 0, 0, (yyvsp[-5].str_val), NonType); } +#line 1698 "parser.c" + break; + + case 13: +#line 63 "parser.y" + { (yyval.node_val) = NULL; } +#line 1704 "parser.c" + break; + + case 14: +#line 64 "parser.y" + { (yyval.node_val) = new_node(ConstExpArray, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), 0, 0, NULL, NonType); } +#line 1710 "parser.c" + break; + + case 15: +#line 65 "parser.y" + { (yyval.node_val) = new_node(ConstInitVal, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 1716 "parser.c" + break; + + case 16: +#line 66 "parser.y" + { (yyval.node_val) = new_node(ConstInitVal, NULL, NULL, NULL, 0, 0, NULL, NonType); } +#line 1722 "parser.c" + break; + + case 17: +#line 67 "parser.y" + { (yyval.node_val) = new_node(ConstInitVal, NULL, NULL, (yyvsp[-1].node_val), 0, 0, NULL, NonType); } +#line 1728 "parser.c" + break; + + case 18: +#line 68 "parser.y" + { (yyval.node_val) = new_node(ConstInitVal, (yyvsp[-1].node_val), NULL, (yyvsp[-3].node_val), 0, 0, NULL, NonType); } +#line 1734 "parser.c" + break; + + case 19: +#line 69 "parser.y" + { (yyval.node_val) = new_node(ConstExp, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 1740 "parser.c" + break; + + case 20: +#line 70 "parser.y" + { (yyval.node_val) = new_node(ConstExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), PLUS, 0, NULL, NonType); } +#line 1746 "parser.c" + break; + + case 21: +#line 71 "parser.y" + { (yyval.node_val) = new_node(ConstExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), MINUS, 0, NULL, NonType); } +#line 1752 "parser.c" + break; + + case 22: +#line 73 "parser.y" + { (yyval.node_val) = new_node(VarDecl, NULL, NULL, (yyvsp[-1].node_val), 0, 0, NULL, Int); } +#line 1758 "parser.c" + break; + + case 23: +#line 74 "parser.y" + { (yyval.node_val) = new_node(VarDecl, NULL, NULL, (yyvsp[-1].node_val), 0, 0, NULL, Float); } +#line 1764 "parser.c" + break; + + case 24: +#line 75 "parser.y" + { (yyval.node_val) = new_node(VarDef, NULL, (yyvsp[0].node_val), NULL, 0, 0, (yyvsp[-1].str_val), NonType); } +#line 1770 "parser.c" + break; + + case 25: +#line 76 "parser.y" + { (yyval.node_val) = new_node(VarDef, NULL, (yyvsp[-2].node_val), (yyvsp[0].node_val), 0, 0, (yyvsp[-3].str_val), NonType); } +#line 1776 "parser.c" + break; + + case 26: +#line 77 "parser.y" + { (yyval.node_val) = new_node(VarDef, (yyvsp[0].node_val), (yyvsp[-2].node_val), NULL, 0, 0, (yyvsp[-3].str_val), NonType); } +#line 1782 "parser.c" + break; + + case 27: +#line 78 "parser.y" + { (yyval.node_val) = new_node(VarDef, (yyvsp[0].node_val), (yyvsp[-4].node_val), (yyvsp[-2].node_val), 0, 0, (yyvsp[-5].str_val), NonType); } +#line 1788 "parser.c" + break; + + case 28: +#line 79 "parser.y" + { (yyval.node_val) = new_node(InitVal, NULL, NULL, (yyvsp[0].node_val), Exp, 0, NULL, NonType); } +#line 1794 "parser.c" + break; + + case 29: +#line 80 "parser.y" + { (yyval.node_val) = new_node(InitVal, NULL, NULL, NULL, InitVals, 0, NULL, NonType); } +#line 1800 "parser.c" + break; + + case 30: +#line 81 "parser.y" + { (yyval.node_val) = new_node(InitVal, NULL, NULL, (yyvsp[-1].node_val), InitVals, 0, NULL, NonType); } +#line 1806 "parser.c" + break; + + case 31: +#line 82 "parser.y" + { (yyval.node_val) = new_node(InitVals, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 1812 "parser.c" + break; + + case 32: +#line 83 "parser.y" + { (yyval.node_val) = new_node(InitVals, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), 0, 0, NULL, NonType); } +#line 1818 "parser.c" + break; + + case 33: +#line 85 "parser.y" + { (yyval.node_val) = new_node(FuncDef, NULL, NULL, (yyvsp[0].node_val), 0, 0, (yyvsp[-3].str_val), Int); } +#line 1824 "parser.c" + break; + + case 34: +#line 86 "parser.y" + { (yyval.node_val) = new_node(FuncDef, NULL, NULL, (yyvsp[0].node_val), 0, 0, (yyvsp[-3].str_val), Float); } +#line 1830 "parser.c" + break; + + case 35: +#line 87 "parser.y" + { (yyval.node_val) = new_node(FuncDef, NULL, NULL, (yyvsp[0].node_val), 0, 0, (yyvsp[-3].str_val), Void); } +#line 1836 "parser.c" + break; + + case 36: +#line 88 "parser.y" + { (yyval.node_val) = new_node(FuncDef, NULL, (yyvsp[-2].node_val), (yyvsp[0].node_val), 0, 0, (yyvsp[-4].str_val), Int); } +#line 1842 "parser.c" + break; + + case 37: +#line 89 "parser.y" + { (yyval.node_val) = new_node(FuncDef, NULL, (yyvsp[-2].node_val), (yyvsp[0].node_val), 0, 0, (yyvsp[-4].str_val), Float); } +#line 1848 "parser.c" + break; + + case 38: +#line 90 "parser.y" + { (yyval.node_val) = new_node(FuncDef, NULL, (yyvsp[-2].node_val), (yyvsp[0].node_val), 0, 0, (yyvsp[-4].str_val), Void); } +#line 1854 "parser.c" + break; + + case 39: +#line 91 "parser.y" + { (yyval.node_val) = new_node(FuncFParam, NULL, NULL, NULL, 0, 0, (yyvsp[0].str_val), Int); } +#line 1860 "parser.c" + break; + + case 40: +#line 92 "parser.y" + { (yyval.node_val) = new_node(FuncFParam, NULL, NULL, NULL, 0, 0, (yyvsp[0].str_val), Float); } +#line 1866 "parser.c" + break; + + case 41: +#line 93 "parser.y" + { (yyval.node_val) = new_node(FuncFParam, NULL, NULL, (yyvsp[0].node_val), 0, 0, (yyvsp[-3].str_val), Int); } +#line 1872 "parser.c" + break; + + case 42: +#line 94 "parser.y" + { (yyval.node_val) = new_node(FuncFParam, NULL, NULL, (yyvsp[0].node_val), 0, 0, (yyvsp[-3].str_val), Float); } +#line 1878 "parser.c" + break; + + case 43: +#line 95 "parser.y" + { (yyval.node_val) = new_node(FuncFParam, (yyvsp[0].node_val), NULL, NULL, 0, 0, (yyvsp[-2].str_val), Int); } +#line 1884 "parser.c" + break; + + case 44: +#line 96 "parser.y" + { (yyval.node_val) = new_node(FuncFParam, (yyvsp[0].node_val), NULL, NULL, 0, 0, (yyvsp[-2].str_val), Float); } +#line 1890 "parser.c" + break; + + case 45: +#line 97 "parser.y" + { (yyval.node_val) = new_node(FuncFParam, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), 0, 0, (yyvsp[-5].str_val), Int); } +#line 1896 "parser.c" + break; + + case 46: +#line 98 "parser.y" + { (yyval.node_val) = new_node(FuncFParam, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), 0, 0, (yyvsp[-5].str_val), Float); } +#line 1902 "parser.c" + break; + + case 47: +#line 100 "parser.y" + { (yyval.node_val) = new_node(Block, NULL, NULL, (yyvsp[-1].node_val), 0, 0, NULL, NonType); } +#line 1908 "parser.c" + break; + + case 48: +#line 101 "parser.y" + { (yyval.node_val) = NULL; } +#line 1914 "parser.c" + break; + + case 49: +#line 102 "parser.y" + { (yyval.node_val) = new_node(BlockItem, (yyvsp[0].node_val), NULL, (yyvsp[-1].node_val), 0, 0, NULL, NonType); } +#line 1920 "parser.c" + break; + + case 50: +#line 103 "parser.y" + { (yyval.node_val) = new_node(BlockItem, (yyvsp[0].node_val), NULL, (yyvsp[-1].node_val), 0, 0, NULL, NonType); } +#line 1926 "parser.c" + break; + + case 51: +#line 104 "parser.y" + { (yyval.node_val) = new_node(BlockItem, (yyvsp[0].node_val), NULL, (yyvsp[-1].node_val), 0, 0, NULL, NonType); } +#line 1932 "parser.c" + break; + + case 52: +#line 107 "parser.y" + {(yyval.node_val) = new_node(Stmt,(yyvsp[-3].node_val),NULL,(yyvsp[-1].node_val),0,0,NULL,NonType);} +#line 1938 "parser.c" + break; + + case 53: +#line 108 "parser.y" + {(yyval.node_val) = new_node(Stmt,NULL,NULL,(yyvsp[-1].node_val),0,0,NULL,NonType);} +#line 1944 "parser.c" + break; + + case 54: +#line 109 "parser.y" + {(yyval.node_val) = NULL;} +#line 1950 "parser.c" + break; + + case 55: +#line 110 "parser.y" + {(yyval.node_val) = new_node(Stmt,NULL,NULL,(yyvsp[0].node_val),0,0,NULL,NonType);} +#line 1956 "parser.c" + break; + + case 56: +#line 111 "parser.y" + {(yyval.node_val) = new_node(Stmt,(yyvsp[-2].node_val),NULL,(yyvsp[0].node_val),0,0,NULL,NonType);} +#line 1962 "parser.c" + break; + + case 57: +#line 112 "parser.y" + {(yyval.node_val) = new_node(Stmt,(yyvsp[-4].node_val),(yyvsp[0].node_val),(yyvsp[-2].node_val),0,0,NULL,NonType);} +#line 1968 "parser.c" + break; + + case 58: +#line 113 "parser.y" + {(yyval.node_val) = new_node(Stmt,(yyvsp[-2].node_val),NULL,(yyvsp[0].node_val),0,0,NULL,NonType);} +#line 1974 "parser.c" + break; + + case 59: +#line 114 "parser.y" + {(yyval.node_val) = NULL;} +#line 1980 "parser.c" + break; + + case 60: +#line 115 "parser.y" + {(yyval.node_val) = NULL;} +#line 1986 "parser.c" + break; + + case 61: +#line 116 "parser.y" + {(yyval.node_val) = NULL;} +#line 1992 "parser.c" + break; + + case 62: +#line 117 "parser.y" + {(yyval.node_val) = new_node(Stmt,NULL,NULL,(yyvsp[-1].node_val),0,0,NULL,NonType);} +#line 1998 "parser.c" + break; + + case 63: +#line 122 "parser.y" + { (yyval.node_val) = new_node(Exp, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 2004 "parser.c" + break; + + case 64: +#line 123 "parser.y" + { (yyval.node_val) = new_node(AddExp, NULL, NULL, (yyvsp[0].node_val), MUL, 0, NULL, NonType); } +#line 2010 "parser.c" + break; + + case 65: +#line 124 "parser.y" + { (yyval.node_val) = new_node(AddExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), PLUS, 0, NULL, NonType); } +#line 2016 "parser.c" + break; + + case 66: +#line 125 "parser.y" + { (yyval.node_val) = new_node(AddExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), MINUS, 0, NULL, NonType); } +#line 2022 "parser.c" + break; + + case 67: +#line 126 "parser.y" + { (yyval.node_val) = new_node(MulExp, NULL, NULL, (yyvsp[0].node_val), UnaryExp, 0, NULL, NonType); } +#line 2028 "parser.c" + break; + + case 68: +#line 127 "parser.y" + { (yyval.node_val) = new_node(MulExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), MUL, 0, NULL, NonType); } +#line 2034 "parser.c" + break; + + case 69: +#line 128 "parser.y" + { (yyval.node_val) = new_node(MulExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), DIV, 0, NULL, NonType); } +#line 2040 "parser.c" + break; + + case 70: +#line 129 "parser.y" + { (yyval.node_val) = new_node(MulExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), MOD, 0, NULL, NonType); } +#line 2046 "parser.c" + break; + + case 71: +#line 130 "parser.y" + { (yyval.node_val) = new_node(UnaryExp, NULL, NULL, (yyvsp[0].node_val), PrimaryExp, 0, NULL, NonType); } +#line 2052 "parser.c" + break; + + case 72: +#line 131 "parser.y" + { (yyval.node_val) = new_node(UnaryExp, NULL, NULL, NULL, FuncRParams, 0, (yyvsp[-2].str_val), NonType); } +#line 2058 "parser.c" + break; + + case 73: +#line 132 "parser.y" + { (yyval.node_val) = new_node(UnaryExp, NULL, NULL, (yyvsp[-1].node_val), FuncRParams, 0, (yyvsp[-3].str_val), NonType); } +#line 2064 "parser.c" + break; + + case 74: +#line 133 "parser.y" + { (yyval.node_val) = new_node(UnaryExp, NULL, NULL, (yyvsp[0].node_val), Plus, 0, NULL, NonType); } +#line 2070 "parser.c" + break; + + case 75: +#line 134 "parser.y" + { (yyval.node_val) = new_node(UnaryExp, NULL, NULL, (yyvsp[0].node_val), Minus, 0, NULL, NonType); } +#line 2076 "parser.c" + break; + + case 76: +#line 135 "parser.y" + { (yyval.node_val) = new_node(UnaryExp, NULL, NULL, (yyvsp[0].node_val), NOT, 0, NULL, NonType); } +#line 2082 "parser.c" + break; + + case 77: +#line 136 "parser.y" + { (yyval.node_val) = new_node(FuncRParams, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 2088 "parser.c" + break; + + case 78: +#line 137 "parser.y" + { (yyval.node_val) = new_node(FuncRParams, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), 0, 0, NULL, NonType); } +#line 2094 "parser.c" + break; + + case 79: +#line 138 "parser.y" + { (yyval.node_val) = new_node(PrimaryExp, NULL, NULL, (yyvsp[-1].node_val), Exp, 0, NULL, NonType); } +#line 2100 "parser.c" + break; + + case 80: +#line 139 "parser.y" + { (yyval.node_val) = new_node(PrimaryExp, NULL, NULL, (yyvsp[0].node_val), LVal, 0, NULL, NonType); } +#line 2106 "parser.c" + break; + + case 81: +#line 140 "parser.y" + { (yyval.node_val) = new_node(PrimaryExp, NULL, NULL, NULL, (yyvsp[0].int_val), 0, NULL, Int); } +#line 2112 "parser.c" + break; + + case 82: +#line 141 "parser.y" + { (yyval.node_val) = new_node(PrimaryExp, NULL, NULL, NULL, 0, (yyvsp[0].float_val), NULL, Float); } +#line 2118 "parser.c" + break; + + case 83: +#line 142 "parser.y" + { (yyval.node_val) = new_node(LVal, NULL, NULL, (yyvsp[0].node_val), 0, 0, (yyvsp[-1].str_val), NonType); } +#line 2124 "parser.c" + break; + + case 84: +#line 144 "parser.y" + { (yyval.node_val) = new_node(Cond, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 2130 "parser.c" + break; + + case 85: +#line 145 "parser.y" + { (yyval.node_val) = new_node(Cond, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 2136 "parser.c" + break; + + case 86: +#line 146 "parser.y" + { (yyval.node_val) = new_node(Cond, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), OR, 0, 0, NonType); } +#line 2142 "parser.c" + break; + + case 87: +#line 147 "parser.y" + { (yyval.node_val) = new_node(Cond, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 2148 "parser.c" + break; + + case 88: +#line 149 "parser.y" + { (yyval.node_val) = new_node(LAndExp, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 2154 "parser.c" + break; + + case 89: +#line 150 "parser.y" + { (yyval.node_val) = new_node(LAndExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), AND, 0, NULL, NonType); } +#line 2160 "parser.c" + break; + + case 90: +#line 152 "parser.y" + { (yyval.node_val) = new_node(LNotExp, NULL, NULL, (yyvsp[-1].node_val), 0, 0, NULL, NonType);} +#line 2166 "parser.c" + break; + + case 91: +#line 154 "parser.y" + { (yyval.node_val) = new_node(EqExp, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType);} +#line 2172 "parser.c" + break; + + case 92: +#line 155 "parser.y" + { (yyval.node_val) = new_node(EqExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), EQ, 0, NULL, NonType); } +#line 2178 "parser.c" + break; + + case 93: +#line 156 "parser.y" + { (yyval.node_val) = new_node(EqExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), NE, 0, NULL, NonType); } +#line 2184 "parser.c" + break; + + case 94: +#line 158 "parser.y" + { (yyval.node_val) = new_node(RelExp, NULL, NULL, (yyvsp[0].node_val), 0, 0, NULL, NonType); } +#line 2190 "parser.c" + break; + + case 95: +#line 159 "parser.y" + { (yyval.node_val) = new_node(RelExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), LT, 0, NULL, NonType); } +#line 2196 "parser.c" + break; + + case 96: +#line 160 "parser.y" + { (yyval.node_val) = new_node(RelExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), GT, 0, NULL, NonType);} +#line 2202 "parser.c" + break; + + case 97: +#line 161 "parser.y" + { (yyval.node_val) = new_node(RelExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), LE, 0, NULL, NonType); } +#line 2208 "parser.c" + break; + + case 98: +#line 162 "parser.y" + { (yyval.node_val) = new_node(RelExp, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), GE, 0, NULL, NonType); } +#line 2214 "parser.c" + break; + + case 99: +#line 164 "parser.y" + { (yyval.node_val) = NULL; } +#line 2220 "parser.c" + break; + + case 100: +#line 165 "parser.y" + { (yyval.node_val) = new_node(ExpArray, (yyvsp[0].node_val), NULL, (yyvsp[-2].node_val), 0, 0, NULL, NonType); } +#line 2226 "parser.c" + break; + + +#line 2230 "parser.c" + + default: break; + } + /* User semantic actions sometimes alter yychar, and that requires + that yytoken be updated with the new translation. We take the + approach of translating immediately before every use of yytoken. + One alternative is translating here after every semantic action, + but that translation would be missed if the semantic action invokes + YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or + if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an + incorrect destructor might then be invoked immediately. In the + case of YYERROR or YYBACKUP, subsequent parser actions might lead + to an incorrect destructor call or verbose syntax error message + before the lookahead is translated. */ + YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc); + + YYPOPSTACK (yylen); + yylen = 0; + YY_STACK_PRINT (yyss, yyssp); + + *++yyvsp = yyval; + *++yylsp = yyloc; + + /* Now 'shift' the result of the reduction. Determine what state + that goes to, based on the state we popped back to and the rule + number reduced by. */ + { + const int yylhs = yyr1[yyn] - YYNTOKENS; + const int yyi = yypgoto[yylhs] + *yyssp; + yystate = (0 <= yyi && yyi <= YYLAST && yycheck[yyi] == *yyssp + ? yytable[yyi] + : yydefgoto[yylhs]); + } + + goto yynewstate; + + +/*--------------------------------------. +| yyerrlab -- here on detecting error. | +`--------------------------------------*/ +yyerrlab: + /* Make sure we have latest lookahead translation. See comments at + user semantic actions for why this is necessary. */ + yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar); + + /* If not already recovering from an error, report this error. */ + if (!yyerrstatus) + { + ++yynerrs; +#if ! YYERROR_VERBOSE + yyerror (YY_("syntax error")); +#else +# define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \ + yyssp, yytoken) + { + char const *yymsgp = YY_("syntax error"); + int yysyntax_error_status; + yysyntax_error_status = YYSYNTAX_ERROR; + if (yysyntax_error_status == 0) + yymsgp = yymsg; + else if (yysyntax_error_status == 1) + { + if (yymsg != yymsgbuf) + YYSTACK_FREE (yymsg); + yymsg = YY_CAST (char *, YYSTACK_ALLOC (YY_CAST (YYSIZE_T, yymsg_alloc))); + if (!yymsg) + { + yymsg = yymsgbuf; + yymsg_alloc = sizeof yymsgbuf; + yysyntax_error_status = 2; + } + else + { + yysyntax_error_status = YYSYNTAX_ERROR; + yymsgp = yymsg; + } + } + yyerror (yymsgp); + if (yysyntax_error_status == 2) + goto yyexhaustedlab; + } +# undef YYSYNTAX_ERROR +#endif + } + + yyerror_range[1] = yylloc; + + if (yyerrstatus == 3) + { + /* If just tried and failed to reuse lookahead token after an + error, discard it. */ + + if (yychar <= YYEOF) + { + /* Return failure if at end of input. */ + if (yychar == YYEOF) + YYABORT; + } + else + { + yydestruct ("Error: discarding", + yytoken, &yylval, &yylloc); + yychar = YYEMPTY; + } + } + + /* Else will try to reuse lookahead token after shifting the error + token. */ + goto yyerrlab1; + + +/*---------------------------------------------------. +| yyerrorlab -- error raised explicitly by YYERROR. | +`---------------------------------------------------*/ +yyerrorlab: + /* Pacify compilers when the user code never invokes YYERROR and the + label yyerrorlab therefore never appears in user code. */ + if (0) + YYERROR; + + /* Do not reclaim the symbols of the rule whose action triggered + this YYERROR. */ + YYPOPSTACK (yylen); + yylen = 0; + YY_STACK_PRINT (yyss, yyssp); + yystate = *yyssp; + goto yyerrlab1; + + +/*-------------------------------------------------------------. +| yyerrlab1 -- common code for both syntax error and YYERROR. | +`-------------------------------------------------------------*/ +yyerrlab1: + yyerrstatus = 3; /* Each real token shifted decrements this. */ + + for (;;) + { + yyn = yypact[yystate]; + if (!yypact_value_is_default (yyn)) + { + yyn += YYTERROR; + if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR) + { + yyn = yytable[yyn]; + if (0 < yyn) + break; + } + } + + /* Pop the current state because it cannot handle the error token. */ + if (yyssp == yyss) + YYABORT; + + yyerror_range[1] = *yylsp; + yydestruct ("Error: popping", + yystos[yystate], yyvsp, yylsp); + YYPOPSTACK (1); + yystate = *yyssp; + YY_STACK_PRINT (yyss, yyssp); + } + + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + *++yyvsp = yylval; + YY_IGNORE_MAYBE_UNINITIALIZED_END + + yyerror_range[2] = yylloc; + /* Using YYLLOC is tempting, but would change the location of + the lookahead. YYLOC is available though. */ + YYLLOC_DEFAULT (yyloc, yyerror_range, 2); + *++yylsp = yyloc; + + /* Shift the error token. */ + YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp); + + yystate = yyn; + goto yynewstate; + + +/*-------------------------------------. +| yyacceptlab -- YYACCEPT comes here. | +`-------------------------------------*/ +yyacceptlab: + yyresult = 0; + goto yyreturn; + + +/*-----------------------------------. +| yyabortlab -- YYABORT comes here. | +`-----------------------------------*/ +yyabortlab: + yyresult = 1; + goto yyreturn; + + +#if !defined yyoverflow || YYERROR_VERBOSE +/*-------------------------------------------------. +| yyexhaustedlab -- memory exhaustion comes here. | +`-------------------------------------------------*/ +yyexhaustedlab: + yyerror (YY_("memory exhausted")); + yyresult = 2; + /* Fall through. */ +#endif + + +/*-----------------------------------------------------. +| yyreturn -- parsing is finished, return the result. | +`-----------------------------------------------------*/ +yyreturn: + if (yychar != YYEMPTY) + { + /* Make sure we have latest lookahead translation. See comments at + user semantic actions for why this is necessary. */ + yytoken = YYTRANSLATE (yychar); + yydestruct ("Cleanup: discarding lookahead", + yytoken, &yylval, &yylloc); + } + /* Do not reclaim the symbols of the rule whose action triggered + this YYABORT or YYACCEPT. */ + YYPOPSTACK (yylen); + YY_STACK_PRINT (yyss, yyssp); + while (yyssp != yyss) + { + yydestruct ("Cleanup: popping", + yystos[+*yyssp], yyvsp, yylsp); + YYPOPSTACK (1); + } +#ifndef yyoverflow + if (yyss != yyssa) + YYSTACK_FREE (yyss); +#endif +#if YYERROR_VERBOSE + if (yymsg != yymsgbuf) + YYSTACK_FREE (yymsg); +#endif + return yyresult; +} +#line 166 "parser.y" + + +int main(int argc, char *argv[]) { + int index = strlen(argv[1]) - 1; + while(index > 0 && argv[1][index - 1] != '/') + index--; + strcpy(filename, argv[1] + index); + freopen(argv[1], "r", stdin); + yyparse(); + if (syntax_error == 0) + display(root); + return 0; +} + +/* +void yyerror(char *msg) { + printf("%s:%d\n", name, yylineno); + printf("error text: %s\n", yytext); + exit(-1); +} +*/ +#include +void yyerror(const char* fmt, ...) +{ + syntax_error = 1; + va_list ap; + va_start(ap, fmt); + fprintf(stderr, "%s:%d ", filename, yylineno); + vfprintf(stderr, fmt, ap); + fprintf(stderr, ".\n"); +} diff --git a/compiler/src/parser/parser.h b/compiler/src/parser/parser.h new file mode 100644 index 0000000..146062c --- /dev/null +++ b/compiler/src/parser/parser.h @@ -0,0 +1,131 @@ +/* A Bison parser, made by GNU Bison 3.5.1. */ + +/* Bison interface for Yacc-like parsers in C + + Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2020 Free Software Foundation, + Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + +/* Undocumented macros, especially those whose name start with YY_, + are private implementation details. Do not rely on them. */ + +#ifndef YY_YY_PARSER_H_INCLUDED +# define YY_YY_PARSER_H_INCLUDED +/* Debug traces. */ +#ifndef YYDEBUG +# define YYDEBUG 0 +#endif +#if YYDEBUG +extern int yydebug; +#endif + +/* Token type. */ +#ifndef YYTOKENTYPE +# define YYTOKENTYPE + enum yytokentype + { + ID = 258, + INT_LIT = 259, + FLOAT_LIT = 260, + INT = 261, + FLOAT = 262, + VOID = 263, + CONST = 264, + RETURN = 265, + IF = 266, + ELSE = 267, + WHILE = 268, + BREAK = 269, + CONTINUE = 270, + LP = 271, + RP = 272, + LB = 273, + RB = 274, + LC = 275, + RC = 276, + COMMA = 277, + SEMICOLON = 278, + MINUS = 279, + NOT = 280, + ASSIGN = 281, + PLUS = 282, + MUL = 283, + DIV = 284, + MOD = 285, + AND = 286, + OR = 287, + EQ = 288, + NE = 289, + LT = 290, + LE = 291, + GT = 292, + GE = 293, + LEX_ERR = 294, + THEN = 295 + }; +#endif + +/* Value type. */ +#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED +union YYSTYPE +{ +#line 25 "parser.y" + + int int_val; + float float_val; + char *str_val; + struct ASTNode *node_val; + +#line 105 "parser.h" + +}; +typedef union YYSTYPE YYSTYPE; +# define YYSTYPE_IS_TRIVIAL 1 +# define YYSTYPE_IS_DECLARED 1 +#endif + +/* Location type. */ +#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED +typedef struct YYLTYPE YYLTYPE; +struct YYLTYPE +{ + int first_line; + int first_column; + int last_line; + int last_column; +}; +# define YYLTYPE_IS_DECLARED 1 +# define YYLTYPE_IS_TRIVIAL 1 +#endif + + +extern YYSTYPE yylval; +extern YYLTYPE yylloc; +int yyparse (void); + +#endif /* !YY_YY_PARSER_H_INCLUDED */ diff --git a/compiler/src/parser/parser.ll b/compiler/src/parser/parser.ll new file mode 100644 index 0000000..ef7e30f --- /dev/null +++ b/compiler/src/parser/parser.ll @@ -0,0 +1,3320 @@ +; ModuleID = 'parser.c' +source_filename = "parser.c" +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-linux-gnu" + +%struct.YYLTYPE = type { i32, i32, i32, i32 } +%union.YYSTYPE = type { i8* } +%struct.ASTNode = type { i32, %struct.ASTNode*, %struct.ASTNode*, %struct.ASTNode*, i32, float, i8*, i32 } +%struct._IO_FILE = type { i32, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, %struct._IO_marker*, %struct._IO_FILE*, i32, i32, i64, i16, i8, [1 x i8], i8*, i64, %struct._IO_codecvt*, %struct._IO_wide_data*, %struct._IO_FILE*, i8*, i64, i32, [20 x i8] } +%struct._IO_marker = type opaque +%struct._IO_codecvt = type opaque +%struct._IO_wide_data = type opaque +%union.yyalloc = type { %union.YYSTYPE, [8 x i8] } +%struct.yypcontext_t = type { i8*, i32, %struct.YYLTYPE* } +%struct.__va_list_tag = type { i32, i32, i8*, i8* } + +@syntax_error = dso_local global i32 0, align 4 +@yylloc = dso_local global %struct.YYLTYPE { i32 1, i32 1, i32 1, i32 1 }, align 4 +@yychar = dso_local global i32 0, align 4 +@yypact = internal constant [207 x i16] [i16 112, i16 57, i16 85, i16 110, i16 94, i16 9, i16 -167, i16 112, i16 112, i16 112, i16 28, i16 93, i16 117, i16 126, i16 131, i16 155, i16 155, i16 -167, i16 -167, i16 -167, i16 -167, i16 64, i16 232, i16 -5, i16 -167, i16 98, i16 -167, i16 100, i16 144, i16 153, i16 164, i16 198, i16 207, i16 191, i16 195, i16 124, i16 -167, i16 -167, i16 232, i16 232, i16 232, i16 232, i16 196, i16 -8, i16 116, i16 -167, i16 -167, i16 214, i16 189, i16 191, i16 201, i16 191, i16 208, i16 200, i16 -167, i16 -167, i16 80, i16 104, i16 150, i16 -167, i16 191, i16 203, i16 232, i16 -167, i16 212, i16 -167, i16 6, i16 -167, i16 -167, i16 -167, i16 144, i16 232, i16 232, i16 232, i16 232, i16 232, i16 144, i16 -167, i16 70, i16 202, i16 -167, i16 -167, i16 191, i16 -167, i16 191, i16 218, i16 220, i16 165, i16 221, i16 165, i16 214, i16 214, i16 228, i16 225, i16 230, i16 224, i16 226, i16 -167, i16 150, i16 150, i16 -167, i16 229, i16 150, i16 231, i16 237, i16 -167, i16 -167, i16 236, i16 250, i16 249, i16 -167, i16 232, i16 232, i16 -167, i16 -167, i16 -167, i16 -167, i16 -167, i16 -167, i16 -167, i16 247, i16 251, i16 214, i16 -167, i16 -167, i16 107, i16 248, i16 -167, i16 253, i16 -167, i16 253, i16 -167, i16 -167, i16 252, i16 257, i16 257, i16 -167, i16 -167, i16 -167, i16 -167, i16 -167, i16 -167, i16 -167, i16 232, i16 232, i16 -167, i16 253, i16 -167, i16 -167, i16 189, i16 -167, i16 -167, i16 -167, i16 182, i16 155, i16 254, i16 256, i16 -167, i16 261, i16 101, i16 262, i16 -167, i16 242, i16 -167, i16 258, i16 163, i16 263, i16 260, i16 -167, i16 -167, i16 -167, i16 -167, i16 218, i16 -167, i16 165, i16 165, i16 232, i16 232, i16 232, i16 232, i16 232, i16 175, i16 257, i16 232, i16 232, i16 232, i16 175, i16 -167, i16 266, i16 -167, i16 -167, i16 12, i16 273, i16 -167, i16 -167, i16 -167, i16 -167, i16 279, i16 -167, i16 -167, i16 -167, i16 -167, i16 -167, i16 -167, i16 -167, i16 175, i16 -167], align 16 +@yytranslate = internal constant [296 x i8] c"\00\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F\10\11\12\13\14\15\16\17\18\19\1A\1B\1C\1D\1E\1F !\22#$%&'(", align 16 +@yycheck = internal constant [302 x i16] [i16 22, i16 58, i16 25, i16 38, i16 27, i16 16, i16 39, i16 40, i16 41, i16 0, i16 2, i16 111, i16 112, i16 48, i16 125, i16 181, i16 24, i16 22, i16 58, i16 27, i16 186, i16 26, i16 128, i16 176, i16 130, i16 28, i16 61, i16 62, i16 58, i16 17, i16 24, i16 184, i16 185, i16 27, i16 134, i16 135, i16 71, i16 72, i16 33, i16 205, i16 146, i16 98, i16 99, i16 78, i16 16, i16 102, i16 18, i16 35, i16 36, i16 37, i16 38, i16 73, i16 74, i16 75, i16 49, i16 47, i16 51, i16 92, i16 98, i16 99, i16 3, i16 172, i16 102, i16 85, i16 87, i16 60, i16 89, i16 70, i16 98, i16 99, i16 6, i16 7, i16 102, i16 3, i16 4, i16 5, i16 176, i16 177, i16 178, i16 179, i16 180, i16 17, i16 182, i16 183, i16 184, i16 185, i16 16, i16 82, i16 3, i16 84, i16 20, i16 21, i16 98, i16 99, i16 24, i16 25, i16 102, i16 27, i16 18, i16 91, i16 6, i16 7, i16 22, i16 125, i16 6, i16 7, i16 6, i16 7, i16 143, i16 144, i16 3, i16 4, i16 5, i16 3, i16 149, i16 17, i16 23, i16 17, i16 6, i16 7, i16 8, i16 9, i16 18, i16 16, i16 181, i16 158, i16 22, i16 20, i16 21, i16 186, i16 122, i16 24, i16 25, i16 16, i16 27, i16 18, i16 35, i16 36, i16 37, i16 38, i16 16, i16 176, i16 18, i16 154, i16 28, i16 29, i16 30, i16 16, i16 205, i16 23, i16 172, i16 174, i16 175, i16 3, i16 4, i16 5, i16 6, i16 7, i16 3, i16 9, i16 10, i16 11, i16 18, i16 13, i16 14, i16 15, i16 16, i16 7, i16 8, i16 9, i16 20, i16 6, i16 7, i16 23, i16 24, i16 25, i16 23, i16 27, i16 3, i16 4, i16 5, i16 177, i16 178, i16 179, i16 180, i16 10, i16 11, i16 23, i16 13, i16 14, i16 15, i16 16, i16 3, i16 4, i16 5, i16 20, i16 33, i16 34, i16 23, i16 24, i16 25, i16 3, i16 27, i16 21, i16 22, i16 16, i16 3, i16 4, i16 5, i16 20, i16 3, i16 20, i16 17, i16 24, i16 25, i16 19, i16 27, i16 3, i16 17, i16 16, i16 17, i16 3, i16 4, i16 5, i16 22, i16 17, i16 26, i16 24, i16 25, i16 17, i16 27, i16 3, i16 4, i16 5, i16 16, i16 3, i16 4, i16 5, i16 20, i16 19, i16 19, i16 16, i16 24, i16 25, i16 16, i16 27, i16 16, i16 23, i16 16, i16 23, i16 21, i16 23, i16 24, i16 25, i16 23, i16 27, i16 24, i16 25, i16 22, i16 27, i16 3, i16 4, i16 5, i16 26, i16 3, i16 4, i16 5, i16 17, i16 19, i16 22, i16 22, i16 18, i16 21, i16 16, i16 32, i16 23, i16 22, i16 16, i16 22, i16 17, i16 17, i16 24, i16 25, i16 23, i16 27, i16 24, i16 25, i16 21, i16 27, i16 31, i16 17, i16 12, i16 22, i16 48, i16 149, i16 144, i16 135, i16 182, i16 -1, i16 -1, i16 -1, i16 183], align 16 +@yytable = internal constant [302 x i16] [i16 43, i16 104, i16 50, i16 64, i16 52, i16 30, i16 67, i16 68, i16 69, i16 17, i16 13, i16 147, i16 148, i16 80, i16 153, i16 197, i16 71, i16 47, i16 98, i16 72, i16 202, i16 48, i16 155, i16 192, i16 156, i16 53, i16 107, i16 109, i16 99, i16 -63, i16 111, i16 200, i16 201, i16 112, i16 159, i16 159, i16 114, i16 115, i16 59, i16 206, i16 169, i16 104, i16 104, i16 80, i16 21, i16 104, i16 22, i16 177, i16 178, i16 179, i16 180, i16 116, i16 117, i16 118, i16 81, i16 77, i16 83, i16 133, i16 98, i16 98, i16 10, i16 188, i16 98, i16 43, i16 129, i16 105, i16 131, i16 113, i16 99, i16 99, i16 31, i16 32, i16 99, i16 35, i16 36, i16 37, i16 191, i16 159, i16 159, i16 159, i16 159, i16 33, i16 159, i16 159, i16 159, i16 159, i16 38, i16 123, i16 12, i16 124, i16 78, i16 119, i16 138, i16 139, i16 39, i16 40, i16 141, i16 41, i16 86, i16 13, i16 15, i16 16, i16 87, i16 43, i16 31, i16 32, i16 31, i16 32, i16 167, i16 107, i16 35, i16 36, i16 37, i16 14, i16 80, i16 49, i16 24, i16 51, i16 1, i16 2, i16 3, i16 4, i16 88, i16 38, i16 104, i16 68, i16 89, i16 125, i16 152, i16 104, i16 151, i16 39, i16 40, i16 25, i16 41, i16 22, i16 177, i16 178, i16 179, i16 180, i16 61, i16 64, i16 62, i16 173, i16 73, i16 74, i16 75, i16 27, i16 104, i16 26, i16 43, i16 189, i16 190, i16 35, i16 36, i16 37, i16 90, i16 91, i16 28, i16 4, i16 92, i16 93, i16 22, i16 94, i16 95, i16 96, i16 38, i16 18, i16 19, i16 20, i16 58, i16 31, i16 32, i16 97, i16 39, i16 40, i16 54, i16 41, i16 35, i16 36, i16 37, i16 193, i16 194, i16 195, i16 196, i16 92, i16 93, i16 55, i16 94, i16 95, i16 96, i16 38, i16 35, i16 36, i16 37, i16 58, i16 184, i16 185, i16 97, i16 39, i16 40, i16 56, i16 41, i16 171, i16 172, i16 38, i16 35, i16 36, i16 37, i16 78, i16 57, i16 58, i16 60, i16 39, i16 40, i16 70, i16 41, i16 76, i16 82, i16 38, i16 106, i16 35, i16 36, i16 37, i16 122, i16 84, i16 85, i16 39, i16 40, i16 110, i16 41, i16 35, i16 36, i16 37, i16 38, i16 35, i16 36, i16 37, i16 125, i16 128, i16 130, i16 134, i16 39, i16 40, i16 38, i16 41, i16 135, i16 136, i16 38, i16 137, i16 140, i16 132, i16 39, i16 40, i16 142, i16 41, i16 39, i16 40, i16 144, i16 41, i16 35, i16 36, i16 37, i16 143, i16 35, i16 36, i16 37, i16 145, i16 146, i16 149, i16 154, i16 62, i16 150, i16 38, i16 182, i16 157, i16 174, i16 176, i16 175, i16 181, i16 186, i16 39, i16 158, i16 187, i16 41, i16 39, i16 40, i16 203, i16 41, i16 183, i16 204, i16 205, i16 42, i16 79, i16 170, i16 168, i16 166, i16 198, i16 0, i16 0, i16 0, i16 199], align 16 +@yylval = dso_local global %union.YYSTYPE zeroinitializer, align 8 +@yydefact = internal constant [207 x i8] c"\00\00\00\00\00\00\02\03\04\05\0D\00\0D\00\00\00\00\01\06\07\08\00\00\18\16\00\17\00\0D\00\00\00\00\00\00cQR\00\00\00\00\00\13CGP\00\00\00\00\00\00\00\09\0A'(0!\00\00\00S\00?@KLJ\0D\00\00\00\00\00\0D\1A\00\19\1C\22\00#\00\00\00\00\00\00\00\00\00\00\00\00\006007\000\00P$HM\00\00O\00\00\0E\15\14DEF\1D\1F\00\00%&\00\0B\0Fc+c,=\00\00\00;<12/35\00\00IcBA\00\1E\1B\10\00\00)*>\00^\00TUWX[\00\00Nd \11\00\0C\00\00\00\00\00\00\00\00\00\00\00\00\004\00-.^\00_a`b8VY\\]:\12Z\009", align 16 +@yyr2 = internal constant [101 x i8] c"\00\02\01\01\01\01\02\02\02\04\04\04\06\00\04\01\02\03\05\01\03\03\03\03\02\04\04\06\01\02\03\01\03\05\05\05\06\06\06\02\02\05\05\04\04\07\07\03\00\02\02\02\04\02\01\01\05\07\05\02\02\02\03\01\01\03\03\01\03\03\03\01\03\04\02\02\02\01\03\03\01\01\01\02\01\01\03\01\01\03\04\01\03\03\01\03\03\03\03\00\04", align 16 +@root = dso_local global %struct.ASTNode* null, align 8 +@yyr1 = internal constant [101 x i8] c"\00)*++++++,,--..////00011222233344555555666666667888899999999999:;;;<<<<======>>????@ABBBCCDEEEFFFFFGG", align 16 +@yypgoto = internal constant [31 x i16] [i16 -167, i16 -167, i16 160, i16 -40, i16 -11, i16 -3, i16 -111, i16 270, i16 -30, i16 8, i16 245, i16 145, i16 -167, i16 -23, i16 5, i16 -6, i16 -166, i16 -35, i16 -100, i16 -22, i16 -33, i16 151, i16 -167, i16 -57, i16 161, i16 115, i16 118, i16 -167, i16 -153, i16 4, i16 -106], align 16 +@yydefgoto = internal constant [31 x i8] c"\00\05\06\07\1D\17~\7F\08\0Bxy\09\22defgAB,l-.\A0\A1\A2\A3\A4\A5?", align 16 +@yynerrs = dso_local global i32 0, align 4 +@.str = private unnamed_addr constant [13 x i8] c"syntax error\00", align 1 +@.str.1 = private unnamed_addr constant [18 x i8] c"Error: discarding\00", align 1 +@.str.2 = private unnamed_addr constant [15 x i8] c"Error: popping\00", align 1 +@yystos = internal constant [207 x i8] c"\00\06\07\08\09*+,15\032\032\03\06\07\00+++\10\12.\17\10\17\10\03--\06\07\116\03\04\05\10\18\19\1B0<=?@\16\1A\116\116.\17\17\03\03\147\11\10\12G:;<===\13\18\1B\1C\1D\1E\032\143:7\117\11\1A\12\16\12\16\06\07\0A\0B\0D\0E\0F\17,1789:@7\11:>:\11\18\1B.::<<<\1534\1677\14/0\136\136\17:\10\10\17\1788\158\17\1A\16\11\13;;\16\152\15/\16GG\17\19;ABCDEFA:>G4\15\16-\16\16\10#$%&\11 \1F!\22\11\17/66;EFFFF9BCEE9\15\11\0C9", align 16 +@.str.3 = private unnamed_addr constant [17 x i8] c"memory exhausted\00", align 1 +@.str.4 = private unnamed_addr constant [30 x i8] c"Cleanup: discarding lookahead\00", align 1 +@.str.5 = private unnamed_addr constant [17 x i8] c"Cleanup: popping\00", align 1 +@filename = dso_local global [100 x i8] zeroinitializer, align 16 +@.str.6 = private unnamed_addr constant [2 x i8] c"r\00", align 1 +@stdin = external global %struct._IO_FILE*, align 8 +@stderr = external global %struct._IO_FILE*, align 8 +@.str.7 = private unnamed_addr constant [7 x i8] c"%s:%d \00", align 1 +@yylineno = external global i32, align 4 +@.str.8 = private unnamed_addr constant [3 x i8] c".\0A\00", align 1 +@.str.9 = private unnamed_addr constant [28 x i8] c"syntax error, unexpected %s\00", align 1 +@.str.10 = private unnamed_addr constant [42 x i8] c"syntax error, unexpected %s, expecting %s\00", align 1 +@.str.11 = private unnamed_addr constant [48 x i8] c"syntax error, unexpected %s, expecting %s or %s\00", align 1 +@.str.12 = private unnamed_addr constant [54 x i8] c"syntax error, unexpected %s, expecting %s or %s or %s\00", align 1 +@.str.13 = private unnamed_addr constant [60 x i8] c"syntax error, unexpected %s, expecting %s or %s or %s or %s\00", align 1 +@yytname = internal constant [73 x i8*] [i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.14, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.15, i32 0, i32 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.16, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.17, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.18, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.19, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.20, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.21, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.22, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.23, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.24, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.25, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.26, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.27, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.28, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.29, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.30, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.31, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.32, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.33, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.34, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.35, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.36, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.37, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.38, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.39, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.40, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.41, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.42, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.43, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.44, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.45, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.46, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.47, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.48, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.49, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.50, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.51, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.52, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.53, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.54, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.55, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.56, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.57, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.58, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.59, i32 0, i32 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.60, i32 0, i32 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.61, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.62, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.63, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.64, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.65, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.66, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.67, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.68, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.69, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.70, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.71, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.72, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.73, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.74, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.75, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.76, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.77, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.78, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.79, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.80, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.81, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.82, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.83, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.84, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.85, i32 0, i32 0), i8* null], align 16 +@.str.14 = private unnamed_addr constant [14 x i8] c"\22end of file\22\00", align 1 +@.str.15 = private unnamed_addr constant [6 x i8] c"error\00", align 1 +@.str.16 = private unnamed_addr constant [16 x i8] c"\22invalid token\22\00", align 1 +@.str.17 = private unnamed_addr constant [3 x i8] c"ID\00", align 1 +@.str.18 = private unnamed_addr constant [8 x i8] c"INT_LIT\00", align 1 +@.str.19 = private unnamed_addr constant [10 x i8] c"FLOAT_LIT\00", align 1 +@.str.20 = private unnamed_addr constant [4 x i8] c"INT\00", align 1 +@.str.21 = private unnamed_addr constant [6 x i8] c"FLOAT\00", align 1 +@.str.22 = private unnamed_addr constant [5 x i8] c"VOID\00", align 1 +@.str.23 = private unnamed_addr constant [6 x i8] c"CONST\00", align 1 +@.str.24 = private unnamed_addr constant [7 x i8] c"RETURN\00", align 1 +@.str.25 = private unnamed_addr constant [3 x i8] c"IF\00", align 1 +@.str.26 = private unnamed_addr constant [5 x i8] c"ELSE\00", align 1 +@.str.27 = private unnamed_addr constant [6 x i8] c"WHILE\00", align 1 +@.str.28 = private unnamed_addr constant [6 x i8] c"BREAK\00", align 1 +@.str.29 = private unnamed_addr constant [9 x i8] c"CONTINUE\00", align 1 +@.str.30 = private unnamed_addr constant [3 x i8] c"LP\00", align 1 +@.str.31 = private unnamed_addr constant [3 x i8] c"RP\00", align 1 +@.str.32 = private unnamed_addr constant [3 x i8] c"LB\00", align 1 +@.str.33 = private unnamed_addr constant [3 x i8] c"RB\00", align 1 +@.str.34 = private unnamed_addr constant [3 x i8] c"LC\00", align 1 +@.str.35 = private unnamed_addr constant [3 x i8] c"RC\00", align 1 +@.str.36 = private unnamed_addr constant [6 x i8] c"COMMA\00", align 1 +@.str.37 = private unnamed_addr constant [10 x i8] c"SEMICOLON\00", align 1 +@.str.38 = private unnamed_addr constant [6 x i8] c"MINUS\00", align 1 +@.str.39 = private unnamed_addr constant [4 x i8] c"NOT\00", align 1 +@.str.40 = private unnamed_addr constant [7 x i8] c"ASSIGN\00", align 1 +@.str.41 = private unnamed_addr constant [5 x i8] c"PLUS\00", align 1 +@.str.42 = private unnamed_addr constant [4 x i8] c"MUL\00", align 1 +@.str.43 = private unnamed_addr constant [4 x i8] c"DIV\00", align 1 +@.str.44 = private unnamed_addr constant [4 x i8] c"MOD\00", align 1 +@.str.45 = private unnamed_addr constant [4 x i8] c"AND\00", align 1 +@.str.46 = private unnamed_addr constant [3 x i8] c"OR\00", align 1 +@.str.47 = private unnamed_addr constant [3 x i8] c"EQ\00", align 1 +@.str.48 = private unnamed_addr constant [3 x i8] c"NE\00", align 1 +@.str.49 = private unnamed_addr constant [3 x i8] c"LT\00", align 1 +@.str.50 = private unnamed_addr constant [3 x i8] c"LE\00", align 1 +@.str.51 = private unnamed_addr constant [3 x i8] c"GT\00", align 1 +@.str.52 = private unnamed_addr constant [3 x i8] c"GE\00", align 1 +@.str.53 = private unnamed_addr constant [8 x i8] c"LEX_ERR\00", align 1 +@.str.54 = private unnamed_addr constant [5 x i8] c"THEN\00", align 1 +@.str.55 = private unnamed_addr constant [8 x i8] c"$accept\00", align 1 +@.str.56 = private unnamed_addr constant [5 x i8] c"Root\00", align 1 +@.str.57 = private unnamed_addr constant [9 x i8] c"CompUnit\00", align 1 +@.str.58 = private unnamed_addr constant [10 x i8] c"ConstDecl\00", align 1 +@.str.59 = private unnamed_addr constant [9 x i8] c"ConstDef\00", align 1 +@.str.60 = private unnamed_addr constant [14 x i8] c"ConstExpArray\00", align 1 +@.str.61 = private unnamed_addr constant [13 x i8] c"ConstInitVal\00", align 1 +@.str.62 = private unnamed_addr constant [9 x i8] c"ConstExp\00", align 1 +@.str.63 = private unnamed_addr constant [8 x i8] c"VarDecl\00", align 1 +@.str.64 = private unnamed_addr constant [7 x i8] c"VarDef\00", align 1 +@.str.65 = private unnamed_addr constant [8 x i8] c"InitVal\00", align 1 +@.str.66 = private unnamed_addr constant [9 x i8] c"InitVals\00", align 1 +@.str.67 = private unnamed_addr constant [8 x i8] c"FuncDef\00", align 1 +@.str.68 = private unnamed_addr constant [11 x i8] c"FuncFParam\00", align 1 +@.str.69 = private unnamed_addr constant [6 x i8] c"Block\00", align 1 +@.str.70 = private unnamed_addr constant [10 x i8] c"BlockItem\00", align 1 +@.str.71 = private unnamed_addr constant [5 x i8] c"Stmt\00", align 1 +@.str.72 = private unnamed_addr constant [4 x i8] c"Exp\00", align 1 +@.str.73 = private unnamed_addr constant [7 x i8] c"AddExp\00", align 1 +@.str.74 = private unnamed_addr constant [7 x i8] c"MulExp\00", align 1 +@.str.75 = private unnamed_addr constant [9 x i8] c"UnaryExp\00", align 1 +@.str.76 = private unnamed_addr constant [12 x i8] c"FuncRParams\00", align 1 +@.str.77 = private unnamed_addr constant [11 x i8] c"PrimaryExp\00", align 1 +@.str.78 = private unnamed_addr constant [5 x i8] c"LVal\00", align 1 +@.str.79 = private unnamed_addr constant [5 x i8] c"Cond\00", align 1 +@.str.80 = private unnamed_addr constant [7 x i8] c"LOrExp\00", align 1 +@.str.81 = private unnamed_addr constant [8 x i8] c"LAndExp\00", align 1 +@.str.82 = private unnamed_addr constant [8 x i8] c"LNotExp\00", align 1 +@.str.83 = private unnamed_addr constant [6 x i8] c"EqExp\00", align 1 +@.str.84 = private unnamed_addr constant [7 x i8] c"RelExp\00", align 1 +@.str.85 = private unnamed_addr constant [9 x i8] c"ExpArray\00", align 1 +@.str.86 = private unnamed_addr constant [9 x i8] c"Deleting\00", align 1 + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @yyparse() #0 { + %1 = alloca i32, align 4 + %2 = alloca i32, align 4 + %3 = alloca i64, align 8 + %4 = alloca [200 x i8], align 16 + %5 = alloca i8*, align 8 + %6 = alloca i8*, align 8 + %7 = alloca [200 x %union.YYSTYPE], align 16 + %8 = alloca %union.YYSTYPE*, align 8 + %9 = alloca %union.YYSTYPE*, align 8 + %10 = alloca [200 x %struct.YYLTYPE], align 16 + %11 = alloca %struct.YYLTYPE*, align 8 + %12 = alloca %struct.YYLTYPE*, align 8 + %13 = alloca i32, align 4 + %14 = alloca i32, align 4 + %15 = alloca i32, align 4 + %16 = alloca %union.YYSTYPE, align 8 + %17 = alloca %struct.YYLTYPE, align 4 + %18 = alloca [3 x %struct.YYLTYPE], align 16 + %19 = alloca [128 x i8], align 16 + %20 = alloca i8*, align 8 + %21 = alloca i64, align 8 + %22 = alloca i32, align 4 + %23 = alloca i64, align 8 + %24 = alloca i8*, align 8 + %25 = alloca %union.yyalloc*, align 8 + %26 = alloca i64, align 8 + %27 = alloca i64, align 8 + %28 = alloca i64, align 8 + %29 = alloca i32, align 4 + %30 = alloca i32, align 4 + %31 = alloca %struct.yypcontext_t, align 8 + %32 = alloca i8*, align 8 + %33 = alloca i32, align 4 + store i32 0, i32* %1, align 4 + store i32 0, i32* %2, align 4 + store i64 200, i64* %3, align 8 + %34 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0 + store i8* %34, i8** %5, align 8 + %35 = load i8*, i8** %5, align 8 + store i8* %35, i8** %6, align 8 + %36 = getelementptr inbounds [200 x %union.YYSTYPE], [200 x %union.YYSTYPE]* %7, i64 0, i64 0 + store %union.YYSTYPE* %36, %union.YYSTYPE** %8, align 8 + %37 = load %union.YYSTYPE*, %union.YYSTYPE** %8, align 8 + store %union.YYSTYPE* %37, %union.YYSTYPE** %9, align 8 + %38 = getelementptr inbounds [200 x %struct.YYLTYPE], [200 x %struct.YYLTYPE]* %10, i64 0, i64 0 + store %struct.YYLTYPE* %38, %struct.YYLTYPE** %11, align 8 + %39 = load %struct.YYLTYPE*, %struct.YYLTYPE** %11, align 8 + store %struct.YYLTYPE* %39, %struct.YYLTYPE** %12, align 8 + store i32 -2, i32* %15, align 4 + %40 = getelementptr inbounds [128 x i8], [128 x i8]* %19, i64 0, i64 0 + store i8* %40, i8** %20, align 8 + store i64 128, i64* %21, align 8 + store i32 0, i32* %22, align 4 + store i32 -2, i32* @yychar, align 4 + %41 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %42 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %41, i64 0 + %43 = bitcast %struct.YYLTYPE* %42 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %43, i8* align 4 bitcast (%struct.YYLTYPE* @yylloc to i8*), i64 16, i1 false) + br label %47 + +44: ; preds = %1505, %1312, %242 + %45 = load i8*, i8** %6, align 8 + %46 = getelementptr inbounds i8, i8* %45, i32 1 + store i8* %46, i8** %6, align 8 + br label %47 + +47: ; preds = %44, %0 + %48 = load i32, i32* %1, align 4 + %49 = trunc i32 %48 to i8 + %50 = load i8*, i8** %6, align 8 + store i8 %49, i8* %50, align 1 + %51 = load i8*, i8** %5, align 8 + %52 = load i64, i64* %3, align 8 + %53 = getelementptr inbounds i8, i8* %51, i64 %52 + %54 = getelementptr inbounds i8, i8* %53, i64 -1 + %55 = load i8*, i8** %6, align 8 + %56 = icmp ule i8* %54, %55 + br i1 %56, label %57, label %162 + +57: ; preds = %47 + %58 = load i8*, i8** %6, align 8 + %59 = load i8*, i8** %5, align 8 + %60 = ptrtoint i8* %58 to i64 + %61 = ptrtoint i8* %59 to i64 + %62 = sub i64 %60, %61 + %63 = add nsw i64 %62, 1 + store i64 %63, i64* %23, align 8 + %64 = load i64, i64* %3, align 8 + %65 = icmp sle i64 10000, %64 + br i1 %65, label %66, label %67 + +66: ; preds = %57 + br label %1509 + +67: ; preds = %57 + %68 = load i64, i64* %3, align 8 + %69 = mul nsw i64 %68, 2 + store i64 %69, i64* %3, align 8 + %70 = load i64, i64* %3, align 8 + %71 = icmp slt i64 10000, %70 + br i1 %71, label %72, label %73 + +72: ; preds = %67 + store i64 10000, i64* %3, align 8 + br label %73 + +73: ; preds = %72, %67 + %74 = load i8*, i8** %5, align 8 + store i8* %74, i8** %24, align 8 + %75 = load i64, i64* %3, align 8 + %76 = mul nsw i64 %75, 25 + %77 = add nsw i64 %76, 30 + %78 = call noalias i8* @malloc(i64 noundef %77) #6 + %79 = bitcast i8* %78 to %union.yyalloc* + store %union.yyalloc* %79, %union.yyalloc** %25, align 8 + %80 = load %union.yyalloc*, %union.yyalloc** %25, align 8 + %81 = icmp ne %union.yyalloc* %80, null + br i1 %81, label %83, label %82 + +82: ; preds = %73 + br label %1509 + +83: ; preds = %73 + br label %84 + +84: ; preds = %83 + %85 = load %union.yyalloc*, %union.yyalloc** %25, align 8 + %86 = bitcast %union.yyalloc* %85 to i8* + %87 = load i8*, i8** %5, align 8 + %88 = load i64, i64* %23, align 8 + %89 = mul i64 %88, 1 + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %86, i8* align 1 %87, i64 %89, i1 false) + %90 = load %union.yyalloc*, %union.yyalloc** %25, align 8 + %91 = bitcast %union.yyalloc* %90 to i8* + store i8* %91, i8** %5, align 8 + %92 = load i64, i64* %3, align 8 + %93 = mul nsw i64 %92, 1 + %94 = add nsw i64 %93, 15 + store i64 %94, i64* %26, align 8 + %95 = load i64, i64* %26, align 8 + %96 = sdiv i64 %95, 16 + %97 = load %union.yyalloc*, %union.yyalloc** %25, align 8 + %98 = getelementptr inbounds %union.yyalloc, %union.yyalloc* %97, i64 %96 + store %union.yyalloc* %98, %union.yyalloc** %25, align 8 + br label %99 + +99: ; preds = %84 + br label %100 + +100: ; preds = %99 + %101 = load %union.yyalloc*, %union.yyalloc** %25, align 8 + %102 = bitcast %union.yyalloc* %101 to %union.YYSTYPE* + %103 = bitcast %union.YYSTYPE* %102 to i8* + %104 = load %union.YYSTYPE*, %union.YYSTYPE** %8, align 8 + %105 = bitcast %union.YYSTYPE* %104 to i8* + %106 = load i64, i64* %23, align 8 + %107 = mul i64 %106, 8 + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %103, i8* align 8 %105, i64 %107, i1 false) + %108 = load %union.yyalloc*, %union.yyalloc** %25, align 8 + %109 = bitcast %union.yyalloc* %108 to %union.YYSTYPE* + store %union.YYSTYPE* %109, %union.YYSTYPE** %8, align 8 + %110 = load i64, i64* %3, align 8 + %111 = mul nsw i64 %110, 8 + %112 = add nsw i64 %111, 15 + store i64 %112, i64* %27, align 8 + %113 = load i64, i64* %27, align 8 + %114 = sdiv i64 %113, 16 + %115 = load %union.yyalloc*, %union.yyalloc** %25, align 8 + %116 = getelementptr inbounds %union.yyalloc, %union.yyalloc* %115, i64 %114 + store %union.yyalloc* %116, %union.yyalloc** %25, align 8 + br label %117 + +117: ; preds = %100 + br label %118 + +118: ; preds = %117 + %119 = load %union.yyalloc*, %union.yyalloc** %25, align 8 + %120 = bitcast %union.yyalloc* %119 to %struct.YYLTYPE* + %121 = bitcast %struct.YYLTYPE* %120 to i8* + %122 = load %struct.YYLTYPE*, %struct.YYLTYPE** %11, align 8 + %123 = bitcast %struct.YYLTYPE* %122 to i8* + %124 = load i64, i64* %23, align 8 + %125 = mul i64 %124, 16 + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %121, i8* align 4 %123, i64 %125, i1 false) + %126 = load %union.yyalloc*, %union.yyalloc** %25, align 8 + %127 = bitcast %union.yyalloc* %126 to %struct.YYLTYPE* + store %struct.YYLTYPE* %127, %struct.YYLTYPE** %11, align 8 + %128 = load i64, i64* %3, align 8 + %129 = mul nsw i64 %128, 16 + %130 = add nsw i64 %129, 15 + store i64 %130, i64* %28, align 8 + %131 = load i64, i64* %28, align 8 + %132 = sdiv i64 %131, 16 + %133 = load %union.yyalloc*, %union.yyalloc** %25, align 8 + %134 = getelementptr inbounds %union.yyalloc, %union.yyalloc* %133, i64 %132 + store %union.yyalloc* %134, %union.yyalloc** %25, align 8 + br label %135 + +135: ; preds = %118 + %136 = load i8*, i8** %24, align 8 + %137 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0 + %138 = icmp ne i8* %136, %137 + br i1 %138, label %139, label %141 + +139: ; preds = %135 + %140 = load i8*, i8** %24, align 8 + call void @free(i8* noundef %140) #6 + br label %141 + +141: ; preds = %139, %135 + %142 = load i8*, i8** %5, align 8 + %143 = load i64, i64* %23, align 8 + %144 = getelementptr inbounds i8, i8* %142, i64 %143 + %145 = getelementptr inbounds i8, i8* %144, i64 -1 + store i8* %145, i8** %6, align 8 + %146 = load %union.YYSTYPE*, %union.YYSTYPE** %8, align 8 + %147 = load i64, i64* %23, align 8 + %148 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %146, i64 %147 + %149 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %148, i64 -1 + store %union.YYSTYPE* %149, %union.YYSTYPE** %9, align 8 + %150 = load %struct.YYLTYPE*, %struct.YYLTYPE** %11, align 8 + %151 = load i64, i64* %23, align 8 + %152 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %150, i64 %151 + %153 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %152, i64 -1 + store %struct.YYLTYPE* %153, %struct.YYLTYPE** %12, align 8 + %154 = load i8*, i8** %5, align 8 + %155 = load i64, i64* %3, align 8 + %156 = getelementptr inbounds i8, i8* %154, i64 %155 + %157 = getelementptr inbounds i8, i8* %156, i64 -1 + %158 = load i8*, i8** %6, align 8 + %159 = icmp ule i8* %157, %158 + br i1 %159, label %160, label %161 + +160: ; preds = %141 + br label %1508 + +161: ; preds = %141 + br label %162 + +162: ; preds = %161, %47 + %163 = load i32, i32* %1, align 4 + %164 = icmp eq i32 %163, 17 + br i1 %164, label %165, label %166 + +165: ; preds = %162 + br label %1507 + +166: ; preds = %162 + br label %167 + +167: ; preds = %166 + %168 = load i32, i32* %1, align 4 + %169 = sext i32 %168 to i64 + %170 = getelementptr inbounds [207 x i16], [207 x i16]* @yypact, i64 0, i64 %169 + %171 = load i16, i16* %170, align 2 + %172 = sext i16 %171 to i32 + store i32 %172, i32* %13, align 4 + %173 = load i32, i32* %13, align 4 + %174 = icmp eq i32 %173, -167 + br i1 %174, label %175, label %176 + +175: ; preds = %167 + br label %250 + +176: ; preds = %167 + %177 = load i32, i32* @yychar, align 4 + %178 = icmp eq i32 %177, -2 + br i1 %178, label %179, label %181 + +179: ; preds = %176 + %180 = call i32 (...) @yylex() + store i32 %180, i32* @yychar, align 4 + br label %181 + +181: ; preds = %179, %176 + %182 = load i32, i32* @yychar, align 4 + %183 = icmp sle i32 %182, 0 + br i1 %183, label %184, label %185 + +184: ; preds = %181 + store i32 0, i32* @yychar, align 4 + store i32 0, i32* %15, align 4 + br label %207 + +185: ; preds = %181 + %186 = load i32, i32* @yychar, align 4 + %187 = icmp eq i32 %186, 256 + br i1 %187, label %188, label %191 + +188: ; preds = %185 + store i32 257, i32* @yychar, align 4 + store i32 1, i32* %15, align 4 + %189 = getelementptr inbounds [3 x %struct.YYLTYPE], [3 x %struct.YYLTYPE]* %18, i64 0, i64 1 + %190 = bitcast %struct.YYLTYPE* %189 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 %190, i8* align 4 bitcast (%struct.YYLTYPE* @yylloc to i8*), i64 16, i1 false) + br label %1415 + +191: ; preds = %185 + %192 = load i32, i32* @yychar, align 4 + %193 = icmp sle i32 0, %192 + br i1 %193, label %194, label %203 + +194: ; preds = %191 + %195 = load i32, i32* @yychar, align 4 + %196 = icmp sle i32 %195, 295 + br i1 %196, label %197, label %203 + +197: ; preds = %194 + %198 = load i32, i32* @yychar, align 4 + %199 = sext i32 %198 to i64 + %200 = getelementptr inbounds [296 x i8], [296 x i8]* @yytranslate, i64 0, i64 %199 + %201 = load i8, i8* %200, align 1 + %202 = sext i8 %201 to i32 + br label %204 + +203: ; preds = %194, %191 + br label %204 + +204: ; preds = %203, %197 + %205 = phi i32 [ %202, %197 ], [ 2, %203 ] + store i32 %205, i32* %15, align 4 + br label %206 + +206: ; preds = %204 + br label %207 + +207: ; preds = %206, %184 + %208 = load i32, i32* %15, align 4 + %209 = load i32, i32* %13, align 4 + %210 = add nsw i32 %209, %208 + store i32 %210, i32* %13, align 4 + %211 = load i32, i32* %13, align 4 + %212 = icmp slt i32 %211, 0 + br i1 %212, label %224, label %213 + +213: ; preds = %207 + %214 = load i32, i32* %13, align 4 + %215 = icmp slt i32 301, %214 + br i1 %215, label %224, label %216 + +216: ; preds = %213 + %217 = load i32, i32* %13, align 4 + %218 = sext i32 %217 to i64 + %219 = getelementptr inbounds [302 x i16], [302 x i16]* @yycheck, i64 0, i64 %218 + %220 = load i16, i16* %219, align 2 + %221 = sext i16 %220 to i32 + %222 = load i32, i32* %15, align 4 + %223 = icmp ne i32 %221, %222 + br i1 %223, label %224, label %225 + +224: ; preds = %216, %213, %207 + br label %250 + +225: ; preds = %216 + %226 = load i32, i32* %13, align 4 + %227 = sext i32 %226 to i64 + %228 = getelementptr inbounds [302 x i16], [302 x i16]* @yytable, i64 0, i64 %227 + %229 = load i16, i16* %228, align 2 + %230 = sext i16 %229 to i32 + store i32 %230, i32* %13, align 4 + %231 = load i32, i32* %13, align 4 + %232 = icmp sle i32 %231, 0 + br i1 %232, label %233, label %236 + +233: ; preds = %225 + %234 = load i32, i32* %13, align 4 + %235 = sub nsw i32 0, %234 + store i32 %235, i32* %13, align 4 + br label %260 + +236: ; preds = %225 + %237 = load i32, i32* %2, align 4 + %238 = icmp ne i32 %237, 0 + br i1 %238, label %239, label %242 + +239: ; preds = %236 + %240 = load i32, i32* %2, align 4 + %241 = add nsw i32 %240, -1 + store i32 %241, i32* %2, align 4 + br label %242 + +242: ; preds = %239, %236 + %243 = load i32, i32* %13, align 4 + store i32 %243, i32* %1, align 4 + %244 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %245 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %244, i32 1 + store %union.YYSTYPE* %245, %union.YYSTYPE** %9, align 8 + %246 = bitcast %union.YYSTYPE* %245 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %246, i8* align 8 bitcast (%union.YYSTYPE* @yylval to i8*), i64 8, i1 false) + %247 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %248 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %247, i32 1 + store %struct.YYLTYPE* %248, %struct.YYLTYPE** %12, align 8 + %249 = bitcast %struct.YYLTYPE* %248 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %249, i8* align 4 bitcast (%struct.YYLTYPE* @yylloc to i8*), i64 16, i1 false) + store i32 -2, i32* @yychar, align 4 + br label %44 + +250: ; preds = %224, %175 + %251 = load i32, i32* %1, align 4 + %252 = sext i32 %251 to i64 + %253 = getelementptr inbounds [207 x i8], [207 x i8]* @yydefact, i64 0, i64 %252 + %254 = load i8, i8* %253, align 1 + %255 = sext i8 %254 to i32 + store i32 %255, i32* %13, align 4 + %256 = load i32, i32* %13, align 4 + %257 = icmp eq i32 %256, 0 + br i1 %257, label %258, label %259 + +258: ; preds = %250 + br label %1314 + +259: ; preds = %250 + br label %260 + +260: ; preds = %259, %233 + %261 = load i32, i32* %13, align 4 + %262 = sext i32 %261 to i64 + %263 = getelementptr inbounds [101 x i8], [101 x i8]* @yyr2, i64 0, i64 %262 + %264 = load i8, i8* %263, align 1 + %265 = sext i8 %264 to i32 + store i32 %265, i32* %22, align 4 + %266 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %267 = load i32, i32* %22, align 4 + %268 = sub nsw i32 1, %267 + %269 = sext i32 %268 to i64 + %270 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %266, i64 %269 + %271 = bitcast %union.YYSTYPE* %16 to i8* + %272 = bitcast %union.YYSTYPE* %270 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %271, i8* align 8 %272, i64 8, i1 false) + br label %273 + +273: ; preds = %260 + %274 = load i32, i32* %22, align 4 + %275 = icmp ne i32 %274, 0 + br i1 %275, label %276, label %317 + +276: ; preds = %273 + %277 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %278 = load i32, i32* %22, align 4 + %279 = sext i32 %278 to i64 + %280 = sub i64 0, %279 + %281 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %277, i64 %280 + %282 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %281, i64 1 + %283 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %282, i32 0, i32 0 + %284 = load i32, i32* %283, align 4 + %285 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %17, i32 0, i32 0 + store i32 %284, i32* %285, align 4 + %286 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %287 = load i32, i32* %22, align 4 + %288 = sext i32 %287 to i64 + %289 = sub i64 0, %288 + %290 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %286, i64 %289 + %291 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %290, i64 1 + %292 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %291, i32 0, i32 1 + %293 = load i32, i32* %292, align 4 + %294 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %17, i32 0, i32 1 + store i32 %293, i32* %294, align 4 + %295 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %296 = load i32, i32* %22, align 4 + %297 = sext i32 %296 to i64 + %298 = sub i64 0, %297 + %299 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %295, i64 %298 + %300 = load i32, i32* %22, align 4 + %301 = sext i32 %300 to i64 + %302 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %299, i64 %301 + %303 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %302, i32 0, i32 2 + %304 = load i32, i32* %303, align 4 + %305 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %17, i32 0, i32 2 + store i32 %304, i32* %305, align 4 + %306 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %307 = load i32, i32* %22, align 4 + %308 = sext i32 %307 to i64 + %309 = sub i64 0, %308 + %310 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %306, i64 %309 + %311 = load i32, i32* %22, align 4 + %312 = sext i32 %311 to i64 + %313 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %310, i64 %312 + %314 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %313, i32 0, i32 3 + %315 = load i32, i32* %314, align 4 + %316 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %17, i32 0, i32 3 + store i32 %315, i32* %316, align 4 + br label %338 + +317: ; preds = %273 + %318 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %319 = load i32, i32* %22, align 4 + %320 = sext i32 %319 to i64 + %321 = sub i64 0, %320 + %322 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %318, i64 %321 + %323 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %322, i64 0 + %324 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %323, i32 0, i32 2 + %325 = load i32, i32* %324, align 4 + %326 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %17, i32 0, i32 2 + store i32 %325, i32* %326, align 4 + %327 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %17, i32 0, i32 0 + store i32 %325, i32* %327, align 4 + %328 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %329 = load i32, i32* %22, align 4 + %330 = sext i32 %329 to i64 + %331 = sub i64 0, %330 + %332 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %328, i64 %331 + %333 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %332, i64 0 + %334 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %333, i32 0, i32 3 + %335 = load i32, i32* %334, align 4 + %336 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %17, i32 0, i32 3 + store i32 %335, i32* %336, align 4 + %337 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %17, i32 0, i32 1 + store i32 %335, i32* %337, align 4 + br label %338 + +338: ; preds = %317, %276 + br label %339 + +339: ; preds = %338 + %340 = getelementptr inbounds [3 x %struct.YYLTYPE], [3 x %struct.YYLTYPE]* %18, i64 0, i64 1 + %341 = bitcast %struct.YYLTYPE* %340 to i8* + %342 = bitcast %struct.YYLTYPE* %17 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 %341, i8* align 4 %342, i64 16, i1 false) + %343 = load i32, i32* %13, align 4 + switch i32 %343, label %1245 [ + i32 2, label %344 + i32 3, label %350 + i32 4, label %357 + i32 5, label %364 + i32 6, label %371 + i32 7, label %382 + i32 8, label %393 + i32 9, label %404 + i32 10, label %411 + i32 11, label %418 + i32 12, label %433 + i32 13, label %452 + i32 14, label %454 + i32 15, label %465 + i32 16, label %472 + i32 17, label %475 + i32 18, label %482 + i32 19, label %493 + i32 20, label %500 + i32 21, label %511 + i32 22, label %522 + i32 23, label %529 + i32 24, label %536 + i32 25, label %547 + i32 26, label %562 + i32 27, label %577 + i32 28, label %596 + i32 29, label %603 + i32 30, label %606 + i32 31, label %613 + i32 32, label %620 + i32 33, label %631 + i32 34, label %642 + i32 35, label %653 + i32 36, label %664 + i32 37, label %679 + i32 38, label %694 + i32 39, label %709 + i32 40, label %716 + i32 41, label %723 + i32 42, label %734 + i32 43, label %745 + i32 44, label %756 + i32 45, label %767 + i32 46, label %782 + i32 47, label %797 + i32 48, label %804 + i32 49, label %806 + i32 50, label %817 + i32 51, label %828 + i32 52, label %839 + i32 53, label %850 + i32 54, label %857 + i32 55, label %859 + i32 56, label %866 + i32 57, label %877 + i32 58, label %892 + i32 59, label %903 + i32 60, label %905 + i32 61, label %907 + i32 62, label %909 + i32 63, label %916 + i32 64, label %923 + i32 65, label %930 + i32 66, label %941 + i32 67, label %952 + i32 68, label %959 + i32 69, label %970 + i32 70, label %981 + i32 71, label %992 + i32 72, label %999 + i32 73, label %1006 + i32 74, label %1017 + i32 75, label %1024 + i32 76, label %1031 + i32 77, label %1038 + i32 78, label %1045 + i32 79, label %1056 + i32 80, label %1063 + i32 81, label %1070 + i32 82, label %1077 + i32 83, label %1084 + i32 84, label %1095 + i32 85, label %1102 + i32 86, label %1109 + i32 87, label %1120 + i32 88, label %1127 + i32 89, label %1134 + i32 90, label %1145 + i32 91, label %1152 + i32 92, label %1159 + i32 93, label %1170 + i32 94, label %1181 + i32 95, label %1188 + i32 96, label %1199 + i32 97, label %1210 + i32 98, label %1221 + i32 99, label %1232 + i32 100, label %1234 + ] + +344: ; preds = %339 + %345 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %346 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %345, i64 0 + %347 = bitcast %union.YYSTYPE* %346 to %struct.ASTNode** + %348 = load %struct.ASTNode*, %struct.ASTNode** %347, align 8 + %349 = call %struct.ASTNode* @new_node(i32 noundef 45, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %348, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + store %struct.ASTNode* %349, %struct.ASTNode** @root, align 8 + br label %1246 + +350: ; preds = %339 + %351 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %352 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %351, i64 0 + %353 = bitcast %union.YYSTYPE* %352 to %struct.ASTNode** + %354 = load %struct.ASTNode*, %struct.ASTNode** %353, align 8 + %355 = call %struct.ASTNode* @new_node(i32 noundef 0, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %354, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %356 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %355, %struct.ASTNode** %356, align 8 + br label %1246 + +357: ; preds = %339 + %358 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %359 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %358, i64 0 + %360 = bitcast %union.YYSTYPE* %359 to %struct.ASTNode** + %361 = load %struct.ASTNode*, %struct.ASTNode** %360, align 8 + %362 = call %struct.ASTNode* @new_node(i32 noundef 0, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %361, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %363 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %362, %struct.ASTNode** %363, align 8 + br label %1246 + +364: ; preds = %339 + %365 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %366 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %365, i64 0 + %367 = bitcast %union.YYSTYPE* %366 to %struct.ASTNode** + %368 = load %struct.ASTNode*, %struct.ASTNode** %367, align 8 + %369 = call %struct.ASTNode* @new_node(i32 noundef 0, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %368, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %370 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %369, %struct.ASTNode** %370, align 8 + br label %1246 + +371: ; preds = %339 + %372 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %373 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %372, i64 0 + %374 = bitcast %union.YYSTYPE* %373 to %struct.ASTNode** + %375 = load %struct.ASTNode*, %struct.ASTNode** %374, align 8 + %376 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %377 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %376, i64 -1 + %378 = bitcast %union.YYSTYPE* %377 to %struct.ASTNode** + %379 = load %struct.ASTNode*, %struct.ASTNode** %378, align 8 + %380 = call %struct.ASTNode* @new_node(i32 noundef 0, %struct.ASTNode* noundef %375, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %379, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %381 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %380, %struct.ASTNode** %381, align 8 + br label %1246 + +382: ; preds = %339 + %383 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %384 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %383, i64 0 + %385 = bitcast %union.YYSTYPE* %384 to %struct.ASTNode** + %386 = load %struct.ASTNode*, %struct.ASTNode** %385, align 8 + %387 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %388 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %387, i64 -1 + %389 = bitcast %union.YYSTYPE* %388 to %struct.ASTNode** + %390 = load %struct.ASTNode*, %struct.ASTNode** %389, align 8 + %391 = call %struct.ASTNode* @new_node(i32 noundef 0, %struct.ASTNode* noundef %386, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %390, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %392 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %391, %struct.ASTNode** %392, align 8 + br label %1246 + +393: ; preds = %339 + %394 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %395 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %394, i64 0 + %396 = bitcast %union.YYSTYPE* %395 to %struct.ASTNode** + %397 = load %struct.ASTNode*, %struct.ASTNode** %396, align 8 + %398 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %399 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %398, i64 -1 + %400 = bitcast %union.YYSTYPE* %399 to %struct.ASTNode** + %401 = load %struct.ASTNode*, %struct.ASTNode** %400, align 8 + %402 = call %struct.ASTNode* @new_node(i32 noundef 0, %struct.ASTNode* noundef %397, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %401, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %403 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %402, %struct.ASTNode** %403, align 8 + br label %1246 + +404: ; preds = %339 + %405 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %406 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %405, i64 -1 + %407 = bitcast %union.YYSTYPE* %406 to %struct.ASTNode** + %408 = load %struct.ASTNode*, %struct.ASTNode** %407, align 8 + %409 = call %struct.ASTNode* @new_node(i32 noundef 1, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %408, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 40) + %410 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %409, %struct.ASTNode** %410, align 8 + br label %1246 + +411: ; preds = %339 + %412 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %413 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %412, i64 -1 + %414 = bitcast %union.YYSTYPE* %413 to %struct.ASTNode** + %415 = load %struct.ASTNode*, %struct.ASTNode** %414, align 8 + %416 = call %struct.ASTNode* @new_node(i32 noundef 1, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %415, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 39) + %417 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %416, %struct.ASTNode** %417, align 8 + br label %1246 + +418: ; preds = %339 + %419 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %420 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %419, i64 -2 + %421 = bitcast %union.YYSTYPE* %420 to %struct.ASTNode** + %422 = load %struct.ASTNode*, %struct.ASTNode** %421, align 8 + %423 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %424 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %423, i64 0 + %425 = bitcast %union.YYSTYPE* %424 to %struct.ASTNode** + %426 = load %struct.ASTNode*, %struct.ASTNode** %425, align 8 + %427 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %428 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %427, i64 -3 + %429 = bitcast %union.YYSTYPE* %428 to i8** + %430 = load i8*, i8** %429, align 8 + %431 = call %struct.ASTNode* @new_node(i32 noundef 4, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %422, %struct.ASTNode* noundef %426, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %430, i32 noundef 38) + %432 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %431, %struct.ASTNode** %432, align 8 + br label %1246 + +433: ; preds = %339 + %434 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %435 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %434, i64 0 + %436 = bitcast %union.YYSTYPE* %435 to %struct.ASTNode** + %437 = load %struct.ASTNode*, %struct.ASTNode** %436, align 8 + %438 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %439 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %438, i64 -4 + %440 = bitcast %union.YYSTYPE* %439 to %struct.ASTNode** + %441 = load %struct.ASTNode*, %struct.ASTNode** %440, align 8 + %442 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %443 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %442, i64 -2 + %444 = bitcast %union.YYSTYPE* %443 to %struct.ASTNode** + %445 = load %struct.ASTNode*, %struct.ASTNode** %444, align 8 + %446 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %447 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %446, i64 -5 + %448 = bitcast %union.YYSTYPE* %447 to i8** + %449 = load i8*, i8** %448, align 8 + %450 = call %struct.ASTNode* @new_node(i32 noundef 4, %struct.ASTNode* noundef %437, %struct.ASTNode* noundef %441, %struct.ASTNode* noundef %445, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %449, i32 noundef 38) + %451 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %450, %struct.ASTNode** %451, align 8 + br label %1246 + +452: ; preds = %339 + %453 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* null, %struct.ASTNode** %453, align 8 + br label %1246 + +454: ; preds = %339 + %455 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %456 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %455, i64 0 + %457 = bitcast %union.YYSTYPE* %456 to %struct.ASTNode** + %458 = load %struct.ASTNode*, %struct.ASTNode** %457, align 8 + %459 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %460 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %459, i64 -2 + %461 = bitcast %union.YYSTYPE* %460 to %struct.ASTNode** + %462 = load %struct.ASTNode*, %struct.ASTNode** %461, align 8 + %463 = call %struct.ASTNode* @new_node(i32 noundef 26, %struct.ASTNode* noundef %458, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %462, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %464 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %463, %struct.ASTNode** %464, align 8 + br label %1246 + +465: ; preds = %339 + %466 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %467 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %466, i64 0 + %468 = bitcast %union.YYSTYPE* %467 to %struct.ASTNode** + %469 = load %struct.ASTNode*, %struct.ASTNode** %468, align 8 + %470 = call %struct.ASTNode* @new_node(i32 noundef 5, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %469, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %471 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %470, %struct.ASTNode** %471, align 8 + br label %1246 + +472: ; preds = %339 + %473 = call %struct.ASTNode* @new_node(i32 noundef 5, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %474 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %473, %struct.ASTNode** %474, align 8 + br label %1246 + +475: ; preds = %339 + %476 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %477 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %476, i64 -1 + %478 = bitcast %union.YYSTYPE* %477 to %struct.ASTNode** + %479 = load %struct.ASTNode*, %struct.ASTNode** %478, align 8 + %480 = call %struct.ASTNode* @new_node(i32 noundef 5, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %479, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %481 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %480, %struct.ASTNode** %481, align 8 + br label %1246 + +482: ; preds = %339 + %483 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %484 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %483, i64 -1 + %485 = bitcast %union.YYSTYPE* %484 to %struct.ASTNode** + %486 = load %struct.ASTNode*, %struct.ASTNode** %485, align 8 + %487 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %488 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %487, i64 -3 + %489 = bitcast %union.YYSTYPE* %488 to %struct.ASTNode** + %490 = load %struct.ASTNode*, %struct.ASTNode** %489, align 8 + %491 = call %struct.ASTNode* @new_node(i32 noundef 5, %struct.ASTNode* noundef %486, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %490, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %492 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %491, %struct.ASTNode** %492, align 8 + br label %1246 + +493: ; preds = %339 + %494 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %495 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %494, i64 0 + %496 = bitcast %union.YYSTYPE* %495 to %struct.ASTNode** + %497 = load %struct.ASTNode*, %struct.ASTNode** %496, align 8 + %498 = call %struct.ASTNode* @new_node(i32 noundef 25, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %497, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %499 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %498, %struct.ASTNode** %499, align 8 + br label %1246 + +500: ; preds = %339 + %501 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %502 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %501, i64 0 + %503 = bitcast %union.YYSTYPE* %502 to %struct.ASTNode** + %504 = load %struct.ASTNode*, %struct.ASTNode** %503, align 8 + %505 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %506 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %505, i64 -2 + %507 = bitcast %union.YYSTYPE* %506 to %struct.ASTNode** + %508 = load %struct.ASTNode*, %struct.ASTNode** %507, align 8 + %509 = call %struct.ASTNode* @new_node(i32 noundef 25, %struct.ASTNode* noundef %504, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %508, i32 noundef 282, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %510 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %509, %struct.ASTNode** %510, align 8 + br label %1246 + +511: ; preds = %339 + %512 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %513 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %512, i64 0 + %514 = bitcast %union.YYSTYPE* %513 to %struct.ASTNode** + %515 = load %struct.ASTNode*, %struct.ASTNode** %514, align 8 + %516 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %517 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %516, i64 -2 + %518 = bitcast %union.YYSTYPE* %517 to %struct.ASTNode** + %519 = load %struct.ASTNode*, %struct.ASTNode** %518, align 8 + %520 = call %struct.ASTNode* @new_node(i32 noundef 25, %struct.ASTNode* noundef %515, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %519, i32 noundef 279, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %521 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %520, %struct.ASTNode** %521, align 8 + br label %1246 + +522: ; preds = %339 + %523 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %524 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %523, i64 -1 + %525 = bitcast %union.YYSTYPE* %524 to %struct.ASTNode** + %526 = load %struct.ASTNode*, %struct.ASTNode** %525, align 8 + %527 = call %struct.ASTNode* @new_node(i32 noundef 2, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %526, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 40) + %528 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %527, %struct.ASTNode** %528, align 8 + br label %1246 + +529: ; preds = %339 + %530 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %531 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %530, i64 -1 + %532 = bitcast %union.YYSTYPE* %531 to %struct.ASTNode** + %533 = load %struct.ASTNode*, %struct.ASTNode** %532, align 8 + %534 = call %struct.ASTNode* @new_node(i32 noundef 2, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %533, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 39) + %535 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %534, %struct.ASTNode** %535, align 8 + br label %1246 + +536: ; preds = %339 + %537 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %538 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %537, i64 0 + %539 = bitcast %union.YYSTYPE* %538 to %struct.ASTNode** + %540 = load %struct.ASTNode*, %struct.ASTNode** %539, align 8 + %541 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %542 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %541, i64 -1 + %543 = bitcast %union.YYSTYPE* %542 to i8** + %544 = load i8*, i8** %543, align 8 + %545 = call %struct.ASTNode* @new_node(i32 noundef 6, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %540, %struct.ASTNode* noundef null, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %544, i32 noundef 38) + %546 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %545, %struct.ASTNode** %546, align 8 + br label %1246 + +547: ; preds = %339 + %548 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %549 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %548, i64 -2 + %550 = bitcast %union.YYSTYPE* %549 to %struct.ASTNode** + %551 = load %struct.ASTNode*, %struct.ASTNode** %550, align 8 + %552 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %553 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %552, i64 0 + %554 = bitcast %union.YYSTYPE* %553 to %struct.ASTNode** + %555 = load %struct.ASTNode*, %struct.ASTNode** %554, align 8 + %556 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %557 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %556, i64 -3 + %558 = bitcast %union.YYSTYPE* %557 to i8** + %559 = load i8*, i8** %558, align 8 + %560 = call %struct.ASTNode* @new_node(i32 noundef 6, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %551, %struct.ASTNode* noundef %555, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %559, i32 noundef 38) + %561 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %560, %struct.ASTNode** %561, align 8 + br label %1246 + +562: ; preds = %339 + %563 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %564 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %563, i64 0 + %565 = bitcast %union.YYSTYPE* %564 to %struct.ASTNode** + %566 = load %struct.ASTNode*, %struct.ASTNode** %565, align 8 + %567 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %568 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %567, i64 -2 + %569 = bitcast %union.YYSTYPE* %568 to %struct.ASTNode** + %570 = load %struct.ASTNode*, %struct.ASTNode** %569, align 8 + %571 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %572 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %571, i64 -3 + %573 = bitcast %union.YYSTYPE* %572 to i8** + %574 = load i8*, i8** %573, align 8 + %575 = call %struct.ASTNode* @new_node(i32 noundef 6, %struct.ASTNode* noundef %566, %struct.ASTNode* noundef %570, %struct.ASTNode* noundef null, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %574, i32 noundef 38) + %576 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %575, %struct.ASTNode** %576, align 8 + br label %1246 + +577: ; preds = %339 + %578 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %579 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %578, i64 0 + %580 = bitcast %union.YYSTYPE* %579 to %struct.ASTNode** + %581 = load %struct.ASTNode*, %struct.ASTNode** %580, align 8 + %582 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %583 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %582, i64 -4 + %584 = bitcast %union.YYSTYPE* %583 to %struct.ASTNode** + %585 = load %struct.ASTNode*, %struct.ASTNode** %584, align 8 + %586 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %587 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %586, i64 -2 + %588 = bitcast %union.YYSTYPE* %587 to %struct.ASTNode** + %589 = load %struct.ASTNode*, %struct.ASTNode** %588, align 8 + %590 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %591 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %590, i64 -5 + %592 = bitcast %union.YYSTYPE* %591 to i8** + %593 = load i8*, i8** %592, align 8 + %594 = call %struct.ASTNode* @new_node(i32 noundef 6, %struct.ASTNode* noundef %581, %struct.ASTNode* noundef %585, %struct.ASTNode* noundef %589, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %593, i32 noundef 38) + %595 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %594, %struct.ASTNode** %595, align 8 + br label %1246 + +596: ; preds = %339 + %597 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %598 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %597, i64 0 + %599 = bitcast %union.YYSTYPE* %598 to %struct.ASTNode** + %600 = load %struct.ASTNode*, %struct.ASTNode** %599, align 8 + %601 = call %struct.ASTNode* @new_node(i32 noundef 7, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %600, i32 noundef 10, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %602 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %601, %struct.ASTNode** %602, align 8 + br label %1246 + +603: ; preds = %339 + %604 = call %struct.ASTNode* @new_node(i32 noundef 7, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, i32 noundef 41, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %605 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %604, %struct.ASTNode** %605, align 8 + br label %1246 + +606: ; preds = %339 + %607 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %608 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %607, i64 -1 + %609 = bitcast %union.YYSTYPE* %608 to %struct.ASTNode** + %610 = load %struct.ASTNode*, %struct.ASTNode** %609, align 8 + %611 = call %struct.ASTNode* @new_node(i32 noundef 7, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %610, i32 noundef 41, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %612 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %611, %struct.ASTNode** %612, align 8 + br label %1246 + +613: ; preds = %339 + %614 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %615 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %614, i64 0 + %616 = bitcast %union.YYSTYPE* %615 to %struct.ASTNode** + %617 = load %struct.ASTNode*, %struct.ASTNode** %616, align 8 + %618 = call %struct.ASTNode* @new_node(i32 noundef 41, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %617, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %619 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %618, %struct.ASTNode** %619, align 8 + br label %1246 + +620: ; preds = %339 + %621 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %622 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %621, i64 0 + %623 = bitcast %union.YYSTYPE* %622 to %struct.ASTNode** + %624 = load %struct.ASTNode*, %struct.ASTNode** %623, align 8 + %625 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %626 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %625, i64 -2 + %627 = bitcast %union.YYSTYPE* %626 to %struct.ASTNode** + %628 = load %struct.ASTNode*, %struct.ASTNode** %627, align 8 + %629 = call %struct.ASTNode* @new_node(i32 noundef 41, %struct.ASTNode* noundef %624, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %628, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %630 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %629, %struct.ASTNode** %630, align 8 + br label %1246 + +631: ; preds = %339 + %632 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %633 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %632, i64 0 + %634 = bitcast %union.YYSTYPE* %633 to %struct.ASTNode** + %635 = load %struct.ASTNode*, %struct.ASTNode** %634, align 8 + %636 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %637 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %636, i64 -3 + %638 = bitcast %union.YYSTYPE* %637 to i8** + %639 = load i8*, i8** %638, align 8 + %640 = call %struct.ASTNode* @new_node(i32 noundef 3, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %635, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %639, i32 noundef 40) + %641 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %640, %struct.ASTNode** %641, align 8 + br label %1246 + +642: ; preds = %339 + %643 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %644 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %643, i64 0 + %645 = bitcast %union.YYSTYPE* %644 to %struct.ASTNode** + %646 = load %struct.ASTNode*, %struct.ASTNode** %645, align 8 + %647 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %648 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %647, i64 -3 + %649 = bitcast %union.YYSTYPE* %648 to i8** + %650 = load i8*, i8** %649, align 8 + %651 = call %struct.ASTNode* @new_node(i32 noundef 3, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %646, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %650, i32 noundef 39) + %652 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %651, %struct.ASTNode** %652, align 8 + br label %1246 + +653: ; preds = %339 + %654 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %655 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %654, i64 0 + %656 = bitcast %union.YYSTYPE* %655 to %struct.ASTNode** + %657 = load %struct.ASTNode*, %struct.ASTNode** %656, align 8 + %658 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %659 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %658, i64 -3 + %660 = bitcast %union.YYSTYPE* %659 to i8** + %661 = load i8*, i8** %660, align 8 + %662 = call %struct.ASTNode* @new_node(i32 noundef 3, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %657, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %661, i32 noundef 42) + %663 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %662, %struct.ASTNode** %663, align 8 + br label %1246 + +664: ; preds = %339 + %665 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %666 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %665, i64 -2 + %667 = bitcast %union.YYSTYPE* %666 to %struct.ASTNode** + %668 = load %struct.ASTNode*, %struct.ASTNode** %667, align 8 + %669 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %670 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %669, i64 0 + %671 = bitcast %union.YYSTYPE* %670 to %struct.ASTNode** + %672 = load %struct.ASTNode*, %struct.ASTNode** %671, align 8 + %673 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %674 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %673, i64 -4 + %675 = bitcast %union.YYSTYPE* %674 to i8** + %676 = load i8*, i8** %675, align 8 + %677 = call %struct.ASTNode* @new_node(i32 noundef 3, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %668, %struct.ASTNode* noundef %672, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %676, i32 noundef 40) + %678 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %677, %struct.ASTNode** %678, align 8 + br label %1246 + +679: ; preds = %339 + %680 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %681 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %680, i64 -2 + %682 = bitcast %union.YYSTYPE* %681 to %struct.ASTNode** + %683 = load %struct.ASTNode*, %struct.ASTNode** %682, align 8 + %684 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %685 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %684, i64 0 + %686 = bitcast %union.YYSTYPE* %685 to %struct.ASTNode** + %687 = load %struct.ASTNode*, %struct.ASTNode** %686, align 8 + %688 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %689 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %688, i64 -4 + %690 = bitcast %union.YYSTYPE* %689 to i8** + %691 = load i8*, i8** %690, align 8 + %692 = call %struct.ASTNode* @new_node(i32 noundef 3, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %683, %struct.ASTNode* noundef %687, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %691, i32 noundef 39) + %693 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %692, %struct.ASTNode** %693, align 8 + br label %1246 + +694: ; preds = %339 + %695 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %696 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %695, i64 -2 + %697 = bitcast %union.YYSTYPE* %696 to %struct.ASTNode** + %698 = load %struct.ASTNode*, %struct.ASTNode** %697, align 8 + %699 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %700 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %699, i64 0 + %701 = bitcast %union.YYSTYPE* %700 to %struct.ASTNode** + %702 = load %struct.ASTNode*, %struct.ASTNode** %701, align 8 + %703 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %704 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %703, i64 -4 + %705 = bitcast %union.YYSTYPE* %704 to i8** + %706 = load i8*, i8** %705, align 8 + %707 = call %struct.ASTNode* @new_node(i32 noundef 3, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %698, %struct.ASTNode* noundef %702, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %706, i32 noundef 42) + %708 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %707, %struct.ASTNode** %708, align 8 + br label %1246 + +709: ; preds = %339 + %710 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %711 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %710, i64 0 + %712 = bitcast %union.YYSTYPE* %711 to i8** + %713 = load i8*, i8** %712, align 8 + %714 = call %struct.ASTNode* @new_node(i32 noundef 8, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %713, i32 noundef 40) + %715 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %714, %struct.ASTNode** %715, align 8 + br label %1246 + +716: ; preds = %339 + %717 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %718 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %717, i64 0 + %719 = bitcast %union.YYSTYPE* %718 to i8** + %720 = load i8*, i8** %719, align 8 + %721 = call %struct.ASTNode* @new_node(i32 noundef 8, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %720, i32 noundef 39) + %722 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %721, %struct.ASTNode** %722, align 8 + br label %1246 + +723: ; preds = %339 + %724 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %725 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %724, i64 0 + %726 = bitcast %union.YYSTYPE* %725 to %struct.ASTNode** + %727 = load %struct.ASTNode*, %struct.ASTNode** %726, align 8 + %728 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %729 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %728, i64 -3 + %730 = bitcast %union.YYSTYPE* %729 to i8** + %731 = load i8*, i8** %730, align 8 + %732 = call %struct.ASTNode* @new_node(i32 noundef 8, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %727, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %731, i32 noundef 40) + %733 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %732, %struct.ASTNode** %733, align 8 + br label %1246 + +734: ; preds = %339 + %735 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %736 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %735, i64 0 + %737 = bitcast %union.YYSTYPE* %736 to %struct.ASTNode** + %738 = load %struct.ASTNode*, %struct.ASTNode** %737, align 8 + %739 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %740 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %739, i64 -3 + %741 = bitcast %union.YYSTYPE* %740 to i8** + %742 = load i8*, i8** %741, align 8 + %743 = call %struct.ASTNode* @new_node(i32 noundef 8, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %738, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %742, i32 noundef 39) + %744 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %743, %struct.ASTNode** %744, align 8 + br label %1246 + +745: ; preds = %339 + %746 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %747 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %746, i64 0 + %748 = bitcast %union.YYSTYPE* %747 to %struct.ASTNode** + %749 = load %struct.ASTNode*, %struct.ASTNode** %748, align 8 + %750 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %751 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %750, i64 -2 + %752 = bitcast %union.YYSTYPE* %751 to i8** + %753 = load i8*, i8** %752, align 8 + %754 = call %struct.ASTNode* @new_node(i32 noundef 8, %struct.ASTNode* noundef %749, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %753, i32 noundef 40) + %755 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %754, %struct.ASTNode** %755, align 8 + br label %1246 + +756: ; preds = %339 + %757 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %758 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %757, i64 0 + %759 = bitcast %union.YYSTYPE* %758 to %struct.ASTNode** + %760 = load %struct.ASTNode*, %struct.ASTNode** %759, align 8 + %761 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %762 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %761, i64 -2 + %763 = bitcast %union.YYSTYPE* %762 to i8** + %764 = load i8*, i8** %763, align 8 + %765 = call %struct.ASTNode* @new_node(i32 noundef 8, %struct.ASTNode* noundef %760, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %764, i32 noundef 39) + %766 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %765, %struct.ASTNode** %766, align 8 + br label %1246 + +767: ; preds = %339 + %768 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %769 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %768, i64 0 + %770 = bitcast %union.YYSTYPE* %769 to %struct.ASTNode** + %771 = load %struct.ASTNode*, %struct.ASTNode** %770, align 8 + %772 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %773 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %772, i64 -2 + %774 = bitcast %union.YYSTYPE* %773 to %struct.ASTNode** + %775 = load %struct.ASTNode*, %struct.ASTNode** %774, align 8 + %776 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %777 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %776, i64 -5 + %778 = bitcast %union.YYSTYPE* %777 to i8** + %779 = load i8*, i8** %778, align 8 + %780 = call %struct.ASTNode* @new_node(i32 noundef 8, %struct.ASTNode* noundef %771, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %775, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %779, i32 noundef 40) + %781 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %780, %struct.ASTNode** %781, align 8 + br label %1246 + +782: ; preds = %339 + %783 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %784 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %783, i64 0 + %785 = bitcast %union.YYSTYPE* %784 to %struct.ASTNode** + %786 = load %struct.ASTNode*, %struct.ASTNode** %785, align 8 + %787 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %788 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %787, i64 -2 + %789 = bitcast %union.YYSTYPE* %788 to %struct.ASTNode** + %790 = load %struct.ASTNode*, %struct.ASTNode** %789, align 8 + %791 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %792 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %791, i64 -5 + %793 = bitcast %union.YYSTYPE* %792 to i8** + %794 = load i8*, i8** %793, align 8 + %795 = call %struct.ASTNode* @new_node(i32 noundef 8, %struct.ASTNode* noundef %786, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %790, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %794, i32 noundef 39) + %796 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %795, %struct.ASTNode** %796, align 8 + br label %1246 + +797: ; preds = %339 + %798 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %799 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %798, i64 -1 + %800 = bitcast %union.YYSTYPE* %799 to %struct.ASTNode** + %801 = load %struct.ASTNode*, %struct.ASTNode** %800, align 8 + %802 = call %struct.ASTNode* @new_node(i32 noundef 11, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %801, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %803 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %802, %struct.ASTNode** %803, align 8 + br label %1246 + +804: ; preds = %339 + %805 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* null, %struct.ASTNode** %805, align 8 + br label %1246 + +806: ; preds = %339 + %807 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %808 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %807, i64 0 + %809 = bitcast %union.YYSTYPE* %808 to %struct.ASTNode** + %810 = load %struct.ASTNode*, %struct.ASTNode** %809, align 8 + %811 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %812 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %811, i64 -1 + %813 = bitcast %union.YYSTYPE* %812 to %struct.ASTNode** + %814 = load %struct.ASTNode*, %struct.ASTNode** %813, align 8 + %815 = call %struct.ASTNode* @new_node(i32 noundef 12, %struct.ASTNode* noundef %810, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %814, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %816 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %815, %struct.ASTNode** %816, align 8 + br label %1246 + +817: ; preds = %339 + %818 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %819 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %818, i64 0 + %820 = bitcast %union.YYSTYPE* %819 to %struct.ASTNode** + %821 = load %struct.ASTNode*, %struct.ASTNode** %820, align 8 + %822 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %823 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %822, i64 -1 + %824 = bitcast %union.YYSTYPE* %823 to %struct.ASTNode** + %825 = load %struct.ASTNode*, %struct.ASTNode** %824, align 8 + %826 = call %struct.ASTNode* @new_node(i32 noundef 12, %struct.ASTNode* noundef %821, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %825, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %827 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %826, %struct.ASTNode** %827, align 8 + br label %1246 + +828: ; preds = %339 + %829 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %830 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %829, i64 0 + %831 = bitcast %union.YYSTYPE* %830 to %struct.ASTNode** + %832 = load %struct.ASTNode*, %struct.ASTNode** %831, align 8 + %833 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %834 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %833, i64 -1 + %835 = bitcast %union.YYSTYPE* %834 to %struct.ASTNode** + %836 = load %struct.ASTNode*, %struct.ASTNode** %835, align 8 + %837 = call %struct.ASTNode* @new_node(i32 noundef 12, %struct.ASTNode* noundef %832, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %836, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %838 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %837, %struct.ASTNode** %838, align 8 + br label %1246 + +839: ; preds = %339 + %840 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %841 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %840, i64 -3 + %842 = bitcast %union.YYSTYPE* %841 to %struct.ASTNode** + %843 = load %struct.ASTNode*, %struct.ASTNode** %842, align 8 + %844 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %845 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %844, i64 -1 + %846 = bitcast %union.YYSTYPE* %845 to %struct.ASTNode** + %847 = load %struct.ASTNode*, %struct.ASTNode** %846, align 8 + %848 = call %struct.ASTNode* @new_node(i32 noundef 13, %struct.ASTNode* noundef %843, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %847, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %849 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %848, %struct.ASTNode** %849, align 8 + br label %1246 + +850: ; preds = %339 + %851 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %852 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %851, i64 -1 + %853 = bitcast %union.YYSTYPE* %852 to %struct.ASTNode** + %854 = load %struct.ASTNode*, %struct.ASTNode** %853, align 8 + %855 = call %struct.ASTNode* @new_node(i32 noundef 13, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %854, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %856 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %855, %struct.ASTNode** %856, align 8 + br label %1246 + +857: ; preds = %339 + %858 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* null, %struct.ASTNode** %858, align 8 + br label %1246 + +859: ; preds = %339 + %860 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %861 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %860, i64 0 + %862 = bitcast %union.YYSTYPE* %861 to %struct.ASTNode** + %863 = load %struct.ASTNode*, %struct.ASTNode** %862, align 8 + %864 = call %struct.ASTNode* @new_node(i32 noundef 13, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %863, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %865 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %864, %struct.ASTNode** %865, align 8 + br label %1246 + +866: ; preds = %339 + %867 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %868 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %867, i64 -2 + %869 = bitcast %union.YYSTYPE* %868 to %struct.ASTNode** + %870 = load %struct.ASTNode*, %struct.ASTNode** %869, align 8 + %871 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %872 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %871, i64 0 + %873 = bitcast %union.YYSTYPE* %872 to %struct.ASTNode** + %874 = load %struct.ASTNode*, %struct.ASTNode** %873, align 8 + %875 = call %struct.ASTNode* @new_node(i32 noundef 13, %struct.ASTNode* noundef %870, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %874, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %876 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %875, %struct.ASTNode** %876, align 8 + br label %1246 + +877: ; preds = %339 + %878 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %879 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %878, i64 -4 + %880 = bitcast %union.YYSTYPE* %879 to %struct.ASTNode** + %881 = load %struct.ASTNode*, %struct.ASTNode** %880, align 8 + %882 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %883 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %882, i64 0 + %884 = bitcast %union.YYSTYPE* %883 to %struct.ASTNode** + %885 = load %struct.ASTNode*, %struct.ASTNode** %884, align 8 + %886 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %887 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %886, i64 -2 + %888 = bitcast %union.YYSTYPE* %887 to %struct.ASTNode** + %889 = load %struct.ASTNode*, %struct.ASTNode** %888, align 8 + %890 = call %struct.ASTNode* @new_node(i32 noundef 13, %struct.ASTNode* noundef %881, %struct.ASTNode* noundef %885, %struct.ASTNode* noundef %889, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %891 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %890, %struct.ASTNode** %891, align 8 + br label %1246 + +892: ; preds = %339 + %893 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %894 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %893, i64 -2 + %895 = bitcast %union.YYSTYPE* %894 to %struct.ASTNode** + %896 = load %struct.ASTNode*, %struct.ASTNode** %895, align 8 + %897 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %898 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %897, i64 0 + %899 = bitcast %union.YYSTYPE* %898 to %struct.ASTNode** + %900 = load %struct.ASTNode*, %struct.ASTNode** %899, align 8 + %901 = call %struct.ASTNode* @new_node(i32 noundef 13, %struct.ASTNode* noundef %896, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %900, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %902 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %901, %struct.ASTNode** %902, align 8 + br label %1246 + +903: ; preds = %339 + %904 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* null, %struct.ASTNode** %904, align 8 + br label %1246 + +905: ; preds = %339 + %906 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* null, %struct.ASTNode** %906, align 8 + br label %1246 + +907: ; preds = %339 + %908 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* null, %struct.ASTNode** %908, align 8 + br label %1246 + +909: ; preds = %339 + %910 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %911 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %910, i64 -1 + %912 = bitcast %union.YYSTYPE* %911 to %struct.ASTNode** + %913 = load %struct.ASTNode*, %struct.ASTNode** %912, align 8 + %914 = call %struct.ASTNode* @new_node(i32 noundef 13, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %913, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %915 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %914, %struct.ASTNode** %915, align 8 + br label %1246 + +916: ; preds = %339 + %917 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %918 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %917, i64 0 + %919 = bitcast %union.YYSTYPE* %918 to %struct.ASTNode** + %920 = load %struct.ASTNode*, %struct.ASTNode** %919, align 8 + %921 = call %struct.ASTNode* @new_node(i32 noundef 10, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %920, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %922 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %921, %struct.ASTNode** %922, align 8 + br label %1246 + +923: ; preds = %339 + %924 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %925 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %924, i64 0 + %926 = bitcast %union.YYSTYPE* %925 to %struct.ASTNode** + %927 = load %struct.ASTNode*, %struct.ASTNode** %926, align 8 + %928 = call %struct.ASTNode* @new_node(i32 noundef 36, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %927, i32 noundef 283, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %929 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %928, %struct.ASTNode** %929, align 8 + br label %1246 + +930: ; preds = %339 + %931 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %932 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %931, i64 0 + %933 = bitcast %union.YYSTYPE* %932 to %struct.ASTNode** + %934 = load %struct.ASTNode*, %struct.ASTNode** %933, align 8 + %935 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %936 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %935, i64 -2 + %937 = bitcast %union.YYSTYPE* %936 to %struct.ASTNode** + %938 = load %struct.ASTNode*, %struct.ASTNode** %937, align 8 + %939 = call %struct.ASTNode* @new_node(i32 noundef 36, %struct.ASTNode* noundef %934, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %938, i32 noundef 282, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %940 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %939, %struct.ASTNode** %940, align 8 + br label %1246 + +941: ; preds = %339 + %942 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %943 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %942, i64 0 + %944 = bitcast %union.YYSTYPE* %943 to %struct.ASTNode** + %945 = load %struct.ASTNode*, %struct.ASTNode** %944, align 8 + %946 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %947 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %946, i64 -2 + %948 = bitcast %union.YYSTYPE* %947 to %struct.ASTNode** + %949 = load %struct.ASTNode*, %struct.ASTNode** %948, align 8 + %950 = call %struct.ASTNode* @new_node(i32 noundef 36, %struct.ASTNode* noundef %945, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %949, i32 noundef 279, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %951 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %950, %struct.ASTNode** %951, align 8 + br label %1246 + +952: ; preds = %339 + %953 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %954 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %953, i64 0 + %955 = bitcast %union.YYSTYPE* %954 to %struct.ASTNode** + %956 = load %struct.ASTNode*, %struct.ASTNode** %955, align 8 + %957 = call %struct.ASTNode* @new_node(i32 noundef 19, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %956, i32 noundef 16, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %958 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %957, %struct.ASTNode** %958, align 8 + br label %1246 + +959: ; preds = %339 + %960 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %961 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %960, i64 0 + %962 = bitcast %union.YYSTYPE* %961 to %struct.ASTNode** + %963 = load %struct.ASTNode*, %struct.ASTNode** %962, align 8 + %964 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %965 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %964, i64 -2 + %966 = bitcast %union.YYSTYPE* %965 to %struct.ASTNode** + %967 = load %struct.ASTNode*, %struct.ASTNode** %966, align 8 + %968 = call %struct.ASTNode* @new_node(i32 noundef 19, %struct.ASTNode* noundef %963, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %967, i32 noundef 283, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %969 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %968, %struct.ASTNode** %969, align 8 + br label %1246 + +970: ; preds = %339 + %971 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %972 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %971, i64 0 + %973 = bitcast %union.YYSTYPE* %972 to %struct.ASTNode** + %974 = load %struct.ASTNode*, %struct.ASTNode** %973, align 8 + %975 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %976 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %975, i64 -2 + %977 = bitcast %union.YYSTYPE* %976 to %struct.ASTNode** + %978 = load %struct.ASTNode*, %struct.ASTNode** %977, align 8 + %979 = call %struct.ASTNode* @new_node(i32 noundef 19, %struct.ASTNode* noundef %974, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %978, i32 noundef 284, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %980 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %979, %struct.ASTNode** %980, align 8 + br label %1246 + +981: ; preds = %339 + %982 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %983 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %982, i64 0 + %984 = bitcast %union.YYSTYPE* %983 to %struct.ASTNode** + %985 = load %struct.ASTNode*, %struct.ASTNode** %984, align 8 + %986 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %987 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %986, i64 -2 + %988 = bitcast %union.YYSTYPE* %987 to %struct.ASTNode** + %989 = load %struct.ASTNode*, %struct.ASTNode** %988, align 8 + %990 = call %struct.ASTNode* @new_node(i32 noundef 19, %struct.ASTNode* noundef %985, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %989, i32 noundef 285, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %991 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %990, %struct.ASTNode** %991, align 8 + br label %1246 + +992: ; preds = %339 + %993 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %994 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %993, i64 0 + %995 = bitcast %union.YYSTYPE* %994 to %struct.ASTNode** + %996 = load %struct.ASTNode*, %struct.ASTNode** %995, align 8 + %997 = call %struct.ASTNode* @new_node(i32 noundef 16, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %996, i32 noundef 15, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %998 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %997, %struct.ASTNode** %998, align 8 + br label %1246 + +999: ; preds = %339 + %1000 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1001 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1000, i64 -2 + %1002 = bitcast %union.YYSTYPE* %1001 to i8** + %1003 = load i8*, i8** %1002, align 8 + %1004 = call %struct.ASTNode* @new_node(i32 noundef 16, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, i32 noundef 18, float noundef 0.000000e+00, i8* noundef %1003, i32 noundef 38) + %1005 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1004, %struct.ASTNode** %1005, align 8 + br label %1246 + +1006: ; preds = %339 + %1007 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1008 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1007, i64 -1 + %1009 = bitcast %union.YYSTYPE* %1008 to %struct.ASTNode** + %1010 = load %struct.ASTNode*, %struct.ASTNode** %1009, align 8 + %1011 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1012 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1011, i64 -3 + %1013 = bitcast %union.YYSTYPE* %1012 to i8** + %1014 = load i8*, i8** %1013, align 8 + %1015 = call %struct.ASTNode* @new_node(i32 noundef 16, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1010, i32 noundef 18, float noundef 0.000000e+00, i8* noundef %1014, i32 noundef 38) + %1016 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1015, %struct.ASTNode** %1016, align 8 + br label %1246 + +1017: ; preds = %339 + %1018 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1019 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1018, i64 0 + %1020 = bitcast %union.YYSTYPE* %1019 to %struct.ASTNode** + %1021 = load %struct.ASTNode*, %struct.ASTNode** %1020, align 8 + %1022 = call %struct.ASTNode* @new_node(i32 noundef 16, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1021, i32 noundef 43, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1023 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1022, %struct.ASTNode** %1023, align 8 + br label %1246 + +1024: ; preds = %339 + %1025 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1026 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1025, i64 0 + %1027 = bitcast %union.YYSTYPE* %1026 to %struct.ASTNode** + %1028 = load %struct.ASTNode*, %struct.ASTNode** %1027, align 8 + %1029 = call %struct.ASTNode* @new_node(i32 noundef 16, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1028, i32 noundef 44, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1030 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1029, %struct.ASTNode** %1030, align 8 + br label %1246 + +1031: ; preds = %339 + %1032 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1033 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1032, i64 0 + %1034 = bitcast %union.YYSTYPE* %1033 to %struct.ASTNode** + %1035 = load %struct.ASTNode*, %struct.ASTNode** %1034, align 8 + %1036 = call %struct.ASTNode* @new_node(i32 noundef 16, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1035, i32 noundef 280, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1037 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1036, %struct.ASTNode** %1037, align 8 + br label %1246 + +1038: ; preds = %339 + %1039 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1040 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1039, i64 0 + %1041 = bitcast %union.YYSTYPE* %1040 to %struct.ASTNode** + %1042 = load %struct.ASTNode*, %struct.ASTNode** %1041, align 8 + %1043 = call %struct.ASTNode* @new_node(i32 noundef 18, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1042, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1044 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1043, %struct.ASTNode** %1044, align 8 + br label %1246 + +1045: ; preds = %339 + %1046 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1047 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1046, i64 0 + %1048 = bitcast %union.YYSTYPE* %1047 to %struct.ASTNode** + %1049 = load %struct.ASTNode*, %struct.ASTNode** %1048, align 8 + %1050 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1051 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1050, i64 -2 + %1052 = bitcast %union.YYSTYPE* %1051 to %struct.ASTNode** + %1053 = load %struct.ASTNode*, %struct.ASTNode** %1052, align 8 + %1054 = call %struct.ASTNode* @new_node(i32 noundef 18, %struct.ASTNode* noundef %1049, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1053, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1055 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1054, %struct.ASTNode** %1055, align 8 + br label %1246 + +1056: ; preds = %339 + %1057 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1058 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1057, i64 -1 + %1059 = bitcast %union.YYSTYPE* %1058 to %struct.ASTNode** + %1060 = load %struct.ASTNode*, %struct.ASTNode** %1059, align 8 + %1061 = call %struct.ASTNode* @new_node(i32 noundef 15, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1060, i32 noundef 10, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1062 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1061, %struct.ASTNode** %1062, align 8 + br label %1246 + +1063: ; preds = %339 + %1064 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1065 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1064, i64 0 + %1066 = bitcast %union.YYSTYPE* %1065 to %struct.ASTNode** + %1067 = load %struct.ASTNode*, %struct.ASTNode** %1066, align 8 + %1068 = call %struct.ASTNode* @new_node(i32 noundef 15, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1067, i32 noundef 14, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1069 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1068, %struct.ASTNode** %1069, align 8 + br label %1246 + +1070: ; preds = %339 + %1071 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1072 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1071, i64 0 + %1073 = bitcast %union.YYSTYPE* %1072 to i32* + %1074 = load i32, i32* %1073, align 8 + %1075 = call %struct.ASTNode* @new_node(i32 noundef 15, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, i32 noundef %1074, float noundef 0.000000e+00, i8* noundef null, i32 noundef 40) + %1076 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1075, %struct.ASTNode** %1076, align 8 + br label %1246 + +1077: ; preds = %339 + %1078 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1079 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1078, i64 0 + %1080 = bitcast %union.YYSTYPE* %1079 to float* + %1081 = load float, float* %1080, align 8 + %1082 = call %struct.ASTNode* @new_node(i32 noundef 15, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, i32 noundef 0, float noundef %1081, i8* noundef null, i32 noundef 39) + %1083 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1082, %struct.ASTNode** %1083, align 8 + br label %1246 + +1084: ; preds = %339 + %1085 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1086 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1085, i64 0 + %1087 = bitcast %union.YYSTYPE* %1086 to %struct.ASTNode** + %1088 = load %struct.ASTNode*, %struct.ASTNode** %1087, align 8 + %1089 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1090 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1089, i64 -1 + %1091 = bitcast %union.YYSTYPE* %1090 to i8** + %1092 = load i8*, i8** %1091, align 8 + %1093 = call %struct.ASTNode* @new_node(i32 noundef 14, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1088, i32 noundef 0, float noundef 0.000000e+00, i8* noundef %1092, i32 noundef 38) + %1094 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1093, %struct.ASTNode** %1094, align 8 + br label %1246 + +1095: ; preds = %339 + %1096 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1097 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1096, i64 0 + %1098 = bitcast %union.YYSTYPE* %1097 to %struct.ASTNode** + %1099 = load %struct.ASTNode*, %struct.ASTNode** %1098, align 8 + %1100 = call %struct.ASTNode* @new_node(i32 noundef 24, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1099, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1101 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1100, %struct.ASTNode** %1101, align 8 + br label %1246 + +1102: ; preds = %339 + %1103 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1104 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1103, i64 0 + %1105 = bitcast %union.YYSTYPE* %1104 to %struct.ASTNode** + %1106 = load %struct.ASTNode*, %struct.ASTNode** %1105, align 8 + %1107 = call %struct.ASTNode* @new_node(i32 noundef 24, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1106, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1108 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1107, %struct.ASTNode** %1108, align 8 + br label %1246 + +1109: ; preds = %339 + %1110 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1111 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1110, i64 0 + %1112 = bitcast %union.YYSTYPE* %1111 to %struct.ASTNode** + %1113 = load %struct.ASTNode*, %struct.ASTNode** %1112, align 8 + %1114 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1115 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1114, i64 -2 + %1116 = bitcast %union.YYSTYPE* %1115 to %struct.ASTNode** + %1117 = load %struct.ASTNode*, %struct.ASTNode** %1116, align 8 + %1118 = call %struct.ASTNode* @new_node(i32 noundef 24, %struct.ASTNode* noundef %1113, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1117, i32 noundef 287, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1119 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1118, %struct.ASTNode** %1119, align 8 + br label %1246 + +1120: ; preds = %339 + %1121 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1122 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1121, i64 0 + %1123 = bitcast %union.YYSTYPE* %1122 to %struct.ASTNode** + %1124 = load %struct.ASTNode*, %struct.ASTNode** %1123, align 8 + %1125 = call %struct.ASTNode* @new_node(i32 noundef 24, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1124, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1126 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1125, %struct.ASTNode** %1126, align 8 + br label %1246 + +1127: ; preds = %339 + %1128 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1129 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1128, i64 0 + %1130 = bitcast %union.YYSTYPE* %1129 to %struct.ASTNode** + %1131 = load %struct.ASTNode*, %struct.ASTNode** %1130, align 8 + %1132 = call %struct.ASTNode* @new_node(i32 noundef 22, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1131, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1133 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1132, %struct.ASTNode** %1133, align 8 + br label %1246 + +1134: ; preds = %339 + %1135 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1136 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1135, i64 0 + %1137 = bitcast %union.YYSTYPE* %1136 to %struct.ASTNode** + %1138 = load %struct.ASTNode*, %struct.ASTNode** %1137, align 8 + %1139 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1140 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1139, i64 -2 + %1141 = bitcast %union.YYSTYPE* %1140 to %struct.ASTNode** + %1142 = load %struct.ASTNode*, %struct.ASTNode** %1141, align 8 + %1143 = call %struct.ASTNode* @new_node(i32 noundef 22, %struct.ASTNode* noundef %1138, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1142, i32 noundef 286, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1144 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1143, %struct.ASTNode** %1144, align 8 + br label %1246 + +1145: ; preds = %339 + %1146 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1147 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1146, i64 -1 + %1148 = bitcast %union.YYSTYPE* %1147 to %struct.ASTNode** + %1149 = load %struct.ASTNode*, %struct.ASTNode** %1148, align 8 + %1150 = call %struct.ASTNode* @new_node(i32 noundef 23, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1149, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1151 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1150, %struct.ASTNode** %1151, align 8 + br label %1246 + +1152: ; preds = %339 + %1153 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1154 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1153, i64 0 + %1155 = bitcast %union.YYSTYPE* %1154 to %struct.ASTNode** + %1156 = load %struct.ASTNode*, %struct.ASTNode** %1155, align 8 + %1157 = call %struct.ASTNode* @new_node(i32 noundef 21, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1156, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1158 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1157, %struct.ASTNode** %1158, align 8 + br label %1246 + +1159: ; preds = %339 + %1160 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1161 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1160, i64 0 + %1162 = bitcast %union.YYSTYPE* %1161 to %struct.ASTNode** + %1163 = load %struct.ASTNode*, %struct.ASTNode** %1162, align 8 + %1164 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1165 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1164, i64 -2 + %1166 = bitcast %union.YYSTYPE* %1165 to %struct.ASTNode** + %1167 = load %struct.ASTNode*, %struct.ASTNode** %1166, align 8 + %1168 = call %struct.ASTNode* @new_node(i32 noundef 21, %struct.ASTNode* noundef %1163, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1167, i32 noundef 288, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1169 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1168, %struct.ASTNode** %1169, align 8 + br label %1246 + +1170: ; preds = %339 + %1171 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1172 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1171, i64 0 + %1173 = bitcast %union.YYSTYPE* %1172 to %struct.ASTNode** + %1174 = load %struct.ASTNode*, %struct.ASTNode** %1173, align 8 + %1175 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1176 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1175, i64 -2 + %1177 = bitcast %union.YYSTYPE* %1176 to %struct.ASTNode** + %1178 = load %struct.ASTNode*, %struct.ASTNode** %1177, align 8 + %1179 = call %struct.ASTNode* @new_node(i32 noundef 21, %struct.ASTNode* noundef %1174, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1178, i32 noundef 289, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1180 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1179, %struct.ASTNode** %1180, align 8 + br label %1246 + +1181: ; preds = %339 + %1182 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1183 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1182, i64 0 + %1184 = bitcast %union.YYSTYPE* %1183 to %struct.ASTNode** + %1185 = load %struct.ASTNode*, %struct.ASTNode** %1184, align 8 + %1186 = call %struct.ASTNode* @new_node(i32 noundef 20, %struct.ASTNode* noundef null, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1185, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1187 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1186, %struct.ASTNode** %1187, align 8 + br label %1246 + +1188: ; preds = %339 + %1189 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1190 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1189, i64 0 + %1191 = bitcast %union.YYSTYPE* %1190 to %struct.ASTNode** + %1192 = load %struct.ASTNode*, %struct.ASTNode** %1191, align 8 + %1193 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1194 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1193, i64 -2 + %1195 = bitcast %union.YYSTYPE* %1194 to %struct.ASTNode** + %1196 = load %struct.ASTNode*, %struct.ASTNode** %1195, align 8 + %1197 = call %struct.ASTNode* @new_node(i32 noundef 20, %struct.ASTNode* noundef %1192, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1196, i32 noundef 290, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1198 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1197, %struct.ASTNode** %1198, align 8 + br label %1246 + +1199: ; preds = %339 + %1200 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1201 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1200, i64 0 + %1202 = bitcast %union.YYSTYPE* %1201 to %struct.ASTNode** + %1203 = load %struct.ASTNode*, %struct.ASTNode** %1202, align 8 + %1204 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1205 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1204, i64 -2 + %1206 = bitcast %union.YYSTYPE* %1205 to %struct.ASTNode** + %1207 = load %struct.ASTNode*, %struct.ASTNode** %1206, align 8 + %1208 = call %struct.ASTNode* @new_node(i32 noundef 20, %struct.ASTNode* noundef %1203, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1207, i32 noundef 292, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1209 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1208, %struct.ASTNode** %1209, align 8 + br label %1246 + +1210: ; preds = %339 + %1211 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1212 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1211, i64 0 + %1213 = bitcast %union.YYSTYPE* %1212 to %struct.ASTNode** + %1214 = load %struct.ASTNode*, %struct.ASTNode** %1213, align 8 + %1215 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1216 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1215, i64 -2 + %1217 = bitcast %union.YYSTYPE* %1216 to %struct.ASTNode** + %1218 = load %struct.ASTNode*, %struct.ASTNode** %1217, align 8 + %1219 = call %struct.ASTNode* @new_node(i32 noundef 20, %struct.ASTNode* noundef %1214, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1218, i32 noundef 291, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1220 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1219, %struct.ASTNode** %1220, align 8 + br label %1246 + +1221: ; preds = %339 + %1222 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1223 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1222, i64 0 + %1224 = bitcast %union.YYSTYPE* %1223 to %struct.ASTNode** + %1225 = load %struct.ASTNode*, %struct.ASTNode** %1224, align 8 + %1226 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1227 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1226, i64 -2 + %1228 = bitcast %union.YYSTYPE* %1227 to %struct.ASTNode** + %1229 = load %struct.ASTNode*, %struct.ASTNode** %1228, align 8 + %1230 = call %struct.ASTNode* @new_node(i32 noundef 20, %struct.ASTNode* noundef %1225, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1229, i32 noundef 293, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1231 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1230, %struct.ASTNode** %1231, align 8 + br label %1246 + +1232: ; preds = %339 + %1233 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* null, %struct.ASTNode** %1233, align 8 + br label %1246 + +1234: ; preds = %339 + %1235 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1236 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1235, i64 0 + %1237 = bitcast %union.YYSTYPE* %1236 to %struct.ASTNode** + %1238 = load %struct.ASTNode*, %struct.ASTNode** %1237, align 8 + %1239 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1240 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1239, i64 -2 + %1241 = bitcast %union.YYSTYPE* %1240 to %struct.ASTNode** + %1242 = load %struct.ASTNode*, %struct.ASTNode** %1241, align 8 + %1243 = call %struct.ASTNode* @new_node(i32 noundef 9, %struct.ASTNode* noundef %1238, %struct.ASTNode* noundef null, %struct.ASTNode* noundef %1242, i32 noundef 0, float noundef 0.000000e+00, i8* noundef null, i32 noundef 38) + %1244 = bitcast %union.YYSTYPE* %16 to %struct.ASTNode** + store %struct.ASTNode* %1243, %struct.ASTNode** %1244, align 8 + br label %1246 + +1245: ; preds = %339 + br label %1246 + +1246: ; preds = %1245, %1234, %1232, %1221, %1210, %1199, %1188, %1181, %1170, %1159, %1152, %1145, %1134, %1127, %1120, %1109, %1102, %1095, %1084, %1077, %1070, %1063, %1056, %1045, %1038, %1031, %1024, %1017, %1006, %999, %992, %981, %970, %959, %952, %941, %930, %923, %916, %909, %907, %905, %903, %892, %877, %866, %859, %857, %850, %839, %828, %817, %806, %804, %797, %782, %767, %756, %745, %734, %723, %716, %709, %694, %679, %664, %653, %642, %631, %620, %613, %606, %603, %596, %577, %562, %547, %536, %529, %522, %511, %500, %493, %482, %475, %472, %465, %454, %452, %433, %418, %411, %404, %393, %382, %371, %364, %357, %350, %344 + %1247 = load i32, i32* %22, align 4 + %1248 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1249 = sext i32 %1247 to i64 + %1250 = sub i64 0, %1249 + %1251 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1248, i64 %1250 + store %union.YYSTYPE* %1251, %union.YYSTYPE** %9, align 8 + %1252 = load i32, i32* %22, align 4 + %1253 = load i8*, i8** %6, align 8 + %1254 = sext i32 %1252 to i64 + %1255 = sub i64 0, %1254 + %1256 = getelementptr inbounds i8, i8* %1253, i64 %1255 + store i8* %1256, i8** %6, align 8 + %1257 = load i32, i32* %22, align 4 + %1258 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %1259 = sext i32 %1257 to i64 + %1260 = sub i64 0, %1259 + %1261 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1258, i64 %1260 + store %struct.YYLTYPE* %1261, %struct.YYLTYPE** %12, align 8 + store i32 0, i32* %22, align 4 + %1262 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1263 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1262, i32 1 + store %union.YYSTYPE* %1263, %union.YYSTYPE** %9, align 8 + %1264 = bitcast %union.YYSTYPE* %1263 to i8* + %1265 = bitcast %union.YYSTYPE* %16 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %1264, i8* align 8 %1265, i64 8, i1 false) + %1266 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %1267 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1266, i32 1 + store %struct.YYLTYPE* %1267, %struct.YYLTYPE** %12, align 8 + %1268 = bitcast %struct.YYLTYPE* %1267 to i8* + %1269 = bitcast %struct.YYLTYPE* %17 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %1268, i8* align 4 %1269, i64 16, i1 false) + %1270 = load i32, i32* %13, align 4 + %1271 = sext i32 %1270 to i64 + %1272 = getelementptr inbounds [101 x i8], [101 x i8]* @yyr1, i64 0, i64 %1271 + %1273 = load i8, i8* %1272, align 1 + %1274 = sext i8 %1273 to i32 + %1275 = sub nsw i32 %1274, 41 + store i32 %1275, i32* %29, align 4 + %1276 = load i32, i32* %29, align 4 + %1277 = sext i32 %1276 to i64 + %1278 = getelementptr inbounds [31 x i16], [31 x i16]* @yypgoto, i64 0, i64 %1277 + %1279 = load i16, i16* %1278, align 2 + %1280 = sext i16 %1279 to i32 + %1281 = load i8*, i8** %6, align 8 + %1282 = load i8, i8* %1281, align 1 + %1283 = zext i8 %1282 to i32 + %1284 = add nsw i32 %1280, %1283 + store i32 %1284, i32* %30, align 4 + %1285 = load i32, i32* %30, align 4 + %1286 = icmp sle i32 0, %1285 + br i1 %1286, label %1287, label %1306 + +1287: ; preds = %1246 + %1288 = load i32, i32* %30, align 4 + %1289 = icmp sle i32 %1288, 301 + br i1 %1289, label %1290, label %1306 + +1290: ; preds = %1287 + %1291 = load i32, i32* %30, align 4 + %1292 = sext i32 %1291 to i64 + %1293 = getelementptr inbounds [302 x i16], [302 x i16]* @yycheck, i64 0, i64 %1292 + %1294 = load i16, i16* %1293, align 2 + %1295 = sext i16 %1294 to i32 + %1296 = load i8*, i8** %6, align 8 + %1297 = load i8, i8* %1296, align 1 + %1298 = zext i8 %1297 to i32 + %1299 = icmp eq i32 %1295, %1298 + br i1 %1299, label %1300, label %1306 + +1300: ; preds = %1290 + %1301 = load i32, i32* %30, align 4 + %1302 = sext i32 %1301 to i64 + %1303 = getelementptr inbounds [302 x i16], [302 x i16]* @yytable, i64 0, i64 %1302 + %1304 = load i16, i16* %1303, align 2 + %1305 = sext i16 %1304 to i32 + br label %1312 + +1306: ; preds = %1290, %1287, %1246 + %1307 = load i32, i32* %29, align 4 + %1308 = sext i32 %1307 to i64 + %1309 = getelementptr inbounds [31 x i8], [31 x i8]* @yydefgoto, i64 0, i64 %1308 + %1310 = load i8, i8* %1309, align 1 + %1311 = zext i8 %1310 to i32 + br label %1312 + +1312: ; preds = %1306, %1300 + %1313 = phi i32 [ %1305, %1300 ], [ %1311, %1306 ] + store i32 %1313, i32* %1, align 4 + br label %44 + +1314: ; preds = %258 + %1315 = load i32, i32* @yychar, align 4 + %1316 = icmp eq i32 %1315, -2 + br i1 %1316, label %1317, label %1318 + +1317: ; preds = %1314 + br label %1333 + +1318: ; preds = %1314 + %1319 = load i32, i32* @yychar, align 4 + %1320 = icmp sle i32 0, %1319 + br i1 %1320, label %1321, label %1330 + +1321: ; preds = %1318 + %1322 = load i32, i32* @yychar, align 4 + %1323 = icmp sle i32 %1322, 295 + br i1 %1323, label %1324, label %1330 + +1324: ; preds = %1321 + %1325 = load i32, i32* @yychar, align 4 + %1326 = sext i32 %1325 to i64 + %1327 = getelementptr inbounds [296 x i8], [296 x i8]* @yytranslate, i64 0, i64 %1326 + %1328 = load i8, i8* %1327, align 1 + %1329 = sext i8 %1328 to i32 + br label %1331 + +1330: ; preds = %1321, %1318 + br label %1331 + +1331: ; preds = %1330, %1324 + %1332 = phi i32 [ %1329, %1324 ], [ 2, %1330 ] + br label %1333 + +1333: ; preds = %1331, %1317 + %1334 = phi i32 [ -2, %1317 ], [ %1332, %1331 ] + store i32 %1334, i32* %15, align 4 + %1335 = load i32, i32* %2, align 4 + %1336 = icmp ne i32 %1335, 0 + br i1 %1336, label %1377, label %1337 + +1337: ; preds = %1333 + %1338 = load i32, i32* @yynerrs, align 4 + %1339 = add nsw i32 %1338, 1 + store i32 %1339, i32* @yynerrs, align 4 + %1340 = getelementptr inbounds %struct.yypcontext_t, %struct.yypcontext_t* %31, i32 0, i32 0 + %1341 = load i8*, i8** %6, align 8 + store i8* %1341, i8** %1340, align 8 + %1342 = getelementptr inbounds %struct.yypcontext_t, %struct.yypcontext_t* %31, i32 0, i32 1 + %1343 = load i32, i32* %15, align 4 + store i32 %1343, i32* %1342, align 8 + %1344 = getelementptr inbounds %struct.yypcontext_t, %struct.yypcontext_t* %31, i32 0, i32 2 + store %struct.YYLTYPE* @yylloc, %struct.YYLTYPE** %1344, align 8 + store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %32, align 8 + %1345 = call i32 @yysyntax_error(i64* noundef %21, i8** noundef %20, %struct.yypcontext_t* noundef %31) + store i32 %1345, i32* %33, align 4 + %1346 = load i32, i32* %33, align 4 + %1347 = icmp eq i32 %1346, 0 + br i1 %1347, label %1348, label %1350 + +1348: ; preds = %1337 + %1349 = load i8*, i8** %20, align 8 + store i8* %1349, i8** %32, align 8 + br label %1371 + +1350: ; preds = %1337 + %1351 = load i32, i32* %33, align 4 + %1352 = icmp eq i32 %1351, -1 + br i1 %1352, label %1353, label %1370 + +1353: ; preds = %1350 + %1354 = load i8*, i8** %20, align 8 + %1355 = getelementptr inbounds [128 x i8], [128 x i8]* %19, i64 0, i64 0 + %1356 = icmp ne i8* %1354, %1355 + br i1 %1356, label %1357, label %1359 + +1357: ; preds = %1353 + %1358 = load i8*, i8** %20, align 8 + call void @free(i8* noundef %1358) #6 + br label %1359 + +1359: ; preds = %1357, %1353 + %1360 = load i64, i64* %21, align 8 + %1361 = call noalias i8* @malloc(i64 noundef %1360) #6 + store i8* %1361, i8** %20, align 8 + %1362 = load i8*, i8** %20, align 8 + %1363 = icmp ne i8* %1362, null + br i1 %1363, label %1364, label %1367 + +1364: ; preds = %1359 + %1365 = call i32 @yysyntax_error(i64* noundef %21, i8** noundef %20, %struct.yypcontext_t* noundef %31) + store i32 %1365, i32* %33, align 4 + %1366 = load i8*, i8** %20, align 8 + store i8* %1366, i8** %32, align 8 + br label %1369 + +1367: ; preds = %1359 + %1368 = getelementptr inbounds [128 x i8], [128 x i8]* %19, i64 0, i64 0 + store i8* %1368, i8** %20, align 8 + store i64 128, i64* %21, align 8 + store i32 -2, i32* %33, align 4 + br label %1369 + +1369: ; preds = %1367, %1364 + br label %1370 + +1370: ; preds = %1369, %1350 + br label %1371 + +1371: ; preds = %1370, %1348 + %1372 = load i8*, i8** %32, align 8 + call void (i8*, ...) @yyerror(i8* noundef %1372) + %1373 = load i32, i32* %33, align 4 + %1374 = icmp eq i32 %1373, -2 + br i1 %1374, label %1375, label %1376 + +1375: ; preds = %1371 + br label %1509 + +1376: ; preds = %1371 + br label %1377 + +1377: ; preds = %1376, %1333 + %1378 = getelementptr inbounds [3 x %struct.YYLTYPE], [3 x %struct.YYLTYPE]* %18, i64 0, i64 1 + %1379 = bitcast %struct.YYLTYPE* %1378 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 %1379, i8* align 4 bitcast (%struct.YYLTYPE* @yylloc to i8*), i64 16, i1 false) + %1380 = load i32, i32* %2, align 4 + %1381 = icmp eq i32 %1380, 3 + br i1 %1381, label %1382, label %1393 + +1382: ; preds = %1377 + %1383 = load i32, i32* @yychar, align 4 + %1384 = icmp sle i32 %1383, 0 + br i1 %1384, label %1385, label %1390 + +1385: ; preds = %1382 + %1386 = load i32, i32* @yychar, align 4 + %1387 = icmp eq i32 %1386, 0 + br i1 %1387, label %1388, label %1389 + +1388: ; preds = %1385 + br label %1508 + +1389: ; preds = %1385 + br label %1392 + +1390: ; preds = %1382 + %1391 = load i32, i32* %15, align 4 + call void @yydestruct(i8* noundef getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 noundef %1391, %union.YYSTYPE* noundef @yylval, %struct.YYLTYPE* noundef @yylloc) + store i32 -2, i32* @yychar, align 4 + br label %1392 + +1392: ; preds = %1390, %1389 + br label %1393 + +1393: ; preds = %1392, %1377 + br label %1415 + +1394: ; No predecessors! + %1395 = load i32, i32* @yynerrs, align 4 + %1396 = add nsw i32 %1395, 1 + store i32 %1396, i32* @yynerrs, align 4 + %1397 = load i32, i32* %22, align 4 + %1398 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1399 = sext i32 %1397 to i64 + %1400 = sub i64 0, %1399 + %1401 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1398, i64 %1400 + store %union.YYSTYPE* %1401, %union.YYSTYPE** %9, align 8 + %1402 = load i32, i32* %22, align 4 + %1403 = load i8*, i8** %6, align 8 + %1404 = sext i32 %1402 to i64 + %1405 = sub i64 0, %1404 + %1406 = getelementptr inbounds i8, i8* %1403, i64 %1405 + store i8* %1406, i8** %6, align 8 + %1407 = load i32, i32* %22, align 4 + %1408 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %1409 = sext i32 %1407 to i64 + %1410 = sub i64 0, %1409 + %1411 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1408, i64 %1410 + store %struct.YYLTYPE* %1411, %struct.YYLTYPE** %12, align 8 + store i32 0, i32* %22, align 4 + %1412 = load i8*, i8** %6, align 8 + %1413 = load i8, i8* %1412, align 1 + %1414 = zext i8 %1413 to i32 + store i32 %1414, i32* %1, align 4 + br label %1415 + +1415: ; preds = %1394, %1393, %188 + store i32 3, i32* %2, align 4 + br label %1416 + +1416: ; preds = %1455, %1415 + %1417 = load i32, i32* %1, align 4 + %1418 = sext i32 %1417 to i64 + %1419 = getelementptr inbounds [207 x i16], [207 x i16]* @yypact, i64 0, i64 %1418 + %1420 = load i16, i16* %1419, align 2 + %1421 = sext i16 %1420 to i32 + store i32 %1421, i32* %13, align 4 + %1422 = load i32, i32* %13, align 4 + %1423 = icmp eq i32 %1422, -167 + br i1 %1423, label %1450, label %1424 + +1424: ; preds = %1416 + %1425 = load i32, i32* %13, align 4 + %1426 = add nsw i32 %1425, 1 + store i32 %1426, i32* %13, align 4 + %1427 = load i32, i32* %13, align 4 + %1428 = icmp sle i32 0, %1427 + br i1 %1428, label %1429, label %1449 + +1429: ; preds = %1424 + %1430 = load i32, i32* %13, align 4 + %1431 = icmp sle i32 %1430, 301 + br i1 %1431, label %1432, label %1449 + +1432: ; preds = %1429 + %1433 = load i32, i32* %13, align 4 + %1434 = sext i32 %1433 to i64 + %1435 = getelementptr inbounds [302 x i16], [302 x i16]* @yycheck, i64 0, i64 %1434 + %1436 = load i16, i16* %1435, align 2 + %1437 = sext i16 %1436 to i32 + %1438 = icmp eq i32 %1437, 1 + br i1 %1438, label %1439, label %1449 + +1439: ; preds = %1432 + %1440 = load i32, i32* %13, align 4 + %1441 = sext i32 %1440 to i64 + %1442 = getelementptr inbounds [302 x i16], [302 x i16]* @yytable, i64 0, i64 %1441 + %1443 = load i16, i16* %1442, align 2 + %1444 = sext i16 %1443 to i32 + store i32 %1444, i32* %13, align 4 + %1445 = load i32, i32* %13, align 4 + %1446 = icmp slt i32 0, %1445 + br i1 %1446, label %1447, label %1448 + +1447: ; preds = %1439 + br label %1476 + +1448: ; preds = %1439 + br label %1449 + +1449: ; preds = %1448, %1432, %1429, %1424 + br label %1450 + +1450: ; preds = %1449, %1416 + %1451 = load i8*, i8** %6, align 8 + %1452 = load i8*, i8** %5, align 8 + %1453 = icmp eq i8* %1451, %1452 + br i1 %1453, label %1454, label %1455 + +1454: ; preds = %1450 + br label %1508 + +1455: ; preds = %1450 + %1456 = getelementptr inbounds [3 x %struct.YYLTYPE], [3 x %struct.YYLTYPE]* %18, i64 0, i64 1 + %1457 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %1458 = bitcast %struct.YYLTYPE* %1456 to i8* + %1459 = bitcast %struct.YYLTYPE* %1457 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 %1458, i8* align 4 %1459, i64 16, i1 false) + %1460 = load i32, i32* %1, align 4 + %1461 = sext i32 %1460 to i64 + %1462 = getelementptr inbounds [207 x i8], [207 x i8]* @yystos, i64 0, i64 %1461 + %1463 = load i8, i8* %1462, align 1 + %1464 = sext i8 %1463 to i32 + %1465 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1466 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + call void @yydestruct(i8* noundef getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 noundef %1464, %union.YYSTYPE* noundef %1465, %struct.YYLTYPE* noundef %1466) + %1467 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1468 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1467, i64 -1 + store %union.YYSTYPE* %1468, %union.YYSTYPE** %9, align 8 + %1469 = load i8*, i8** %6, align 8 + %1470 = getelementptr inbounds i8, i8* %1469, i64 -1 + store i8* %1470, i8** %6, align 8 + %1471 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %1472 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1471, i64 -1 + store %struct.YYLTYPE* %1472, %struct.YYLTYPE** %12, align 8 + %1473 = load i8*, i8** %6, align 8 + %1474 = load i8, i8* %1473, align 1 + %1475 = zext i8 %1474 to i32 + store i32 %1475, i32* %1, align 4 + br label %1416 + +1476: ; preds = %1447 + %1477 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1478 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1477, i32 1 + store %union.YYSTYPE* %1478, %union.YYSTYPE** %9, align 8 + %1479 = bitcast %union.YYSTYPE* %1478 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %1479, i8* align 8 bitcast (%union.YYSTYPE* @yylval to i8*), i64 8, i1 false) + %1480 = getelementptr inbounds [3 x %struct.YYLTYPE], [3 x %struct.YYLTYPE]* %18, i64 0, i64 2 + %1481 = bitcast %struct.YYLTYPE* %1480 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 %1481, i8* align 4 bitcast (%struct.YYLTYPE* @yylloc to i8*), i64 16, i1 false) + %1482 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %1483 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1482, i32 1 + store %struct.YYLTYPE* %1483, %struct.YYLTYPE** %12, align 8 + br label %1484 + +1484: ; preds = %1476 + %1485 = getelementptr inbounds [3 x %struct.YYLTYPE], [3 x %struct.YYLTYPE]* %18, i64 0, i64 1 + %1486 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1485, i32 0, i32 0 + %1487 = load i32, i32* %1486, align 16 + %1488 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %1489 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1488, i32 0, i32 0 + store i32 %1487, i32* %1489, align 4 + %1490 = getelementptr inbounds [3 x %struct.YYLTYPE], [3 x %struct.YYLTYPE]* %18, i64 0, i64 1 + %1491 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1490, i32 0, i32 1 + %1492 = load i32, i32* %1491, align 4 + %1493 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %1494 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1493, i32 0, i32 1 + store i32 %1492, i32* %1494, align 4 + %1495 = getelementptr inbounds [3 x %struct.YYLTYPE], [3 x %struct.YYLTYPE]* %18, i64 0, i64 2 + %1496 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1495, i32 0, i32 2 + %1497 = load i32, i32* %1496, align 8 + %1498 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %1499 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1498, i32 0, i32 2 + store i32 %1497, i32* %1499, align 4 + %1500 = getelementptr inbounds [3 x %struct.YYLTYPE], [3 x %struct.YYLTYPE]* %18, i64 0, i64 2 + %1501 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1500, i32 0, i32 3 + %1502 = load i32, i32* %1501, align 4 + %1503 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %1504 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1503, i32 0, i32 3 + store i32 %1502, i32* %1504, align 4 + br label %1505 + +1505: ; preds = %1484 + %1506 = load i32, i32* %13, align 4 + store i32 %1506, i32* %1, align 4 + br label %44 + +1507: ; preds = %165 + store i32 0, i32* %14, align 4 + br label %1510 + +1508: ; preds = %1454, %1388, %160 + store i32 1, i32* %14, align 4 + br label %1510 + +1509: ; preds = %1375, %82, %66 + call void (i8*, ...) @yyerror(i8* noundef getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0)) + store i32 2, i32* %14, align 4 + br label %1510 + +1510: ; preds = %1509, %1508, %1507 + %1511 = load i32, i32* @yychar, align 4 + %1512 = icmp ne i32 %1511, -2 + br i1 %1512, label %1513, label %1529 + +1513: ; preds = %1510 + %1514 = load i32, i32* @yychar, align 4 + %1515 = icmp sle i32 0, %1514 + br i1 %1515, label %1516, label %1525 + +1516: ; preds = %1513 + %1517 = load i32, i32* @yychar, align 4 + %1518 = icmp sle i32 %1517, 295 + br i1 %1518, label %1519, label %1525 + +1519: ; preds = %1516 + %1520 = load i32, i32* @yychar, align 4 + %1521 = sext i32 %1520 to i64 + %1522 = getelementptr inbounds [296 x i8], [296 x i8]* @yytranslate, i64 0, i64 %1521 + %1523 = load i8, i8* %1522, align 1 + %1524 = sext i8 %1523 to i32 + br label %1526 + +1525: ; preds = %1516, %1513 + br label %1526 + +1526: ; preds = %1525, %1519 + %1527 = phi i32 [ %1524, %1519 ], [ 2, %1525 ] + store i32 %1527, i32* %15, align 4 + %1528 = load i32, i32* %15, align 4 + call void @yydestruct(i8* noundef getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 noundef %1528, %union.YYSTYPE* noundef @yylval, %struct.YYLTYPE* noundef @yylloc) + br label %1529 + +1529: ; preds = %1526, %1510 + %1530 = load i32, i32* %22, align 4 + %1531 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1532 = sext i32 %1530 to i64 + %1533 = sub i64 0, %1532 + %1534 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1531, i64 %1533 + store %union.YYSTYPE* %1534, %union.YYSTYPE** %9, align 8 + %1535 = load i32, i32* %22, align 4 + %1536 = load i8*, i8** %6, align 8 + %1537 = sext i32 %1535 to i64 + %1538 = sub i64 0, %1537 + %1539 = getelementptr inbounds i8, i8* %1536, i64 %1538 + store i8* %1539, i8** %6, align 8 + %1540 = load i32, i32* %22, align 4 + %1541 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %1542 = sext i32 %1540 to i64 + %1543 = sub i64 0, %1542 + %1544 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1541, i64 %1543 + store %struct.YYLTYPE* %1544, %struct.YYLTYPE** %12, align 8 + br label %1545 + +1545: ; preds = %1549, %1529 + %1546 = load i8*, i8** %6, align 8 + %1547 = load i8*, i8** %5, align 8 + %1548 = icmp ne i8* %1546, %1547 + br i1 %1548, label %1549, label %1565 + +1549: ; preds = %1545 + %1550 = load i8*, i8** %6, align 8 + %1551 = load i8, i8* %1550, align 1 + %1552 = zext i8 %1551 to i32 + %1553 = sext i32 %1552 to i64 + %1554 = getelementptr inbounds [207 x i8], [207 x i8]* @yystos, i64 0, i64 %1553 + %1555 = load i8, i8* %1554, align 1 + %1556 = sext i8 %1555 to i32 + %1557 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1558 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + call void @yydestruct(i8* noundef getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 noundef %1556, %union.YYSTYPE* noundef %1557, %struct.YYLTYPE* noundef %1558) + %1559 = load %union.YYSTYPE*, %union.YYSTYPE** %9, align 8 + %1560 = getelementptr inbounds %union.YYSTYPE, %union.YYSTYPE* %1559, i64 -1 + store %union.YYSTYPE* %1560, %union.YYSTYPE** %9, align 8 + %1561 = load i8*, i8** %6, align 8 + %1562 = getelementptr inbounds i8, i8* %1561, i64 -1 + store i8* %1562, i8** %6, align 8 + %1563 = load %struct.YYLTYPE*, %struct.YYLTYPE** %12, align 8 + %1564 = getelementptr inbounds %struct.YYLTYPE, %struct.YYLTYPE* %1563, i64 -1 + store %struct.YYLTYPE* %1564, %struct.YYLTYPE** %12, align 8 + br label %1545, !llvm.loop !6 + +1565: ; preds = %1545 + %1566 = load i8*, i8** %5, align 8 + %1567 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0 + %1568 = icmp ne i8* %1566, %1567 + br i1 %1568, label %1569, label %1571 + +1569: ; preds = %1565 + %1570 = load i8*, i8** %5, align 8 + call void @free(i8* noundef %1570) #6 + br label %1571 + +1571: ; preds = %1569, %1565 + %1572 = load i8*, i8** %20, align 8 + %1573 = getelementptr inbounds [128 x i8], [128 x i8]* %19, i64 0, i64 0 + %1574 = icmp ne i8* %1572, %1573 + br i1 %1574, label %1575, label %1577 + +1575: ; preds = %1571 + %1576 = load i8*, i8** %20, align 8 + call void @free(i8* noundef %1576) #6 + br label %1577 + +1577: ; preds = %1575, %1571 + %1578 = load i32, i32* %14, align 4 + ret i32 %1578 +} + +; Function Attrs: argmemonly nofree nounwind willreturn +declare void @llvm.memcpy.p0i8.p0i8.i64(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i64, i1 immarg) #1 + +; Function Attrs: nounwind +declare noalias i8* @malloc(i64 noundef) #2 + +; Function Attrs: nounwind +declare void @free(i8* noundef) #2 + +declare i32 @yylex(...) #3 + +declare %struct.ASTNode* @new_node(i32 noundef, %struct.ASTNode* noundef, %struct.ASTNode* noundef, %struct.ASTNode* noundef, i32 noundef, float noundef, i8* noundef, i32 noundef) #3 + +; Function Attrs: noinline nounwind optnone uwtable +define internal i32 @yysyntax_error(i64* noundef %0, i8** noundef %1, %struct.yypcontext_t* noundef %2) #0 { + %4 = alloca i32, align 4 + %5 = alloca i64*, align 8 + %6 = alloca i8**, align 8 + %7 = alloca %struct.yypcontext_t*, align 8 + %8 = alloca i8*, align 8 + %9 = alloca [5 x i32], align 16 + %10 = alloca i64, align 8 + %11 = alloca i32, align 4 + %12 = alloca i32, align 4 + %13 = alloca i64, align 8 + %14 = alloca i8*, align 8 + %15 = alloca i32, align 4 + store i64* %0, i64** %5, align 8 + store i8** %1, i8*** %6, align 8 + store %struct.yypcontext_t* %2, %struct.yypcontext_t** %7, align 8 + store i8* null, i8** %8, align 8 + store i64 0, i64* %10, align 8 + %16 = load %struct.yypcontext_t*, %struct.yypcontext_t** %7, align 8 + %17 = getelementptr inbounds [5 x i32], [5 x i32]* %9, i64 0, i64 0 + %18 = call i32 @yy_syntax_error_arguments(%struct.yypcontext_t* noundef %16, i32* noundef %17, i32 noundef 5) + store i32 %18, i32* %11, align 4 + %19 = load i32, i32* %11, align 4 + %20 = icmp eq i32 %19, -2 + br i1 %20, label %21, label %22 + +21: ; preds = %3 + store i32 -2, i32* %4, align 4 + br label %133 + +22: ; preds = %3 + %23 = load i32, i32* %11, align 4 + switch i32 %23, label %24 [ + i32 0, label %25 + i32 1, label %26 + i32 2, label %27 + i32 3, label %28 + i32 4, label %29 + i32 5, label %30 + ] + +24: ; preds = %22 + br label %25 + +25: ; preds = %22, %24 + store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %8, align 8 + br label %31 + +26: ; preds = %22 + store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i8** %8, align 8 + br label %31 + +27: ; preds = %22 + store i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.10, i64 0, i64 0), i8** %8, align 8 + br label %31 + +28: ; preds = %22 + store i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.11, i64 0, i64 0), i8** %8, align 8 + br label %31 + +29: ; preds = %22 + store i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.12, i64 0, i64 0), i8** %8, align 8 + br label %31 + +30: ; preds = %22 + store i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.13, i64 0, i64 0), i8** %8, align 8 + br label %31 + +31: ; preds = %30, %29, %28, %27, %26, %25 + %32 = load i8*, i8** %8, align 8 + %33 = call i64 @strlen(i8* noundef %32) #7 + %34 = load i32, i32* %11, align 4 + %35 = mul nsw i32 2, %34 + %36 = sext i32 %35 to i64 + %37 = sub nsw i64 %33, %36 + %38 = add nsw i64 %37, 1 + store i64 %38, i64* %10, align 8 + store i32 0, i32* %12, align 4 + br label %39 + +39: ; preds = %64, %31 + %40 = load i32, i32* %12, align 4 + %41 = load i32, i32* %11, align 4 + %42 = icmp slt i32 %40, %41 + br i1 %42, label %43, label %67 + +43: ; preds = %39 + %44 = load i64, i64* %10, align 8 + %45 = load i32, i32* %12, align 4 + %46 = sext i32 %45 to i64 + %47 = getelementptr inbounds [5 x i32], [5 x i32]* %9, i64 0, i64 %46 + %48 = load i32, i32* %47, align 4 + %49 = sext i32 %48 to i64 + %50 = getelementptr inbounds [73 x i8*], [73 x i8*]* @yytname, i64 0, i64 %49 + %51 = load i8*, i8** %50, align 8 + %52 = call i64 @yytnamerr(i8* noundef null, i8* noundef %51) + %53 = add nsw i64 %44, %52 + store i64 %53, i64* %13, align 8 + %54 = load i64, i64* %10, align 8 + %55 = load i64, i64* %13, align 8 + %56 = icmp sle i64 %54, %55 + br i1 %56, label %57, label %62 + +57: ; preds = %43 + %58 = load i64, i64* %13, align 8 + %59 = icmp sle i64 %58, 9223372036854775807 + br i1 %59, label %60, label %62 + +60: ; preds = %57 + %61 = load i64, i64* %13, align 8 + store i64 %61, i64* %10, align 8 + br label %63 + +62: ; preds = %57, %43 + store i32 -2, i32* %4, align 4 + br label %133 + +63: ; preds = %60 + br label %64 + +64: ; preds = %63 + %65 = load i32, i32* %12, align 4 + %66 = add nsw i32 %65, 1 + store i32 %66, i32* %12, align 4 + br label %39, !llvm.loop !8 + +67: ; preds = %39 + %68 = load i64*, i64** %5, align 8 + %69 = load i64, i64* %68, align 8 + %70 = load i64, i64* %10, align 8 + %71 = icmp slt i64 %69, %70 + br i1 %71, label %72, label %87 + +72: ; preds = %67 + %73 = load i64, i64* %10, align 8 + %74 = mul nsw i64 2, %73 + %75 = load i64*, i64** %5, align 8 + store i64 %74, i64* %75, align 8 + %76 = load i64, i64* %10, align 8 + %77 = load i64*, i64** %5, align 8 + %78 = load i64, i64* %77, align 8 + %79 = icmp sle i64 %76, %78 + br i1 %79, label %80, label %84 + +80: ; preds = %72 + %81 = load i64*, i64** %5, align 8 + %82 = load i64, i64* %81, align 8 + %83 = icmp sle i64 %82, 9223372036854775807 + br i1 %83, label %86, label %84 + +84: ; preds = %80, %72 + %85 = load i64*, i64** %5, align 8 + store i64 9223372036854775807, i64* %85, align 8 + br label %86 + +86: ; preds = %84, %80 + store i32 -1, i32* %4, align 4 + br label %133 + +87: ; preds = %67 + %88 = load i8**, i8*** %6, align 8 + %89 = load i8*, i8** %88, align 8 + store i8* %89, i8** %14, align 8 + store i32 0, i32* %15, align 4 + br label %90 + +90: ; preds = %131, %87 + %91 = load i8*, i8** %8, align 8 + %92 = load i8, i8* %91, align 1 + %93 = load i8*, i8** %14, align 8 + store i8 %92, i8* %93, align 1 + %94 = sext i8 %92 to i32 + %95 = icmp ne i32 %94, 0 + br i1 %95, label %96, label %132 + +96: ; preds = %90 + %97 = load i8*, i8** %14, align 8 + %98 = load i8, i8* %97, align 1 + %99 = sext i8 %98 to i32 + %100 = icmp eq i32 %99, 37 + br i1 %100, label %101, label %126 + +101: ; preds = %96 + %102 = load i8*, i8** %8, align 8 + %103 = getelementptr inbounds i8, i8* %102, i64 1 + %104 = load i8, i8* %103, align 1 + %105 = sext i8 %104 to i32 + %106 = icmp eq i32 %105, 115 + br i1 %106, label %107, label %126 + +107: ; preds = %101 + %108 = load i32, i32* %15, align 4 + %109 = load i32, i32* %11, align 4 + %110 = icmp slt i32 %108, %109 + br i1 %110, label %111, label %126 + +111: ; preds = %107 + %112 = load i8*, i8** %14, align 8 + %113 = load i32, i32* %15, align 4 + %114 = add nsw i32 %113, 1 + store i32 %114, i32* %15, align 4 + %115 = sext i32 %113 to i64 + %116 = getelementptr inbounds [5 x i32], [5 x i32]* %9, i64 0, i64 %115 + %117 = load i32, i32* %116, align 4 + %118 = sext i32 %117 to i64 + %119 = getelementptr inbounds [73 x i8*], [73 x i8*]* @yytname, i64 0, i64 %118 + %120 = load i8*, i8** %119, align 8 + %121 = call i64 @yytnamerr(i8* noundef %112, i8* noundef %120) + %122 = load i8*, i8** %14, align 8 + %123 = getelementptr inbounds i8, i8* %122, i64 %121 + store i8* %123, i8** %14, align 8 + %124 = load i8*, i8** %8, align 8 + %125 = getelementptr inbounds i8, i8* %124, i64 2 + store i8* %125, i8** %8, align 8 + br label %131 + +126: ; preds = %107, %101, %96 + %127 = load i8*, i8** %14, align 8 + %128 = getelementptr inbounds i8, i8* %127, i32 1 + store i8* %128, i8** %14, align 8 + %129 = load i8*, i8** %8, align 8 + %130 = getelementptr inbounds i8, i8* %129, i32 1 + store i8* %130, i8** %8, align 8 + br label %131 + +131: ; preds = %126, %111 + br label %90, !llvm.loop !9 + +132: ; preds = %90 + store i32 0, i32* %4, align 4 + br label %133 + +133: ; preds = %132, %86, %62, %21 + %134 = load i32, i32* %4, align 4 + ret i32 %134 +} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local void @yyerror(i8* noundef %0, ...) #0 { + %2 = alloca i8*, align 8 + %3 = alloca [1 x %struct.__va_list_tag], align 16 + store i8* %0, i8** %2, align 8 + store i32 1, i32* @syntax_error, align 4 + %4 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %3, i64 0, i64 0 + %5 = bitcast %struct.__va_list_tag* %4 to i8* + call void @llvm.va_start(i8* %5) + %6 = load %struct._IO_FILE*, %struct._IO_FILE** @stderr, align 8 + %7 = load i32, i32* @yylineno, align 4 + %8 = call i32 (%struct._IO_FILE*, i8*, ...) @fprintf(%struct._IO_FILE* noundef %6, i8* noundef getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* noundef getelementptr inbounds ([100 x i8], [100 x i8]* @filename, i64 0, i64 0), i32 noundef %7) + %9 = load %struct._IO_FILE*, %struct._IO_FILE** @stderr, align 8 + %10 = load i8*, i8** %2, align 8 + %11 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %3, i64 0, i64 0 + %12 = call i32 @vfprintf(%struct._IO_FILE* noundef %9, i8* noundef %10, %struct.__va_list_tag* noundef %11) + %13 = load %struct._IO_FILE*, %struct._IO_FILE** @stderr, align 8 + %14 = call i32 (%struct._IO_FILE*, i8*, ...) @fprintf(%struct._IO_FILE* noundef %13, i8* noundef getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0)) + ret void +} + +; Function Attrs: noinline nounwind optnone uwtable +define internal void @yydestruct(i8* noundef %0, i32 noundef %1, %union.YYSTYPE* noundef %2, %struct.YYLTYPE* noundef %3) #0 { + %5 = alloca i8*, align 8 + %6 = alloca i32, align 4 + %7 = alloca %union.YYSTYPE*, align 8 + %8 = alloca %struct.YYLTYPE*, align 8 + store i8* %0, i8** %5, align 8 + store i32 %1, i32* %6, align 4 + store %union.YYSTYPE* %2, %union.YYSTYPE** %7, align 8 + store %struct.YYLTYPE* %3, %struct.YYLTYPE** %8, align 8 + %9 = load %union.YYSTYPE*, %union.YYSTYPE** %7, align 8 + %10 = load %struct.YYLTYPE*, %struct.YYLTYPE** %8, align 8 + %11 = load i8*, i8** %5, align 8 + %12 = icmp ne i8* %11, null + br i1 %12, label %14, label %13 + +13: ; preds = %4 + store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.86, i64 0, i64 0), i8** %5, align 8 + br label %14 + +14: ; preds = %13, %4 + %15 = load i32, i32* %6, align 4 + ret void +} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @main(i32 noundef %0, i8** noundef %1) #0 { + %3 = alloca i32, align 4 + %4 = alloca i32, align 4 + %5 = alloca i8**, align 8 + %6 = alloca i32, align 4 + store i32 0, i32* %3, align 4 + store i32 %0, i32* %4, align 4 + store i8** %1, i8*** %5, align 8 + %7 = load i8**, i8*** %5, align 8 + %8 = getelementptr inbounds i8*, i8** %7, i64 1 + %9 = load i8*, i8** %8, align 8 + %10 = call i64 @strlen(i8* noundef %9) #7 + %11 = sub i64 %10, 1 + %12 = trunc i64 %11 to i32 + store i32 %12, i32* %6, align 4 + br label %13 + +13: ; preds = %29, %2 + %14 = load i32, i32* %6, align 4 + %15 = icmp sgt i32 %14, 0 + br i1 %15, label %16, label %27 + +16: ; preds = %13 + %17 = load i8**, i8*** %5, align 8 + %18 = getelementptr inbounds i8*, i8** %17, i64 1 + %19 = load i8*, i8** %18, align 8 + %20 = load i32, i32* %6, align 4 + %21 = sub nsw i32 %20, 1 + %22 = sext i32 %21 to i64 + %23 = getelementptr inbounds i8, i8* %19, i64 %22 + %24 = load i8, i8* %23, align 1 + %25 = sext i8 %24 to i32 + %26 = icmp ne i32 %25, 47 + br label %27 + +27: ; preds = %16, %13 + %28 = phi i1 [ false, %13 ], [ %26, %16 ] + br i1 %28, label %29, label %32 + +29: ; preds = %27 + %30 = load i32, i32* %6, align 4 + %31 = add nsw i32 %30, -1 + store i32 %31, i32* %6, align 4 + br label %13, !llvm.loop !10 + +32: ; preds = %27 + %33 = load i8**, i8*** %5, align 8 + %34 = getelementptr inbounds i8*, i8** %33, i64 1 + %35 = load i8*, i8** %34, align 8 + %36 = load i32, i32* %6, align 4 + %37 = sext i32 %36 to i64 + %38 = getelementptr inbounds i8, i8* %35, i64 %37 + %39 = call i8* @strcpy(i8* noundef getelementptr inbounds ([100 x i8], [100 x i8]* @filename, i64 0, i64 0), i8* noundef %38) #6 + %40 = load i8**, i8*** %5, align 8 + %41 = getelementptr inbounds i8*, i8** %40, i64 1 + %42 = load i8*, i8** %41, align 8 + %43 = load %struct._IO_FILE*, %struct._IO_FILE** @stdin, align 8 + %44 = call %struct._IO_FILE* @freopen(i8* noundef %42, i8* noundef getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), %struct._IO_FILE* noundef %43) + %45 = call i32 @yyparse() + %46 = load i32, i32* @syntax_error, align 4 + %47 = icmp eq i32 %46, 0 + br i1 %47, label %48, label %50 + +48: ; preds = %32 + %49 = load %struct.ASTNode*, %struct.ASTNode** @root, align 8 + call void @display(%struct.ASTNode* noundef %49) + br label %50 + +50: ; preds = %48, %32 + ret i32 0 +} + +; Function Attrs: nounwind readonly willreturn +declare i64 @strlen(i8* noundef) #4 + +; Function Attrs: nounwind +declare i8* @strcpy(i8* noundef, i8* noundef) #2 + +declare %struct._IO_FILE* @freopen(i8* noundef, i8* noundef, %struct._IO_FILE* noundef) #3 + +declare void @display(%struct.ASTNode* noundef) #3 + +; Function Attrs: nofree nosync nounwind willreturn +declare void @llvm.va_start(i8*) #5 + +declare i32 @fprintf(%struct._IO_FILE* noundef, i8* noundef, ...) #3 + +declare i32 @vfprintf(%struct._IO_FILE* noundef, i8* noundef, %struct.__va_list_tag* noundef) #3 + +; Function Attrs: noinline nounwind optnone uwtable +define internal i32 @yy_syntax_error_arguments(%struct.yypcontext_t* noundef %0, i32* noundef %1, i32 noundef %2) #0 { + %4 = alloca i32, align 4 + %5 = alloca %struct.yypcontext_t*, align 8 + %6 = alloca i32*, align 8 + %7 = alloca i32, align 4 + %8 = alloca i32, align 4 + %9 = alloca i32, align 4 + store %struct.yypcontext_t* %0, %struct.yypcontext_t** %5, align 8 + store i32* %1, i32** %6, align 8 + store i32 %2, i32* %7, align 4 + store i32 0, i32* %8, align 4 + %10 = load %struct.yypcontext_t*, %struct.yypcontext_t** %5, align 8 + %11 = getelementptr inbounds %struct.yypcontext_t, %struct.yypcontext_t* %10, i32 0, i32 1 + %12 = load i32, i32* %11, align 8 + %13 = icmp ne i32 %12, -2 + br i1 %13, label %14, label %49 + +14: ; preds = %3 + %15 = load i32*, i32** %6, align 8 + %16 = icmp ne i32* %15, null + br i1 %16, label %17, label %25 + +17: ; preds = %14 + %18 = load %struct.yypcontext_t*, %struct.yypcontext_t** %5, align 8 + %19 = getelementptr inbounds %struct.yypcontext_t, %struct.yypcontext_t* %18, i32 0, i32 1 + %20 = load i32, i32* %19, align 8 + %21 = load i32*, i32** %6, align 8 + %22 = load i32, i32* %8, align 4 + %23 = sext i32 %22 to i64 + %24 = getelementptr inbounds i32, i32* %21, i64 %23 + store i32 %20, i32* %24, align 4 + br label %25 + +25: ; preds = %17, %14 + %26 = load i32, i32* %8, align 4 + %27 = add nsw i32 %26, 1 + store i32 %27, i32* %8, align 4 + %28 = load %struct.yypcontext_t*, %struct.yypcontext_t** %5, align 8 + %29 = load i32*, i32** %6, align 8 + %30 = icmp ne i32* %29, null + br i1 %30, label %31, label %34 + +31: ; preds = %25 + %32 = load i32*, i32** %6, align 8 + %33 = getelementptr inbounds i32, i32* %32, i64 1 + br label %36 + +34: ; preds = %25 + %35 = load i32*, i32** %6, align 8 + br label %36 + +36: ; preds = %34, %31 + %37 = phi i32* [ %33, %31 ], [ %35, %34 ] + %38 = load i32, i32* %7, align 4 + %39 = sub nsw i32 %38, 1 + %40 = call i32 @yypcontext_expected_tokens(%struct.yypcontext_t* noundef %28, i32* noundef %37, i32 noundef %39) + store i32 %40, i32* %9, align 4 + %41 = load i32, i32* %9, align 4 + %42 = icmp eq i32 %41, -2 + br i1 %42, label %43, label %44 + +43: ; preds = %36 + store i32 -2, i32* %4, align 4 + br label %51 + +44: ; preds = %36 + %45 = load i32, i32* %9, align 4 + %46 = load i32, i32* %8, align 4 + %47 = add nsw i32 %46, %45 + store i32 %47, i32* %8, align 4 + br label %48 + +48: ; preds = %44 + br label %49 + +49: ; preds = %48, %3 + %50 = load i32, i32* %8, align 4 + store i32 %50, i32* %4, align 4 + br label %51 + +51: ; preds = %49, %43 + %52 = load i32, i32* %4, align 4 + ret i32 %52 +} + +; Function Attrs: noinline nounwind optnone uwtable +define internal i64 @yytnamerr(i8* noundef %0, i8* noundef %1) #0 { + %3 = alloca i64, align 8 + %4 = alloca i8*, align 8 + %5 = alloca i8*, align 8 + %6 = alloca i64, align 8 + %7 = alloca i8*, align 8 + store i8* %0, i8** %4, align 8 + store i8* %1, i8** %5, align 8 + %8 = load i8*, i8** %5, align 8 + %9 = load i8, i8* %8, align 1 + %10 = sext i8 %9 to i32 + %11 = icmp eq i32 %10, 34 + br i1 %11, label %12, label %52 + +12: ; preds = %2 + store i64 0, i64* %6, align 8 + %13 = load i8*, i8** %5, align 8 + store i8* %13, i8** %7, align 8 + br label %14 + +14: ; preds = %50, %12 + %15 = load i8*, i8** %7, align 8 + %16 = getelementptr inbounds i8, i8* %15, i32 1 + store i8* %16, i8** %7, align 8 + %17 = load i8, i8* %16, align 1 + %18 = sext i8 %17 to i32 + switch i32 %18, label %29 [ + i32 39, label %19 + i32 44, label %19 + i32 92, label %20 + i32 34, label %41 + ] + +19: ; preds = %14, %14 + br label %51 + +20: ; preds = %14 + %21 = load i8*, i8** %7, align 8 + %22 = getelementptr inbounds i8, i8* %21, i32 1 + store i8* %22, i8** %7, align 8 + %23 = load i8, i8* %22, align 1 + %24 = sext i8 %23 to i32 + %25 = icmp ne i32 %24, 92 + br i1 %25, label %26, label %27 + +26: ; preds = %20 + br label %51 + +27: ; preds = %20 + br label %28 + +28: ; preds = %27 + br label %29 + +29: ; preds = %14, %28 + %30 = load i8*, i8** %4, align 8 + %31 = icmp ne i8* %30, null + br i1 %31, label %32, label %38 + +32: ; preds = %29 + %33 = load i8*, i8** %7, align 8 + %34 = load i8, i8* %33, align 1 + %35 = load i8*, i8** %4, align 8 + %36 = load i64, i64* %6, align 8 + %37 = getelementptr inbounds i8, i8* %35, i64 %36 + store i8 %34, i8* %37, align 1 + br label %38 + +38: ; preds = %32, %29 + %39 = load i64, i64* %6, align 8 + %40 = add nsw i64 %39, 1 + store i64 %40, i64* %6, align 8 + br label %50 + +41: ; preds = %14 + %42 = load i8*, i8** %4, align 8 + %43 = icmp ne i8* %42, null + br i1 %43, label %44, label %48 + +44: ; preds = %41 + %45 = load i8*, i8** %4, align 8 + %46 = load i64, i64* %6, align 8 + %47 = getelementptr inbounds i8, i8* %45, i64 %46 + store i8 0, i8* %47, align 1 + br label %48 + +48: ; preds = %44, %41 + %49 = load i64, i64* %6, align 8 + store i64 %49, i64* %3, align 8 + br label %66 + +50: ; preds = %38 + br label %14 + +51: ; preds = %26, %19 + br label %52 + +52: ; preds = %51, %2 + %53 = load i8*, i8** %4, align 8 + %54 = icmp ne i8* %53, null + br i1 %54, label %55, label %63 + +55: ; preds = %52 + %56 = load i8*, i8** %4, align 8 + %57 = load i8*, i8** %5, align 8 + %58 = call i8* @yystpcpy(i8* noundef %56, i8* noundef %57) + %59 = load i8*, i8** %4, align 8 + %60 = ptrtoint i8* %58 to i64 + %61 = ptrtoint i8* %59 to i64 + %62 = sub i64 %60, %61 + store i64 %62, i64* %3, align 8 + br label %66 + +63: ; preds = %52 + %64 = load i8*, i8** %5, align 8 + %65 = call i64 @strlen(i8* noundef %64) #7 + store i64 %65, i64* %3, align 8 + br label %66 + +66: ; preds = %63, %55, %48 + %67 = load i64, i64* %3, align 8 + ret i64 %67 +} + +; Function Attrs: noinline nounwind optnone uwtable +define internal i32 @yypcontext_expected_tokens(%struct.yypcontext_t* noundef %0, i32* noundef %1, i32 noundef %2) #0 { + %4 = alloca i32, align 4 + %5 = alloca %struct.yypcontext_t*, align 8 + %6 = alloca i32*, align 8 + %7 = alloca i32, align 4 + %8 = alloca i32, align 4 + %9 = alloca i32, align 4 + %10 = alloca i32, align 4 + %11 = alloca i32, align 4 + %12 = alloca i32, align 4 + %13 = alloca i32, align 4 + store %struct.yypcontext_t* %0, %struct.yypcontext_t** %5, align 8 + store i32* %1, i32** %6, align 8 + store i32 %2, i32* %7, align 4 + store i32 0, i32* %8, align 4 + %14 = load %struct.yypcontext_t*, %struct.yypcontext_t** %5, align 8 + %15 = getelementptr inbounds %struct.yypcontext_t, %struct.yypcontext_t* %14, i32 0, i32 0 + %16 = load i8*, i8** %15, align 8 + %17 = load i8, i8* %16, align 1 + %18 = zext i8 %17 to i32 + %19 = sext i32 %18 to i64 + %20 = getelementptr inbounds [207 x i16], [207 x i16]* @yypact, i64 0, i64 %19 + %21 = load i16, i16* %20, align 2 + %22 = sext i16 %21 to i32 + store i32 %22, i32* %9, align 4 + %23 = load i32, i32* %9, align 4 + %24 = icmp eq i32 %23, -167 + br i1 %24, label %87, label %25 + +25: ; preds = %3 + %26 = load i32, i32* %9, align 4 + %27 = icmp slt i32 %26, 0 + br i1 %27, label %28, label %31 + +28: ; preds = %25 + %29 = load i32, i32* %9, align 4 + %30 = sub nsw i32 0, %29 + br label %32 + +31: ; preds = %25 + br label %32 + +32: ; preds = %31, %28 + %33 = phi i32 [ %30, %28 ], [ 0, %31 ] + store i32 %33, i32* %10, align 4 + %34 = load i32, i32* %9, align 4 + %35 = sub nsw i32 301, %34 + %36 = add nsw i32 %35, 1 + store i32 %36, i32* %11, align 4 + %37 = load i32, i32* %11, align 4 + %38 = icmp slt i32 %37, 41 + br i1 %38, label %39, label %41 + +39: ; preds = %32 + %40 = load i32, i32* %11, align 4 + br label %42 + +41: ; preds = %32 + br label %42 + +42: ; preds = %41, %39 + %43 = phi i32 [ %40, %39 ], [ 41, %41 ] + store i32 %43, i32* %12, align 4 + %44 = load i32, i32* %10, align 4 + store i32 %44, i32* %13, align 4 + br label %45 + +45: ; preds = %83, %42 + %46 = load i32, i32* %13, align 4 + %47 = load i32, i32* %12, align 4 + %48 = icmp slt i32 %46, %47 + br i1 %48, label %49, label %86 + +49: ; preds = %45 + %50 = load i32, i32* %13, align 4 + %51 = load i32, i32* %9, align 4 + %52 = add nsw i32 %50, %51 + %53 = sext i32 %52 to i64 + %54 = getelementptr inbounds [302 x i16], [302 x i16]* @yycheck, i64 0, i64 %53 + %55 = load i16, i16* %54, align 2 + %56 = sext i16 %55 to i32 + %57 = load i32, i32* %13, align 4 + %58 = icmp eq i32 %56, %57 + br i1 %58, label %59, label %82 + +59: ; preds = %49 + %60 = load i32, i32* %13, align 4 + %61 = icmp ne i32 %60, 1 + br i1 %61, label %62, label %82 + +62: ; preds = %59 + %63 = load i32*, i32** %6, align 8 + %64 = icmp ne i32* %63, null + br i1 %64, label %68, label %65 + +65: ; preds = %62 + %66 = load i32, i32* %8, align 4 + %67 = add nsw i32 %66, 1 + store i32 %67, i32* %8, align 4 + br label %81 + +68: ; preds = %62 + %69 = load i32, i32* %8, align 4 + %70 = load i32, i32* %7, align 4 + %71 = icmp eq i32 %69, %70 + br i1 %71, label %72, label %73 + +72: ; preds = %68 + store i32 0, i32* %4, align 4 + br label %101 + +73: ; preds = %68 + %74 = load i32, i32* %13, align 4 + %75 = load i32*, i32** %6, align 8 + %76 = load i32, i32* %8, align 4 + %77 = add nsw i32 %76, 1 + store i32 %77, i32* %8, align 4 + %78 = sext i32 %76 to i64 + %79 = getelementptr inbounds i32, i32* %75, i64 %78 + store i32 %74, i32* %79, align 4 + br label %80 + +80: ; preds = %73 + br label %81 + +81: ; preds = %80, %65 + br label %82 + +82: ; preds = %81, %59, %49 + br label %83 + +83: ; preds = %82 + %84 = load i32, i32* %13, align 4 + %85 = add nsw i32 %84, 1 + store i32 %85, i32* %13, align 4 + br label %45, !llvm.loop !11 + +86: ; preds = %45 + br label %87 + +87: ; preds = %86, %3 + %88 = load i32*, i32** %6, align 8 + %89 = icmp ne i32* %88, null + br i1 %89, label %90, label %99 + +90: ; preds = %87 + %91 = load i32, i32* %8, align 4 + %92 = icmp eq i32 %91, 0 + br i1 %92, label %93, label %99 + +93: ; preds = %90 + %94 = load i32, i32* %7, align 4 + %95 = icmp slt i32 0, %94 + br i1 %95, label %96, label %99 + +96: ; preds = %93 + %97 = load i32*, i32** %6, align 8 + %98 = getelementptr inbounds i32, i32* %97, i64 0 + store i32 -2, i32* %98, align 4 + br label %99 + +99: ; preds = %96, %93, %90, %87 + %100 = load i32, i32* %8, align 4 + store i32 %100, i32* %4, align 4 + br label %101 + +101: ; preds = %99, %72 + %102 = load i32, i32* %4, align 4 + ret i32 %102 +} + +; Function Attrs: noinline nounwind optnone uwtable +define internal i8* @yystpcpy(i8* noundef %0, i8* noundef %1) #0 { + %3 = alloca i8*, align 8 + %4 = alloca i8*, align 8 + %5 = alloca i8*, align 8 + %6 = alloca i8*, align 8 + store i8* %0, i8** %3, align 8 + store i8* %1, i8** %4, align 8 + %7 = load i8*, i8** %3, align 8 + store i8* %7, i8** %5, align 8 + %8 = load i8*, i8** %4, align 8 + store i8* %8, i8** %6, align 8 + br label %9 + +9: ; preds = %17, %2 + %10 = load i8*, i8** %6, align 8 + %11 = getelementptr inbounds i8, i8* %10, i32 1 + store i8* %11, i8** %6, align 8 + %12 = load i8, i8* %10, align 1 + %13 = load i8*, i8** %5, align 8 + %14 = getelementptr inbounds i8, i8* %13, i32 1 + store i8* %14, i8** %5, align 8 + store i8 %12, i8* %13, align 1 + %15 = sext i8 %12 to i32 + %16 = icmp ne i32 %15, 0 + br i1 %16, label %17, label %18 + +17: ; preds = %9 + br label %9, !llvm.loop !12 + +18: ; preds = %9 + %19 = load i8*, i8** %5, align 8 + %20 = getelementptr inbounds i8, i8* %19, i64 -1 + ret i8* %20 +} + +attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #1 = { argmemonly nofree nounwind willreturn } +attributes #2 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #3 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #4 = { nounwind readonly willreturn "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #5 = { nofree nosync nounwind willreturn } +attributes #6 = { nounwind } +attributes #7 = { nounwind readonly willreturn } + +!llvm.module.flags = !{!0, !1, !2, !3, !4} +!llvm.ident = !{!5} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{i32 7, !"PIC Level", i32 2} +!2 = !{i32 7, !"PIE Level", i32 2} +!3 = !{i32 7, !"uwtable", i32 1} +!4 = !{i32 7, !"frame-pointer", i32 2} +!5 = !{!"Ubuntu clang version 14.0.0-1ubuntu1.1"} +!6 = distinct !{!6, !7} +!7 = !{!"llvm.loop.mustprogress"} +!8 = distinct !{!8, !7} +!9 = distinct !{!9, !7} +!10 = distinct !{!10, !7} +!11 = distinct !{!11, !7} +!12 = distinct !{!12, !7} diff --git a/compiler/src/parser/parser.o b/compiler/src/parser/parser.o new file mode 100644 index 0000000..a5b207e Binary files /dev/null and b/compiler/src/parser/parser.o differ diff --git a/compiler/src/parser/parser.y b/compiler/src/parser/parser.y new file mode 100644 index 0000000..20ff22a --- /dev/null +++ b/compiler/src/parser/parser.y @@ -0,0 +1,196 @@ +%define parse.error verbose +%locations +%{ +#include +#include +#include +#include + +#include "ast.h" + +ASTNode *root; + +extern FILE *yyin; +extern int line_cnt; +extern int yylineno; +extern char *yytext; +extern int yylex(); +extern int yyparse(); +//extern void yyerror(char *msg); +void yyerror(const char* fmt, ...); +int syntax_error = 0; +char filename[100]; +%} + +%union { + int int_val; + float float_val; + char *str_val; + struct ASTNode *node_val; +} + +%type CompUnit ConstDecl VarDecl FuncDef ConstDef ConstInitVal VarDef InitVal FuncFParam ConstExpArray Block +%type Root BlockItem Stmt LVal PrimaryExp UnaryExp FuncRParams MulExp Exp RelExp EqExp LAndExp LNotExp Cond ConstExp +%type ExpArray AddExp LOrExp InitVals +//ForList + +%token ID +%token INT_LIT +%token FLOAT_LIT + +%token INT FLOAT VOID CONST RETURN IF ELSE WHILE BREAK CONTINUE LP RP LB RB LC RC COMMA SEMICOLON +%token MINUS NOT ASSIGN PLUS MUL DIV MOD AND OR EQ NE LT LE GT GE LEX_ERR +//FOR INC DEC THEN + +%nonassoc THEN +%nonassoc ELSE + +%start Root + +%% +Root: CompUnit { root = new_node(Root, NULL, NULL, $1, 0, 0, NULL, NonType); }; +CompUnit: ConstDecl { $$ = new_node(CompUnit, NULL, NULL, $1, 0, 0, NULL, NonType); } + | VarDecl { $$ = new_node(CompUnit, NULL, NULL, $1, 0, 0, NULL, NonType); } + | FuncDef { $$ = new_node(CompUnit, NULL, NULL, $1, 0, 0, NULL, NonType); } + | ConstDecl CompUnit { $$ = new_node(CompUnit, $2, NULL, $1, 0, 0, NULL, NonType); } + | VarDecl CompUnit { $$ = new_node(CompUnit, $2, NULL, $1, 0, 0, NULL, NonType); } + | FuncDef CompUnit { $$ = new_node(CompUnit, $2, NULL, $1, 0, 0, NULL, NonType); }; + +ConstDecl: CONST INT ConstDef SEMICOLON { $$ = new_node(ConstDecl, NULL, NULL, $3, 0, 0, NULL, Int); } + | CONST FLOAT ConstDef SEMICOLON { $$ = new_node(ConstDecl, NULL, NULL, $3, 0, 0, NULL, Float); }; +ConstDef: ID ConstExpArray ASSIGN ConstInitVal { $$ = new_node(ConstDef, NULL, $2, $4, 0, 0, $1, NonType); } + | ID ConstExpArray ASSIGN ConstInitVal COMMA ConstDef { $$ = new_node(ConstDef, $6, $2, $4, 0, 0, $1, NonType); }; +ConstExpArray: { $$ = NULL; } + | LB ConstExp RB ConstExpArray { $$ = new_node(ConstExpArray, $4, NULL, $2, 0, 0, NULL, NonType); }; +ConstInitVal: ConstExp { $$ = new_node(ConstInitVal, NULL, NULL, $1, 0, 0, NULL, NonType); } + | LC RC { $$ = new_node(ConstInitVal, NULL, NULL, NULL, 0, 0, NULL, NonType); } + | LC ConstInitVal RC { $$ = new_node(ConstInitVal, NULL, NULL, $2, 0, 0, NULL, NonType); } + | LC ConstInitVal COMMA ConstInitVal RC { $$ = new_node(ConstInitVal, $4, NULL, $2, 0, 0, NULL, NonType); }; +ConstExp: MulExp { $$ = new_node(ConstExp, NULL, NULL, $1, 0, 0, NULL, NonType); } + | MulExp PLUS Exp { $$ = new_node(ConstExp, $3, NULL, $1, PLUS, 0, NULL, NonType); } + | MulExp MINUS Exp { $$ = new_node(ConstExp, $3, NULL, $1, MINUS, 0, NULL, NonType); }; + +VarDecl: INT VarDef SEMICOLON { $$ = new_node(VarDecl, NULL, NULL, $2, 0, 0, NULL, Int); } + | FLOAT VarDef SEMICOLON { $$ = new_node(VarDecl, NULL, NULL, $2, 0, 0, NULL, Float); }; +VarDef: ID ConstExpArray { $$ = new_node(VarDef, NULL, $2, NULL, 0, 0, $1, NonType); } + | ID ConstExpArray ASSIGN InitVal { $$ = new_node(VarDef, NULL, $2, $4, 0, 0, $1, NonType); } + | ID ConstExpArray COMMA VarDef { $$ = new_node(VarDef, $4, $2, NULL, 0, 0, $1, NonType); } + | ID ConstExpArray ASSIGN InitVal COMMA VarDef { $$ = new_node(VarDef, $6, $2, $4, 0, 0, $1, NonType); }; +InitVal: Exp { $$ = new_node(InitVal, NULL, NULL, $1, Exp, 0, NULL, NonType); } + | LC RC { $$ = new_node(InitVal, NULL, NULL, NULL, InitVals, 0, NULL, NonType); } + | LC InitVals RC { $$ = new_node(InitVal, NULL, NULL, $2, InitVals, 0, NULL, NonType); }; +InitVals: InitVal { $$ = new_node(InitVals, NULL, NULL, $1, 0, 0, NULL, NonType); } + | InitVal COMMA InitVals { $$ = new_node(InitVals, $3, NULL, $1, 0, 0, NULL, NonType); }; + +FuncDef: INT ID LP RP Block { $$ = new_node(FuncDef, NULL, NULL, $5, 0, 0, $2, Int); } + | FLOAT ID LP RP Block { $$ = new_node(FuncDef, NULL, NULL, $5, 0, 0, $2, Float); } + | VOID ID LP RP Block { $$ = new_node(FuncDef, NULL, NULL, $5, 0, 0, $2, Void); } + | INT ID LP FuncFParam RP Block { $$ = new_node(FuncDef, NULL, $4, $6, 0, 0, $2, Int); } + | FLOAT ID LP FuncFParam RP Block { $$ = new_node(FuncDef, NULL, $4, $6, 0, 0, $2, Float); } + | VOID ID LP FuncFParam RP Block { $$ = new_node(FuncDef, NULL, $4, $6, 0, 0, $2, Void); };; +FuncFParam: INT ID { $$ = new_node(FuncFParam, NULL, NULL, NULL, 0, 0, $2, Int); } + | FLOAT ID { $$ = new_node(FuncFParam, NULL, NULL, NULL, 0, 0, $2, Float); } + | INT ID LB RB ExpArray { $$ = new_node(FuncFParam, NULL, NULL, $5, 0, 0, $2, Int); } + | FLOAT ID LB RB ExpArray { $$ = new_node(FuncFParam, NULL, NULL, $5, 0, 0, $2, Float); } + | INT ID COMMA FuncFParam { $$ = new_node(FuncFParam, $4, NULL, NULL, 0, 0, $2, Int); } + | FLOAT ID COMMA FuncFParam { $$ = new_node(FuncFParam, $4, NULL, NULL, 0, 0, $2, Float); } + | INT ID LB RB ExpArray COMMA FuncFParam { $$ = new_node(FuncFParam, $7, NULL, $5, 0, 0, $2, Int); } + | FLOAT ID LB RB ExpArray COMMA FuncFParam { $$ = new_node(FuncFParam, $7, NULL, $5, 0, 0, $2, Float); }; + +Block: LC BlockItem RC { $$ = new_node(Block, NULL, NULL, $2, 0, 0, NULL, NonType); }; +BlockItem: { $$ = NULL; } + | ConstDecl BlockItem { $$ = new_node(BlockItem, $2, NULL, $1, 0, 0, NULL, NonType); } + | VarDecl BlockItem { $$ = new_node(BlockItem, $2, NULL, $1, 0, 0, NULL, NonType); } + | Stmt BlockItem { $$ = new_node(BlockItem, $2, NULL, $1, 0, 0, NULL, NonType); }; + + +Stmt: LVal ASSIGN Exp SEMICOLON{$$ = new_node(Stmt,$1,NULL,$3,0,0,NULL,NonType);} +| Exp SEMICOLON {$$ = new_node(Stmt,NULL,NULL,$1,0,0,NULL,NonType);} +| SEMICOLON {$$ = NULL;} +| Block{$$ = new_node(Stmt,NULL,NULL,$1,0,0,NULL,NonType);} +| IF LP Cond RP Stmt {$$ = new_node(Stmt,$3,NULL,$5,0,0,NULL,NonType);} +| IF LP Cond RP Stmt ELSE Stmt {$$ = new_node(Stmt,$3,$7,$5,0,0,NULL,NonType);} +| WHILE LP Cond RP Stmt {$$ = new_node(Stmt,$3,NULL,$5,0,0,NULL,NonType);} +| BREAK SEMICOLON {$$ = NULL;} +| CONTINUE SEMICOLON {$$ = NULL;} +| RETURN SEMICOLON {$$ = NULL;} +| RETURN Exp SEMICOLON {$$ = new_node(Stmt,NULL,NULL,$2,0,0,NULL,NonType);} +; + + + +Exp: AddExp { $$ = new_node(Exp, NULL, NULL, $1, 0, 0, NULL, NonType); }; +AddExp: MulExp { $$ = new_node(AddExp, NULL, NULL, $1, MUL, 0, NULL, NonType); } + | MulExp PLUS AddExp { $$ = new_node(AddExp, $3, NULL, $1, PLUS, 0, NULL, NonType); } + | MulExp MINUS AddExp { $$ = new_node(AddExp, $3, NULL, $1, MINUS, 0, NULL, NonType); }; +MulExp: UnaryExp { $$ = new_node(MulExp, NULL, NULL, $1, UnaryExp, 0, NULL, NonType); } + | UnaryExp MUL MulExp { $$ = new_node(MulExp, $3, NULL, $1, MUL, 0, NULL, NonType); } + | UnaryExp DIV MulExp { $$ = new_node(MulExp, $3, NULL, $1, DIV, 0, NULL, NonType); } + | UnaryExp MOD MulExp { $$ = new_node(MulExp, $3, NULL, $1, MOD, 0, NULL, NonType); }; +UnaryExp: PrimaryExp { $$ = new_node(UnaryExp, NULL, NULL, $1, PrimaryExp, 0, NULL, NonType); } + | ID LP RP { $$ = new_node(UnaryExp, NULL, NULL, NULL, FuncRParams, 0, $1, NonType); } + | ID LP FuncRParams RP { $$ = new_node(UnaryExp, NULL, NULL, $3, FuncRParams, 0, $1, NonType); } + | PLUS UnaryExp { $$ = new_node(UnaryExp, NULL, NULL, $2, Plus, 0, NULL, NonType); } + | MINUS UnaryExp { $$ = new_node(UnaryExp, NULL, NULL, $2, Minus, 0, NULL, NonType); } + | NOT UnaryExp { $$ = new_node(UnaryExp, NULL, NULL, $2, NOT, 0, NULL, NonType); }; +FuncRParams: Exp { $$ = new_node(FuncRParams, NULL, NULL, $1, 0, 0, NULL, NonType); } + | Exp COMMA FuncRParams { $$ = new_node(FuncRParams, $3, NULL, $1, 0, 0, NULL, NonType); }; +PrimaryExp: LP Exp RP { $$ = new_node(PrimaryExp, NULL, NULL, $2, Exp, 0, NULL, NonType); } + | LVal { $$ = new_node(PrimaryExp, NULL, NULL, $1, LVal, 0, NULL, NonType); } + | INT_LIT { $$ = new_node(PrimaryExp, NULL, NULL, NULL, $1, 0, NULL, Int); } + | FLOAT_LIT { $$ = new_node(PrimaryExp, NULL, NULL, NULL, 0, $1, NULL, Float); }; +LVal: ID ExpArray { $$ = new_node(LVal, NULL, NULL, $2, 0, 0, $1, NonType); }; + +Cond: LOrExp { $$ = new_node(Cond, NULL, NULL, $1, 0, 0, NULL, NonType); }; +LOrExp: LAndExp { $$ = new_node(Cond, NULL, NULL, $1, 0, 0, NULL, NonType); } + | LAndExp OR LOrExp { $$ = new_node(Cond, $3, NULL, $1, OR, 0, 0, NonType); } + | LNotExp{ $$ = new_node(Cond, NULL, NULL, $1, 0, 0, NULL, NonType); }; + +LAndExp: EqExp { $$ = new_node(LAndExp, NULL, NULL, $1, 0, 0, NULL, NonType); } + | EqExp AND LAndExp { $$ = new_node(LAndExp, $3, NULL, $1, AND, 0, NULL, NonType); }; + +LNotExp: NOT LP EqExp RP { $$ = new_node(LNotExp, NULL, NULL, $3, 0, 0, NULL, NonType);}; + +EqExp: RelExp { $$ = new_node(EqExp, NULL, NULL, $1, 0, 0, NULL, NonType);} + | RelExp EQ EqExp { $$ = new_node(EqExp, $3, NULL, $1, EQ, 0, NULL, NonType); } + | RelExp NE EqExp { $$ = new_node(EqExp, $3, NULL, $1, NE, 0, NULL, NonType); }; + +RelExp: AddExp { $$ = new_node(RelExp, NULL, NULL, $1, 0, 0, NULL, NonType); } + | AddExp LT RelExp { $$ = new_node(RelExp, $3, NULL, $1, LT, 0, NULL, NonType); } + | AddExp GT RelExp { $$ = new_node(RelExp, $3, NULL, $1, GT, 0, NULL, NonType);} + | AddExp LE RelExp { $$ = new_node(RelExp, $3, NULL, $1, LE, 0, NULL, NonType); } + | AddExp GE RelExp { $$ = new_node(RelExp, $3, NULL, $1, GE, 0, NULL, NonType); }; + +ExpArray: { $$ = NULL; } + | LB Exp RB ExpArray { $$ = new_node(ExpArray, $4, NULL, $2, 0, 0, NULL, NonType); }; +%% + +int main(int argc, char *argv[]) { + int index = strlen(argv[1]) - 1; + while(index > 0 && argv[1][index - 1] != '/') + index--; + strcpy(filename, argv[1] + index); + freopen(argv[1], "r", stdin); + yyparse(); + if (syntax_error == 0) + display(root); + return 0; +} + +/* +void yyerror(char *msg) { + printf("%s:%d\n", name, yylineno); + printf("error text: %s\n", yytext); + exit(-1); +} +*/ +#include +void yyerror(const char* fmt, ...) +{ + syntax_error = 1; + va_list ap; + va_start(ap, fmt); + fprintf(stderr, "%s:%d ", filename, yylineno); + vfprintf(stderr, fmt, ap); + fprintf(stderr, ".\n"); +} diff --git a/compiler/src/riscv/.backend.cpp.swp b/compiler/src/riscv/.backend.cpp.swp new file mode 100644 index 0000000..df32ca3 Binary files /dev/null and b/compiler/src/riscv/.backend.cpp.swp differ diff --git a/compiler/src/riscv/CMakeLists.txt b/compiler/src/riscv/CMakeLists.txt new file mode 100644 index 0000000..b622912 --- /dev/null +++ b/compiler/src/riscv/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.21) + +set(SOURCE_FILES "riscv.cpp" instruction.cpp optimize.cpp backend.cpp regalloc.cpp) + +add_library(riscv STATIC ${SOURCE_FILES}) + +target_link_libraries(riscv PRIVATE ir) +target_include_directories(riscv PRIVATE ${CMAKE_SOURCE_DIR}/src/ir) \ No newline at end of file diff --git a/compiler/src/riscv/backend.cpp b/compiler/src/riscv/backend.cpp new file mode 100644 index 0000000..5b59e3f --- /dev/null +++ b/compiler/src/riscv/backend.cpp @@ -0,0 +1,970 @@ +#include "backend.h" +#include + +void RiscvBuilder::initializeRegisterFile() { + // 分配寄存器堆,初始化寄存器堆各项参数 + // assert(false); +} + +// 进行标号 +// 未知指令:FNeg +// 注意:IR中因为没有addi和add和浮点的区别,该步操作由build操作进行修正 +const std::map toRiscvOp = { + {Instruction::OpID::Add, RiscvInstr::InstrType::ADD}, + {Instruction::OpID::Sub, RiscvInstr::InstrType::SUB}, + {Instruction::OpID::Mul, RiscvInstr::InstrType::MUL}, + {Instruction::OpID::SDiv, RiscvInstr::InstrType::DIV}, + {Instruction::OpID::SRem, RiscvInstr::InstrType::REM}, + {Instruction::OpID::FAdd, RiscvInstr::InstrType::FADD}, + {Instruction::OpID::FSub, RiscvInstr::InstrType::FSUB}, + {Instruction::OpID::FMul, RiscvInstr::InstrType::FMUL}, + {Instruction::OpID::FDiv, RiscvInstr::InstrType::FDIV}, + {Instruction::OpID::Ret, RiscvInstr::InstrType::RET}, + {Instruction::OpID::ICmp, RiscvInstr::InstrType::ICMP}, + {Instruction::OpID::FCmp, RiscvInstr::InstrType::FCMP}, + {Instruction::OpID::Call, RiscvInstr::InstrType::CALL}, + {Instruction::OpID::SItoFP, RiscvInstr::InstrType::SITOFP}, + {Instruction::OpID::FPtoSI, RiscvInstr::InstrType::FPTOSI}, + {Instruction::OpID::Or, RiscvInstr::InstrType::OR}, + {Instruction::OpID::And, RiscvInstr::InstrType::AND}, + {Instruction::OpID::Shl, RiscvInstr::InstrType::SHL}, + {Instruction::OpID::LShr, RiscvInstr::InstrType::LSHR}, + {Instruction::OpID::AShr, RiscvInstr::InstrType::ASHR}, + {Instruction::OpID::Load, RiscvInstr::InstrType::LW}, + {Instruction::OpID::Store, RiscvInstr::InstrType::SW}, +}; + +int LabelCount = 0; +std::map rbbLabel; +std::map functionLabel; +std::string toLabel(int ind) { return ".L" + std::to_string(ind); } + +RiscvBasicBlock *createRiscvBasicBlock(BasicBlock *bb) { + if (bb == nullptr) { + LabelCount++; + return new RiscvBasicBlock(toLabel(LabelCount), LabelCount); + } + if (rbbLabel.count(bb)) + return rbbLabel[bb]; + LabelCount++; + auto cur = new RiscvBasicBlock(toLabel(LabelCount), LabelCount); + return rbbLabel[bb] = cur; +} + +RiscvFunction *createRiscvFunction(Function *foo) { + assert(foo != nullptr); + if (functionLabel.count(foo) == 0) { + auto ty = RiscvOperand::Void; + switch (foo->type_->tid_) { + case Type::VoidTyID: + ty = RiscvOperand::Void; + break; + case Type::IntegerTyID: + ty = RiscvOperand::IntReg; + break; + case Type::FloatTyID: + ty = RiscvOperand::FloatReg; + break; + } + RiscvFunction *cur = + new RiscvFunction(foo->name_, foo->arguments_.size(), ty); + return functionLabel[foo] = cur; + } + return functionLabel[foo]; +} + +BinaryRiscvInst *RiscvBuilder::createBinaryInstr(RegAlloca *regAlloca, + BinaryInst *binaryInstr, + RiscvBasicBlock *rbb) { + auto id = toRiscvOp.at(binaryInstr->op_id_); + // 立即数处理 + + // If both operands are imm value, caculate the result directly and save to + // binaryInstr value. + if (binaryInstr->operands_[0]->is_constant() && + binaryInstr->operands_[1]->is_constant() && + dynamic_cast(binaryInstr->operands_[0]) != nullptr) { + int value[] = { + static_cast(binaryInstr->operands_[0])->value_, + static_cast(binaryInstr->operands_[1])->value_}; + int value_result; + switch (binaryInstr->op_id_) { + case Instruction::OpID::Add: + value_result = value[0] + value[1]; + break; + case Instruction::OpID::Sub: + value_result = value[0] - value[1]; + break; + case Instruction::OpID::Mul: + value_result = value[0] * value[1]; + break; + case Instruction::OpID::SDiv: + value_result = value[0] / value[1]; + break; + case Instruction::OpID::SRem: + value_result = value[0] % value[1]; + break; + default: + std::cerr << "[Fatal Error] Binary instruction immediate caculation not " + "implemented." + << std::endl; + std::terminate(); + } + rbb->addInstrBack( + new MoveRiscvInst(regAlloca->findReg(binaryInstr, rbb, nullptr, 0, 0), + value_result, rbb)); + return nullptr; + } + BinaryRiscvInst *instr = new BinaryRiscvInst( + id, regAlloca->findReg(binaryInstr->operands_[0], rbb, nullptr, 1), + regAlloca->findReg(binaryInstr->operands_[1], rbb, nullptr, 1), + regAlloca->findReg(binaryInstr, rbb, nullptr, 1, 0), rbb, true); + return instr; +} + +UnaryRiscvInst *RiscvBuilder::createUnaryInstr(RegAlloca *regAlloca, + UnaryInst *unaryInstr, + RiscvBasicBlock *rbb) { + UnaryRiscvInst *instr = new UnaryRiscvInst( + toRiscvOp.at(unaryInstr->op_id_), + regAlloca->findReg(unaryInstr->operands_[0], rbb, nullptr, 1), + regAlloca->findReg(unaryInstr, rbb, nullptr, 1, 0), rbb); + return instr; +} + +// IR中的Store对应到RISCV为MOV指令或浮点MOV指令或LI指令或真正的store指令 +std::vector RiscvBuilder::createStoreInstr(RegAlloca *regAlloca, + StoreInst *storeInstr, + RiscvBasicBlock *rbb) { + auto testConstInt = dynamic_cast(storeInstr->operands_[0]); + if (testConstInt != nullptr) { + // 整数部分可以直接li指令 + std::vector ans; + auto regPos = getRegOperand("t0"); + ans.push_back( + new MoveRiscvInst(regPos, new RiscvConst(testConstInt->value_), rbb)); + // 指针类型找ptr + if (storeInstr->operands_[1]->type_->tid_ == Type::TypeID::PointerTyID) + ans.push_back(new StoreRiscvInst( + storeInstr->operands_[0]->type_, regPos, + regAlloca->findMem(storeInstr->operands_[1], rbb, nullptr, 0), rbb)); + else + ans.push_back(new StoreRiscvInst( + storeInstr->operands_[0]->type_, regPos, + regAlloca->findMem(storeInstr->operands_[1], rbb, nullptr, 0), rbb)); + return ans; + } + // 真正的store:第二操作数为一个指针类型 + if (storeInstr->operands_[1]->type_->tid_ == Type::TypeID::PointerTyID) { + auto curType = static_cast(storeInstr->operands_[1]->type_); + + //Alignment check. + if (calcTypeSize(curType->contained_) > 4) { + auto mem = + regAlloca->findMem(storeInstr->operands_[1], rbb, nullptr, false); + if (static_cast(mem)->shift_ & 7) { + std::cerr << "[Error] Alignment error." << std::endl; + std::terminate(); + } + } + + StoreRiscvInst *instr = new StoreRiscvInst( + curType->contained_, + regAlloca->findReg(storeInstr->operands_[0], rbb, nullptr, 1), + regAlloca->findMem(storeInstr->operands_[1], rbb, nullptr, 0), rbb); + return {instr}; + } + // 下面为整型或浮点的mov + // 浮点部分需要提前写入内存中,然后等效于直接mov + // 先把浮点常数以全局变量形式存入内存中,再直接fmv + std::vector ans; + auto regPos = regAlloca->findReg(storeInstr->operands_[0], rbb, nullptr, 1); + ans.push_back(new MoveRiscvInst( + regAlloca->findReg(storeInstr->operands_[1], rbb), regPos, rbb)); + ans.push_back(new StoreRiscvInst(storeInstr->operands_[0]->type_, regPos, + regAlloca->findMem(storeInstr->operands_[0]), + rbb)); + return ans; +} + +// Load 指令仅一个参数!它本身就是一个value +std::vector RiscvBuilder::createLoadInstr(RegAlloca *regAlloca, + LoadInst *loadInstr, + RiscvBasicBlock *rbb) { + assert(loadInstr->operands_[0]->type_->tid_ == Type::TypeID::PointerTyID); + auto curType = static_cast(loadInstr->operands_[0]->type_); + if (calcTypeSize(curType->contained_) > 4) { + auto mem = regAlloca->findMem(loadInstr->operands_[0], rbb, nullptr, false); + if (static_cast(mem)->shift_ & 7) { + std::cerr << "[Error] Alignment error." << std::endl; + std::terminate(); + } + } + std::vector ans; + auto regPos = + regAlloca->findReg(static_cast(loadInstr), rbb, nullptr, 1, 0); + ans.push_back(new LoadRiscvInst( + curType->contained_, regPos, + regAlloca->findMem(loadInstr->operands_[0], rbb, nullptr, false), rbb)); + return ans; +} + +ICmpRiscvInstr *RiscvBuilder::createICMPInstr(RegAlloca *regAlloca, + ICmpInst *icmpInstr, + BranchInst *brInstr, + RiscvBasicBlock *rbb) { + ICmpRiscvInstr *instr = new ICmpRiscvInstr( + icmpInstr->icmp_op_, + regAlloca->findReg(icmpInstr->operands_[0], rbb, nullptr, 1), + regAlloca->findReg(icmpInstr->operands_[1], rbb, nullptr, 1), + createRiscvBasicBlock(static_cast(brInstr->operands_[1])), + createRiscvBasicBlock(static_cast(brInstr->operands_[2])), + rbb); + return instr; +} + +ICmpRiscvInstr *RiscvBuilder::createICMPSInstr(RegAlloca *regAlloca, + ICmpInst *icmpInstr, + RiscvBasicBlock *rbb) { + bool swap = ICmpSRiscvInstr::ICmpOpSName.count(icmpInstr->icmp_op_) == 0; + if (swap) { + std::swap(icmpInstr->operands_[0], icmpInstr->operands_[1]); + icmpInstr->icmp_op_ = + ICmpRiscvInstr::ICmpOpEquiv.find(icmpInstr->icmp_op_)->second; + } + bool inv = false; + switch (icmpInstr->icmp_op_) { + case ICmpInst::ICMP_SGE: + case ICmpInst::ICMP_SLE: + case ICmpInst::ICMP_UGE: + case ICmpInst::ICMP_ULE: + inv = true; + default: + break; + } + ICmpSRiscvInstr *instr = new ICmpSRiscvInstr( + icmpInstr->icmp_op_, + regAlloca->findReg(icmpInstr->operands_[0], rbb, nullptr, 1), + regAlloca->findReg(icmpInstr->operands_[1], rbb, nullptr, 1), + regAlloca->findReg(icmpInstr, rbb, nullptr, 1, 0), rbb); + rbb->addInstrBack(instr); + if (inv) { + auto instr_reg = regAlloca->findReg(icmpInstr, rbb, nullptr, 1, 0); + rbb->addInstrBack(new BinaryRiscvInst(RiscvInstr::XORI, instr_reg, + new RiscvConst(1), instr_reg, rbb)); + } + return instr; +} + +RiscvInstr *RiscvBuilder::createFCMPInstr(RegAlloca *regAlloca, + FCmpInst *fcmpInstr, + RiscvBasicBlock *rbb) { + // Deal with always true + if (fcmpInstr->fcmp_op_ == fcmpInstr->FCMP_TRUE || + fcmpInstr->fcmp_op_ == fcmpInstr->FCMP_FALSE) { + auto instr = + new MoveRiscvInst(regAlloca->findReg(fcmpInstr, rbb, nullptr, 1, 0), + fcmpInstr->fcmp_op_ == fcmpInstr->FCMP_TRUE, rbb); + rbb->addInstrBack(instr); + return instr; + } + bool swap = FCmpRiscvInstr::FCmpOpName.count(fcmpInstr->fcmp_op_) == 0; + if (swap) { + std::swap(fcmpInstr->operands_[0], fcmpInstr->operands_[1]); + fcmpInstr->fcmp_op_ = + FCmpRiscvInstr::FCmpOpEquiv.find(fcmpInstr->fcmp_op_)->second; + } + bool inv = false; + bool inv_classify = false; + switch (fcmpInstr->fcmp_op_) { + case FCmpInst::FCMP_ONE: + case FCmpInst::FCMP_UNE: + inv = true; + default: + break; + } + switch (fcmpInstr->fcmp_op_) { + case FCmpInst::FCMP_OEQ: + case FCmpInst::FCMP_OGT: + case FCmpInst::FCMP_OGE: + case FCmpInst::FCMP_OLT: + case FCmpInst::FCMP_OLE: + case FCmpInst::FCMP_ONE: + case FCmpInst::FCMP_ORD: + inv_classify = true; + default: + break; + } + + if (inv_classify) { + std::cerr << "[Warning] Not implemented FCLASS yet.\n"; + } + FCmpRiscvInstr *instr = new FCmpRiscvInstr( + fcmpInstr->fcmp_op_, + regAlloca->findReg(fcmpInstr->operands_[0], rbb, nullptr, 1), + regAlloca->findReg(fcmpInstr->operands_[1], rbb, nullptr, 1), + regAlloca->findReg(fcmpInstr, rbb, nullptr, 1, 0), rbb); + rbb->addInstrBack(instr); + if (inv) { + auto instr_reg = regAlloca->findReg(fcmpInstr, rbb, nullptr, 1, 0); + rbb->addInstrBack(new BinaryRiscvInst(RiscvInstr::XORI, instr_reg, + new RiscvConst(1), instr_reg, rbb)); + return instr; + } + return instr; +} + +CallRiscvInst *RiscvBuilder::createCallInstr(RegAlloca *regAlloca, + CallInst *callInstr, + RiscvBasicBlock *rbb) { + // push 指令需要寄存器 + int argnum = callInstr->operands_.size() - 1; + // 涉及从Function 到RISCV function转换问题(第一个参数) + CallRiscvInst *instr = + new CallRiscvInst(createRiscvFunction(static_cast( + callInstr->operands_[argnum])), + rbb); + return instr; +} + +// 注意:return语句本身并不负责返回值的传递,该语句由storeRet函数实现 +ReturnRiscvInst *RiscvBuilder::createRetInstr(RegAlloca *regAlloca, + ReturnInst *returnInstr, + RiscvBasicBlock *rbb, + RiscvFunction *rfoo) { + RiscvOperand *reg_to_save = nullptr; + + // If ret i32 %4 + if (returnInstr->num_ops_ > 0) { + // 写返回值到 a0/fa0 中 + auto operand = returnInstr->operands_[0]; + if (operand->type_->tid_ == Type::TypeID::IntegerTyID) + reg_to_save = regAlloca->findSpecificReg(operand, "a0", rbb); + else if (operand->type_->tid_ == Type::TypeID::FloatTyID) + reg_to_save = regAlloca->findSpecificReg(operand, "fa0", rbb); + // auto instr = regAlloca->writeback(reg_to_save, rbb); + + rbb->addInstrBack(new MoveRiscvInst( + reg_to_save, regAlloca->findReg(operand, rbb, nullptr), rbb)); + } + + return new ReturnRiscvInst(rbb); +} + +BranchRiscvInstr *RiscvBuilder::createBrInstr(RegAlloca *regAlloca, + BranchInst *brInstr, + RiscvBasicBlock *rbb) { + + BranchRiscvInstr *instr; + if (brInstr->num_ops_ == 1) { + instr = new BranchRiscvInstr( + nullptr, nullptr, + createRiscvBasicBlock(static_cast(brInstr->operands_[0])), + rbb); + } else { + instr = new BranchRiscvInstr( + regAlloca->findReg(brInstr->operands_[0], rbb, nullptr, 1), + createRiscvBasicBlock(static_cast(brInstr->operands_[1])), + createRiscvBasicBlock(static_cast(brInstr->operands_[2])), + rbb); + } + return instr; +} + +SiToFpRiscvInstr *RiscvBuilder::createSiToFpInstr(RegAlloca *regAlloca, + SiToFpInst *sitofpInstr, + RiscvBasicBlock *rbb) { + return new SiToFpRiscvInstr( + regAlloca->findReg(sitofpInstr->operands_[0], rbb, nullptr, 1), + regAlloca->findReg(static_cast(sitofpInstr), rbb, nullptr, 1, 0), + rbb); +} + +FpToSiRiscvInstr *RiscvBuilder::createFptoSiInstr(RegAlloca *regAlloca, + FpToSiInst *fptosiInstr, + RiscvBasicBlock *rbb) { + return new FpToSiRiscvInstr( + regAlloca->findReg(fptosiInstr->operands_[0], rbb, nullptr, 1), + regAlloca->findReg(static_cast(fptosiInstr), rbb, nullptr, 1, 0), + rbb); +} + +// 固定采用x30作为偏移量,x31作为乘法的LI指令地址 +RiscvInstr *RiscvBuilder::solveGetElementPtr(RegAlloca *regAlloca, + GetElementPtrInst *instr, + RiscvBasicBlock *rbb) { + Value *op0 = instr->get_operand(0); + RiscvOperand *dest = getRegOperand("t2"); + bool isConst = 1; // 能否用确定的形如 -12(sp)访问 + int finalOffset = 0; + if (dynamic_cast(op0) != nullptr) { + // 全局变量:使用la指令取基础地址 + isConst = 0; + rbb->addInstrBack(new LoadAddressRiscvInstr(dest, op0->name_, rbb)); + } else if (auto oi = dynamic_cast(op0)) { + // 获取指针指向的地址 + int varOffset = 0; + + rbb->addInstrBack(new MoveRiscvInst( + dest, regAlloca->findReg(op0, rbb, nullptr, 1, 1), rbb)); + + finalOffset += varOffset; + } + int curTypeSize = 0; + unsigned int num_operands = instr->num_ops_; + int indexVal, totalOffset = 0; + Type *cur_type = + static_cast(instr->get_operand(0)->type_)->contained_; + for (unsigned int i = 1; i <= num_operands - 1; i++) { + if (i > 1) + cur_type = static_cast(cur_type)->contained_; + Value *opi = instr->get_operand(i); + curTypeSize = calcTypeSize(cur_type); + if (auto ci = dynamic_cast(opi)) { + indexVal = ci->value_; + totalOffset += indexVal * curTypeSize; + } else { + // 存在变量参与偏移量计算 + isConst = 0; + // 考虑目标数是int还是float + RiscvOperand *mulTempReg = getRegOperand("t3"); + rbb->addInstrBack(new MoveRiscvInst(mulTempReg, curTypeSize, rbb)); + rbb->addInstrBack(new BinaryRiscvInst( + RiscvInstr::InstrType::MUL, regAlloca->findReg(opi, rbb, nullptr, 1), + mulTempReg, mulTempReg, rbb)); + rbb->addInstrBack(new BinaryRiscvInst(RiscvInstr::InstrType::ADD, + mulTempReg, dest, dest, rbb)); + } + } + // if (totalOffset > 0) + rbb->addInstrBack(new BinaryRiscvInst(RiscvInstr::InstrType::ADDI, dest, + new RiscvConst(totalOffset), dest, + rbb)); + rbb->addInstrBack( + new StoreRiscvInst(instr->type_, dest, regAlloca->findMem(instr), rbb)); + return nullptr; +} + +void RiscvBuilder::initRetInstr(RegAlloca *regAlloca, RiscvInstr *returnInstr, + RiscvBasicBlock *rbb, RiscvFunction *foo) { + // 将被保护的寄存器还原 + // ! FP 必须被最后还原。 + int curSP = foo->querySP(); + auto reg_to_recover = regAlloca->savedRegister; + auto reg_used = regAlloca->getUsedReg(); + reverse(reg_to_recover.begin(), reg_to_recover.end()); + for (auto reg : reg_to_recover) + if (reg_used.find(reg) != reg_used.end()) { + if (reg->getType() == reg->IntReg) + rbb->addInstrBefore(new LoadRiscvInst(new Type(Type::PointerTyID), reg, + new RiscvIntPhiReg("fp", curSP), + rbb), + returnInstr); + else + rbb->addInstrBefore(new LoadRiscvInst(new Type(Type::FloatTyID), reg, + new RiscvIntPhiReg("fp", curSP), + rbb), + returnInstr); + curSP += VARIABLE_ALIGN_BYTE; + } + + // 还原 fp + rbb->addInstrBefore(new LoadRiscvInst(new Type(Type::PointerTyID), + getRegOperand("fp"), + new RiscvIntPhiReg("fp", curSP), rbb), + returnInstr); + + // 释放栈帧 + rbb->addInstrBefore(new BinaryRiscvInst(RiscvInstr::ADDI, getRegOperand("sp"), + new RiscvConst(-foo->querySP()), + getRegOperand("sp"), rbb), + returnInstr); +} + +RiscvBasicBlock *RiscvBuilder::transferRiscvBasicBlock(BasicBlock *bb, + RiscvFunction *foo) { + int translationCount = 0; + RiscvBasicBlock *rbb = createRiscvBasicBlock(bb); + Instruction *forward = nullptr; // 前置指令,用于icmp、fcmp和branch指令合并 + bool brFound = false; + for (Instruction *instr : bb->instr_list_) { + switch (instr->op_id_) { + case Instruction::Ret: + // Before leaving basic block writeback all registers + foo->regAlloca->writeback_all(rbb); + brFound = true; + // 在翻译过程中先指ret,恢复寄存器等操作在第二遍扫描的时候再插入 + rbb->addInstrBack(this->createRetInstr( + foo->regAlloca, static_cast(instr), rbb, foo)); + break; + // 分支指令 + case Instruction::Br: + // Before leaving basic block writeback all registers + foo->regAlloca->writeback_all(rbb); + brFound = true; + + rbb->addInstrBack(this->createBrInstr( + foo->regAlloca, static_cast(instr), rbb)); + break; + case Instruction::Add: + case Instruction::Sub: + case Instruction::Mul: + case Instruction::SDiv: + case Instruction::SRem: + case Instruction::UDiv: + case Instruction::URem: + case Instruction::FAdd: + case Instruction::FSub: + case Instruction::FMul: + case Instruction::FDiv: + case Instruction::Shl: + case Instruction::LShr: + case Instruction::AShr: + case Instruction::And: + case Instruction::Or: + case Instruction::Xor: + rbb->addInstrBack(this->createBinaryInstr( + foo->regAlloca, static_cast(instr), rbb)); + // foo->regAlloca->writeback(static_cast(instr), rbb); + break; + case Instruction::FNeg: + rbb->addInstrBack(this->createUnaryInstr( + foo->regAlloca, static_cast(instr), rbb)); + // foo->regAlloca->writeback(static_cast(instr), rbb); + break; + case Instruction::PHI: + break; + // 直接删除的指令 + case Instruction::BitCast: + break; + case Instruction::ZExt: + // 等价一条合流语句操作 + break; + case Instruction::Alloca: + break; + case Instruction::GetElementPtr: { + this->solveGetElementPtr(foo->regAlloca, + static_cast(instr), rbb); + // Writeback inside solveGetElementPtr(). + break; + } + case Instruction::FPtoSI: + rbb->addInstrBack(this->createFptoSiInstr( + foo->regAlloca, static_cast(instr), rbb)); + // foo->regAlloca->writeback(static_cast(instr), rbb); + break; + case Instruction::SItoFP: + rbb->addInstrBack(this->createSiToFpInstr( + foo->regAlloca, static_cast(instr), rbb)); + // foo->regAlloca->writeback(static_cast(instr), rbb); + break; + case Instruction::Load: { + auto instrSet = this->createLoadInstr( + foo->regAlloca, static_cast(instr), rbb); + for (auto x : instrSet) + rbb->addInstrBack(x); + // foo->regAlloca->writeback(static_cast(instr), rbb); + break; + } + case Instruction::Store: { + auto instrSet = this->createStoreInstr( + foo->regAlloca, static_cast(instr), rbb); + for (auto *x : instrSet) + rbb->addInstrBack(x); + // Store Instruction returns void value. + break; + } + case Instruction::ICmp: + createICMPSInstr(foo->regAlloca, static_cast(instr), rbb); + // foo->regAlloca->writeback(static_cast(instr), rbb); + break; + case Instruction::FCmp: + createFCMPInstr(foo->regAlloca, static_cast(instr), rbb); + // foo->regAlloca->writeback(static_cast(instr), rbb); + break; + case Instruction::Call: { + // 注意:该部分并未单独考虑系统函数! + // 注意:区分float还是int调用是看寄存器分配部分实现 + // 说明:call函数部分本身需要进行栈寄存器调整,调整到0栈帧供新函数使用 + // 除此之外不在任何地方调整sp的值 + // 在call语句结束之后要手动恢复回原来的栈帧 + CallInst *curInstr = static_cast(instr); + RiscvFunction *calleeFoo = createRiscvFunction( + static_cast(curInstr->operands_.back())); + + // 根据函数调用约定,按需传递参数。 + + int sp_shift_for_paras = 0; + int sp_shift_alignment_padding = 0; // Align sp pointer to 16-byte + int paraShift = 0; + + int intRegCount = 0, floatRegCount = 0; + + // 计算存储参数需要的额外栈帧大小 + for (int i = 0; i < curInstr->operands_.size() - 1; i++) { + sp_shift_for_paras += VARIABLE_ALIGN_BYTE; + } + + sp_shift_alignment_padding = + 16 - ((abs(foo->querySP()) + sp_shift_for_paras) & 15); + sp_shift_for_paras += sp_shift_alignment_padding; + + // 为参数申请栈帧 + rbb->addInstrBack(new BinaryRiscvInst( + BinaryRiscvInst::ADDI, getRegOperand("sp"), + new RiscvConst(-sp_shift_for_paras), getRegOperand("sp"), rbb)); + + // 将参数移动至寄存器与内存中 + for (int i = 0; i < curInstr->operands_.size() - 1; i++) { + std::string name = ""; + auto operand = curInstr->operands_[i]; + if (operand->type_->tid_ != Type::FloatTyID) { + if (intRegCount < 8) + name = "a" + std::to_string(intRegCount); + intRegCount++; + } else if (operand->type_->tid_ == Type::FloatTyID) { + if (floatRegCount < 8) + name = "fa" + std::to_string(floatRegCount); + floatRegCount++; + } + // 将额外的参数直接写入内存中 + if (name.empty()) { + if (operand->type_->tid_ != Type::FloatTyID) { + rbb->addInstrBack(new StoreRiscvInst( + operand->type_, + foo->regAlloca->findSpecificReg(operand, "t1", rbb), + new RiscvIntPhiReg("sp", paraShift), rbb)); + } else { + rbb->addInstrBack(new StoreRiscvInst( + operand->type_, + foo->regAlloca->findSpecificReg(operand, "ft1", rbb), + new RiscvIntPhiReg("sp", paraShift), rbb)); + } + } else { + foo->regAlloca->findSpecificReg(operand, name, rbb, nullptr); + } + paraShift += VARIABLE_ALIGN_BYTE; // Add operand size lastly + } + + // Call the function. + rbb->addInstrBack(this->createCallInstr(foo->regAlloca, curInstr, rbb)); + + // 为参数释放栈帧 + rbb->addInstrBack(new BinaryRiscvInst( + BinaryRiscvInst::ADDI, getRegOperand("sp"), + new RiscvConst(sp_shift_for_paras), getRegOperand("sp"), rbb)); + + // At last, save return value (a0) to target value. + if (curInstr->type_->tid_ != curInstr->type_->VoidTyID) { + if (curInstr->type_->tid_ != curInstr->type_->FloatTyID) { + rbb->addInstrBack( + new StoreRiscvInst(new IntegerType(32), getRegOperand("a0"), + foo->regAlloca->findMem(curInstr), rbb)); + } else { + rbb->addInstrBack(new StoreRiscvInst( + new Type(Type::FloatTyID), getRegOperand("fa0"), + foo->regAlloca->findMem(curInstr), rbb)); + } + } + break; + } + } + // std::cout << "FINISH TRANSFER " << ++translationCount << "Codes\n"; + } + // If br instruction is not found, then leave basic block at the block's end. + if (!brFound) { + foo->regAlloca->writeback_all(rbb); + } + return rbb; +} + +// 总控程序 +std::string RiscvBuilder::buildRISCV(Module *m) { + this->rm = new RiscvModule(); + std::string data = ".align 2\n.section .data\n"; // Add align attribute + // 全局变量 + if (m->global_list_.size()) { + for (GlobalVariable *gb : m->global_list_) { + auto curType = static_cast(gb->type_)->contained_; + RiscvGlobalVariable *curGB = nullptr; + Type *containedType = nullptr; + switch (curType->tid_) { + case Type::PointerTyID: + assert(false); + break; + case Type::ArrayTyID: + containedType = curType; + while (1) { + if (containedType->tid_ == Type::TypeID::ArrayTyID) + containedType = static_cast(containedType)->contained_; + else + break; + } + if (containedType->tid_ == Type::IntegerTyID) { + curGB = new RiscvGlobalVariable(RiscvOperand::IntImm, gb->name_, + gb->is_const_, gb->init_val_, + calcTypeSize(curType) / 4); + rm->addGlobalVariable(curGB); + data += curGB->print(); + } else if (containedType->tid_ == Type::FloatTyID) { + curGB = new RiscvGlobalVariable(RiscvOperand::FloatImm, gb->name_, + gb->is_const_, gb->init_val_, + calcTypeSize(curType) / 4); + rm->addGlobalVariable(curGB); + data += curGB->print(); + } + break; + case Type::TypeID::IntegerTyID: { + auto curGB = + new RiscvGlobalVariable(RiscvOperand::OpTy::IntImm, gb->name_, + gb->is_const_, gb->init_val_); + assert(curGB != nullptr); + rm->addGlobalVariable(curGB); + data += curGB->print(); + break; + } + case Type::TypeID::FloatTyID: { + auto curGB = + new RiscvGlobalVariable(RiscvOperand::OpTy::FloatImm, gb->name_, + gb->is_const_, gb->init_val_); + rm->addGlobalVariable(curGB); + data += curGB->print(); + break; + } + } + } + } + // 浮点常量进入内存 + int ConstFloatCount = 0; + std::string code = ".section .text\n"; + // 函数体 + // 预处理:首先合并所有的合流语句操作,然后在分配单元(storeOnStack)部分使用DSU合并 + for (Function *foo : m->function_list_) { + auto rfoo = createRiscvFunction(foo); + rm->addFunction(rfoo); + if (rfoo->is_libfunc()) { + auto *libFunc = createSyslibFunc(foo); + if (libFunc != nullptr) + code += libFunc->print(); + continue; + } + for (BasicBlock *bb : foo->basic_blocks_) + for (Instruction *instr : bb->instr_list_) + if (instr->op_id_ == Instruction::OpID::PHI) { + for (auto *operand : instr->operands_) + rfoo->regAlloca->DSU_for_Variable.merge( + operand, static_cast(instr)); + } else if (instr->op_id_ == Instruction::OpID::ZExt) { + rfoo->regAlloca->DSU_for_Variable.merge(instr->operands_[0], + static_cast(instr)); + } else if (instr->op_id_ == Instruction::OpID::BitCast) { + // std::cerr << "[Debug] [DSU] Bitcast Instruction: Merge value " + // << static_cast(instr)->print() << " to " + // << instr->operands_[0]->print() << " ." << std::endl; + rfoo->regAlloca->DSU_for_Variable.merge(static_cast(instr), + instr->operands_[0]); + } + // 将该函数内的浮点常量全部处理出来并告知寄存器分配单元 + for (BasicBlock *bb : foo->basic_blocks_) + for (Instruction *instr : bb->instr_list_) + for (auto *Operand : instr->operands_) + // 找到浮点常数,存入内存,写入全局变量区 + if (dynamic_cast(Operand) != nullptr) { + std::string curFloatName = + "FloatConst" + std::to_string(ConstFloatCount); + ConstFloatCount++; + std::string valString = + dynamic_cast(Operand)->print32(); + while (valString.length() < 10) + valString += "0"; + data += + curFloatName + ":\n\t.word\t" + valString.substr(0, 10) + "\n"; + rfoo->regAlloca->setPosition(Operand, + new RiscvFloatPhiReg(curFloatName, 0)); + } + // 首先检查所有的alloca指令,加入一个基本块进行寄存器保护以及栈空间分配 + RiscvBasicBlock *initBlock = createRiscvBasicBlock(); + std::map haveAllocated; + int IntParaCount = 0, FloatParaCount = 0; + int sp_shift_for_paras = 0; + int paraShift = 0; + + rfoo->setSP(0); // set sp to 0 initially. + + // Lambda function to write value to stack frame. + auto storeOnStack = [&](Value **val) { + if (val == nullptr) + return; + assert(*val != nullptr); + if (haveAllocated.count(*val)) + return; + // 几种特殊类型,不需要分栈空间 + if (dynamic_cast(*val) != nullptr) + return; + if (dynamic_cast(*val) != nullptr) + return; + // 注意:函数参数不用分配,而是直接指定! + // 这里设定是:v开头的是局部变量,arg开头的是函数寄存器变量 + // 无名常量 + if ((*val)->name_.empty()) + return; + // 全局变量不用给他保存栈上地址,它本身就有对应的内存地址,直接忽略 + if (dynamic_cast(*val) != nullptr) { + auto curType = (*val)->type_; + while (1) { + if (curType->tid_ == Type::TypeID::ArrayTyID) + curType = static_cast(curType)->contained_; + else if (curType->tid_ == Type::TypeID::PointerTyID) + curType = static_cast(curType)->contained_; + else + break; + } + if (curType->tid_ != Type::TypeID::FloatTyID) + rfoo->regAlloca->setPosition(*val, + new RiscvIntPhiReg((*val)->name_, 0, 1)); + else + rfoo->regAlloca->setPosition( + *val, new RiscvFloatPhiReg((*val)->name_, 0, 1)); + return; + } + // 除了全局变量之外的参数 + if (dynamic_cast(*val) != nullptr) { + // 不用额外分配空间 + // 整型参数 + if ((*val)->type_->tid_ == Type::TypeID::IntegerTyID || + (*val)->type_->tid_ == Type::TypeID::PointerTyID) { + // Pointer type's size is set to 8 byte. + if (IntParaCount < 8) + rfoo->regAlloca->setPositionReg( + *val, getRegOperand("a" + std::to_string(IntParaCount))); + rfoo->regAlloca->setPosition( + *val, new RiscvIntPhiReg(NamefindReg("fp"), paraShift)); + IntParaCount++; + } + // 浮点参数 + else { + assert((*val)->type_->tid_ == Type::TypeID::FloatTyID); + // 寄存器有 + if (FloatParaCount < 8) { + rfoo->regAlloca->setPositionReg( + *val, getRegOperand("fa" + std::to_string(FloatParaCount))); + } + rfoo->regAlloca->setPosition( + *val, new RiscvFloatPhiReg(NamefindReg("fp"), paraShift)); + FloatParaCount++; + } + paraShift += VARIABLE_ALIGN_BYTE; + } + // 函数内变量 + else { + int curSP = rfoo->querySP(); + RiscvOperand *stackPos = static_cast( + new RiscvIntPhiReg(NamefindReg("fp"), curSP - VARIABLE_ALIGN_BYTE)); + rfoo->regAlloca->setPosition(static_cast(*val), stackPos); + rfoo->addTempVar(stackPos); + } + haveAllocated[*val] = 1; + }; + + // 关联函数参数、寄存器与内存 + for (Value *arg : foo->arguments_) + storeOnStack(&arg); + + for (BasicBlock *bb : foo->basic_blocks_) + for (Instruction *instr : bb->instr_list_) + if (instr->op_id_ != Instruction::OpID::PHI && + instr->op_id_ != Instruction::OpID::ZExt && + instr->op_id_ != Instruction::OpID::Alloca) { + // 所有的函数局部变量都要压入栈 + Value *tempPtr = static_cast(instr); + storeOnStack(&tempPtr); + for (auto *val : instr->operands_) { + tempPtr = static_cast(val); + storeOnStack(&tempPtr); + } + } + for (BasicBlock *bb : foo->basic_blocks_) + for (Instruction *instr : bb->instr_list_) + if (instr->op_id_ == Instruction::OpID::Alloca) { + // 分配指针,并且将指针地址也同步保存 + auto curInstr = static_cast(instr); + int curTypeSize = calcTypeSize(curInstr->alloca_ty_); + rfoo->storeArray(curTypeSize); + int curSP = rfoo->querySP(); + RiscvOperand *ptrPos = new RiscvIntPhiReg(NamefindReg("fp"), curSP); + rfoo->regAlloca->setPosition(static_cast(instr), ptrPos); + rfoo->regAlloca->setPointerPos(static_cast(instr), ptrPos); + } + + // 添加初始化基本块 + rfoo->addBlock(initBlock); + // 翻译语句并计算被使用的寄存器 + for (BasicBlock *bb : foo->basic_blocks_) + rfoo->addBlock(this->transferRiscvBasicBlock(bb, rfoo)); + rfoo->ChangeBlock(initBlock, 0); + + // 保护寄存器 + rfoo->shiftSP(-VARIABLE_ALIGN_BYTE); + int fp_sp = rfoo->querySP(); // 为保护 fp 分配空间 + auto ®_to_save = rfoo->regAlloca->savedRegister; + auto reg_used = rfoo->regAlloca->getUsedReg(); + for (auto reg : reg_to_save) + if (reg_used.find(reg) != reg_used.end()) { + rfoo->shiftSP(-VARIABLE_ALIGN_BYTE); + if (reg->getType() == reg->IntReg) + initBlock->addInstrBack(new StoreRiscvInst( + new Type(Type::PointerTyID), reg, + new RiscvIntPhiReg(NamefindReg("fp"), rfoo->querySP()), + initBlock)); + else + initBlock->addInstrBack(new StoreRiscvInst( + new Type(Type::FloatTyID), reg, + new RiscvIntPhiReg(NamefindReg("fp"), rfoo->querySP()), + initBlock)); + } + + // 分配整体的栈空间,并设置s0为原sp + initBlock->addInstrFront(new BinaryRiscvInst( + RiscvInstr::ADDI, getRegOperand("sp"), new RiscvConst(-rfoo->querySP()), + getRegOperand("fp"), + initBlock)); // 3: fp <- t0 + initBlock->addInstrFront(new StoreRiscvInst( + new Type(Type::PointerTyID), getRegOperand("fp"), + new RiscvIntPhiReg(NamefindReg("sp"), fp_sp - rfoo->querySP()), + initBlock)); // 2: 保护 fp + initBlock->addInstrFront(new BinaryRiscvInst( + RiscvInstr::ADDI, getRegOperand("sp"), new RiscvConst(rfoo->querySP()), + getRegOperand("sp"), initBlock)); // 1: 分配栈帧 + + // 扫描所有的返回语句并插入寄存器还原等相关内容 + for (RiscvBasicBlock *rbb : rfoo->blk) + for (RiscvInstr *rinstr : rbb->instruction) + if (rinstr->type_ == rinstr->RET) { + initRetInstr(rfoo->regAlloca, rinstr, rbb, rfoo); + break; + } + + code += rfoo->print(); + } + return data + code; +} + +/** + * 辅助函数,计算一个类型的字节大小。 + * @param ty 将要计算的类型 `ty` 。 + * @return 返回类型的字节大小。 + */ +int calcTypeSize(Type *ty) { + int totalsize = 1; + while (ty->tid_ == Type::ArrayTyID) { + totalsize *= static_cast(ty)->num_elements_; + ty = static_cast(ty)->contained_; + } + assert(ty->tid_ == Type::IntegerTyID || ty->tid_ == Type::FloatTyID || + ty->tid_ == Type::PointerTyID); + if (ty->tid_ == Type::IntegerTyID || ty->tid_ == Type::FloatTyID) + totalsize *= 4; + else if (ty->tid_ == Type::PointerTyID) + totalsize *= 8; + return totalsize; +} diff --git a/compiler/src/riscv/backend.h b/compiler/src/riscv/backend.h new file mode 100644 index 0000000..12a2649 --- /dev/null +++ b/compiler/src/riscv/backend.h @@ -0,0 +1,82 @@ +#ifndef BACKENDH +#define BACKENDH +#include "instruction.h" +#include "ir.h" +#include "optimize.h" +#include "regalloc.h" +#include "riscv.h" +#include + +// 建立IR到RISCV指令集的映射 +const extern std::map toRiscvOp; + +extern int LabelCount; +extern std::map rbbLabel; +extern std::map functionLabel; + +// 下面的函数仅为一个basic +// block产生一个标号,指令集为空,需要使用总控程序中具体遍历该bb才会产生内部指令 +RiscvBasicBlock *createRiscvBasicBlock(BasicBlock *bb = nullptr); +RiscvFunction *createRiscvFunction(Function *foo = nullptr); +std::string toLabel(int ind); +int calcTypeSize(Type *ty); + +// 总控程序 +class RiscvBuilder { +private: + void initializeRegisterFile(); + +public: + RiscvBuilder() { + rm = new RiscvModule(); + initializeRegisterFile(); + } + RiscvModule *rm; + // phi语句的合流:此处建立一个并查集DSU_for_Variable维护相同的变量。 + // 例如,对于if (A) y1=do something else y2=do another thing. Phi y3 y1, y2 + std::string buildRISCV(Module *m); + + // 下面的语句是需要生成对应riscv语句 + // Zext语句零扩展,因而没有必要 + // ZExtRiscvInstr createZextInstr(ZextInst *instr); + // void resolveLibfunc(RiscvFunction *foo); + BinaryRiscvInst *createBinaryInstr(RegAlloca *regAlloca, + BinaryInst *binaryInstr, + RiscvBasicBlock *rbb); + UnaryRiscvInst *createUnaryInstr(RegAlloca *regAlloca, UnaryInst *unaryInstr, + RiscvBasicBlock *rbb); + std::vector createStoreInstr(RegAlloca *regAlloca, + StoreInst *storeInstr, + RiscvBasicBlock *rbb); + std::vector createLoadInstr(RegAlloca *regAlloca, + LoadInst *loadInstr, + RiscvBasicBlock *rbb); + ICmpRiscvInstr *createICMPInstr(RegAlloca *regAlloca, ICmpInst *icmpInstr, + BranchInst *brInstr, RiscvBasicBlock *rbb); + ICmpRiscvInstr *createICMPSInstr(RegAlloca *regAlloca, ICmpInst *icmpInstr, + RiscvBasicBlock *rbb); + RiscvInstr *createFCMPInstr(RegAlloca *regAlloca, FCmpInst *fcmpInstr, + RiscvBasicBlock *rbb); + SiToFpRiscvInstr *createSiToFpInstr(RegAlloca *regAlloca, + SiToFpInst *sitofpInstr, + RiscvBasicBlock *rbb); + FpToSiRiscvInstr *createFptoSiInstr(RegAlloca *regAlloca, + FpToSiInst *fptosiInstr, + RiscvBasicBlock *rbb); + CallRiscvInst *createCallInstr(RegAlloca *regAlloca, CallInst *callInstr, + RiscvBasicBlock *rbb); + RiscvBasicBlock *transferRiscvBasicBlock(BasicBlock *bb, RiscvFunction *foo); + ReturnRiscvInst *createRetInstr(RegAlloca *regAlloca, ReturnInst *returnInstr, + RiscvBasicBlock *rbb, RiscvFunction *rfoo); + BranchRiscvInstr *createBrInstr(RegAlloca *regAlloca, BranchInst *brInstr, + RiscvBasicBlock *rbb); + RiscvInstr *solveGetElementPtr(RegAlloca *regAlloca, GetElementPtrInst *instr, + RiscvBasicBlock *rbb); + + /** + * 在返回语句前插入必要的语句。 + */ + void initRetInstr(RegAlloca *regAlloca, RiscvInstr *returnInstr, + RiscvBasicBlock *rbb, RiscvFunction *foo); +}; +#endif // !BACKENDH \ No newline at end of file diff --git a/compiler/src/riscv/instruction.cpp b/compiler/src/riscv/instruction.cpp new file mode 100644 index 0000000..ed21ccc --- /dev/null +++ b/compiler/src/riscv/instruction.cpp @@ -0,0 +1,360 @@ +#include "instruction.h" +#include "riscv.h" +#include +#include +// IR的指令转变到RISV的指令 +std::map instrTy2Riscv = { + {RiscvInstr::ADD, "ADD"}, {RiscvInstr::ADDI, "ADDI"}, + {RiscvInstr::ADDIW, "ADDIW"}, {RiscvInstr::SUB, "SUB"}, + {RiscvInstr::SUBI, "SUBI"}, {RiscvInstr::FADD, "FADD.S"}, + {RiscvInstr::FSUB, "FSUB.S"}, {RiscvInstr::FMUL, "FMUL.S"}, + {RiscvInstr::FDIV, "FDIV.S"}, {RiscvInstr::MUL, "MUL"}, + {RiscvInstr::DIV, "DIV"}, {RiscvInstr::REM, "REM"}, + {RiscvInstr::AND, "AND"}, {RiscvInstr::OR, "OR"}, + {RiscvInstr::ANDI, "ANDI"}, {RiscvInstr::ORI, "ORI"}, + {RiscvInstr::XOR, "XOR"}, {RiscvInstr::XORI, "XORI"}, + {RiscvInstr::RET, "RET"}, {RiscvInstr::FPTOSI, "FCVT.W.S"}, + {RiscvInstr::SITOFP, "FCVT.S.W"}, {RiscvInstr::FMV, "FMV.S"}, + {RiscvInstr::CALL, "CALL"}, {RiscvInstr::LI, "LI"}, + {RiscvInstr::MOV, "MV"}, {RiscvInstr::PUSH, "PUSH"}, + {RiscvInstr::POP, "POP"}, {RiscvInstr::SW, "SW"}, + {RiscvInstr::LW, "LW"}, {RiscvInstr::FSW, "FSW"}, + {RiscvInstr::FLW, "FLW"}, {RiscvInstr::SHL, "SLL"}, + {RiscvInstr::ASHR, "SRA"}, {RiscvInstr::SHLI, "SLLI"}, + {RiscvInstr::LSHR, "SRL"}, {RiscvInstr::ASHRI, "SRAI"}, + {RiscvInstr::LSHRI, "SRLI"}, +}; +// Instruction from opid to string +const std::map ICmpRiscvInstr::ICmpOpName = { + {ICmpInst::ICmpOp::ICMP_EQ, "BEQ"}, {ICmpInst::ICmpOp::ICMP_NE, "BNE"}, + {ICmpInst::ICmpOp::ICMP_UGE, "BGEU"}, {ICmpInst::ICmpOp::ICMP_ULT, "BLTU"}, + {ICmpInst::ICmpOp::ICMP_SGE, "BGE"}, {ICmpInst::ICmpOp::ICMP_SLT, "BLT"}, + {ICmpInst::ICmpOp::ICMP_SLE, "BLE"}}; +const std::map ICmpRiscvInstr::ICmpOpSName = { + {ICmpInst::ICmpOp::ICMP_EQ, "SEQZ"}, {ICmpInst::ICmpOp::ICMP_NE, "SNEZ"}, + {ICmpInst::ICmpOp::ICMP_UGE, "SLTU"}, {ICmpInst::ICmpOp::ICMP_ULT, "SLTU"}, + {ICmpInst::ICmpOp::ICMP_SGE, "SLT"}, {ICmpInst::ICmpOp::ICMP_SLT, "SLT"}}; +const std::map ICmpRiscvInstr::ICmpOpEquiv = + {{ICmpInst::ICmpOp::ICMP_ULE, ICmpInst::ICmpOp::ICMP_UGE}, + {ICmpInst::ICmpOp::ICMP_UGT, ICmpInst::ICmpOp::ICMP_ULT}, + {ICmpInst::ICmpOp::ICMP_SLE, ICmpInst::ICmpOp::ICMP_SGE}, + {ICmpInst::ICmpOp::ICMP_SGT, ICmpInst::ICmpOp::ICMP_SLT}}; +const std::map FCmpRiscvInstr::FCmpOpName = { + {FCmpInst::FCmpOp::FCMP_OLT, "FLT.S"}, + {FCmpInst::FCmpOp::FCMP_ULT, "FLT.S"}, + {FCmpInst::FCmpOp::FCMP_OLE, "FLE.S"}, + {FCmpInst::FCmpOp::FCMP_ULE, "FLE.S"}, + {FCmpInst::FCmpOp::FCMP_ORD, "FCLASS.S"}, + {FCmpInst::FCmpOp::FCMP_UNO, "FCLASS.S"}, // 取反 + {FCmpInst::FCmpOp::FCMP_OEQ, "FEQ.S"}, + {FCmpInst::FCmpOp::FCMP_UEQ, "FEQ.S"}, + {FCmpInst::FCmpOp::FCMP_ONE, "FEQ.S"}, // 取反 + {FCmpInst::FCmpOp::FCMP_UNE, "FEQ.S"} // 取反 +}; +const std::map FCmpRiscvInstr::FCmpOpEquiv = + {{FCmpInst::FCmpOp::FCMP_OGT, FCmpInst::FCmpOp::FCMP_OLT}, + {FCmpInst::FCmpOp::FCMP_UGT, FCmpInst::FCmpOp::FCMP_ULT}, + {FCmpInst::FCmpOp::FCMP_OGE, FCmpInst::FCmpOp::FCMP_OLE}, + {FCmpInst::FCmpOp::FCMP_UGE, FCmpInst::FCmpOp::FCMP_ULE}}; +std::string print_as_op(Value *v, bool print_ty); +std::string print_cmp_type(ICmpInst::ICmpOp op); +std::string print_fcmp_type(FCmpInst::FCmpOp op); + +RiscvInstr::RiscvInstr(InstrType type, int op_nums) + : type_(type), parent_(nullptr) { + operand_.resize(op_nums); +} + +RiscvInstr::RiscvInstr(InstrType type, int op_nums, RiscvBasicBlock *bb) + : type_(type), parent_(bb) { + operand_.resize(op_nums); +} + +// 格式:op tar, v1, v2->tar=v1 op v2 +std::string BinaryRiscvInst::print() { + // 这里需要将每个参数根据当前需要进行upcasting + assert(this->operand_.size() == 2); + std::string riscv_instr = "\t\t"; + + bool overflow = false; + if (type_ == ADDI && + std::abs(static_cast(operand_[1])->intval) >= 1024) { + overflow = true; + type_ = ADD; + riscv_instr += "LI\tt6, " + operand_[1]->print(); + riscv_instr += "\n\t\t"; + } + + riscv_instr += instrTy2Riscv.at(this->type_); + if (word && (type_ == ADDI || type_ == ADD || type_ == MUL || type_ == REM || + type_ == DIV)) + riscv_instr += "W"; // Integer word type instruction. + riscv_instr += "\t"; + riscv_instr += this->result_->print(); + riscv_instr += ", "; + riscv_instr += this->operand_[0]->print(); + riscv_instr += ", "; + if (overflow) { + riscv_instr += "t6"; + } else { + riscv_instr += this->operand_[1]->print(); + } + riscv_instr += "\n"; + return riscv_instr; +} + +std::string UnaryRiscvInst::print() { + assert(this->operand_.size() == 1); + std::string riscv_instr = "\t\t"; + riscv_instr += instrTy2Riscv[this->type_]; + riscv_instr += "\t"; + riscv_instr += this->result_->print(); + riscv_instr += ", "; + riscv_instr += this->operand_[0]->print(); + riscv_instr += ", "; + return riscv_instr; +} + +std::string CallRiscvInst::print() { + std::string riscv_instr = "\t\tCALL\t"; + riscv_instr += static_cast(this->operand_[0])->name_; + riscv_instr += "\n"; + return riscv_instr; +} + +// 注意:return 语句不会进行相应的寄存器约定检查 +std::string ReturnRiscvInst::print() { + std::string riscv_instr = "\t\tRET\n"; + return riscv_instr; +} + +std::string PushRiscvInst::print() { + std::string riscv_instr = ""; + int shift = this->basicShift_; + for (auto x : this->operand_) { + shift -= VARIABLE_ALIGN_BYTE; + riscv_instr += + "\t\tSD\t" + x->print() + ", " + std::to_string(shift) + "(sp)\n"; + } + return riscv_instr; +} + +std::string PopRiscvInst::print() { + std::string riscv_instr = ""; + int shift = this->basicShift_; + for (auto x : this->operand_) { + shift -= VARIABLE_ALIGN_BYTE; + riscv_instr += + "\t\tLD\t" + x->print() + ", " + std::to_string(shift) + "(sp)\n"; + } + riscv_instr += "\t\tADDI\tsp, " + std::to_string(-shift) + "\n"; + return riscv_instr; +} + +std::string ICmpRiscvInstr::print() { + std::string riscv_instr = "\t\t"; + // 注意:由于RISCV不支持全部的比较运算,因而需要根据比较条件对式子进行等价变换 + if (ICmpOpName.count(this->icmp_op_) == 0) { + std::swap(this->operand_[0], this->operand_[1]); + this->icmp_op_ = ICmpRiscvInstr::ICmpOpEquiv.find(this->icmp_op_)->second; + } + riscv_instr += ICmpOpName.at(this->icmp_op_) + "\t"; + riscv_instr += this->operand_[0]->print(); + riscv_instr += ", "; + riscv_instr += this->operand_[1]->print(); + riscv_instr += ", "; + riscv_instr += static_cast(this->operand_[2])->name_; + riscv_instr += "\n"; + auto falseLink = dynamic_cast(this->operand_[3]); + // Force Jump + if (falseLink != nullptr) + riscv_instr += "\t\tJ\t" + falseLink->name_ + "\n"; + return riscv_instr; +} + +std::string ICmpSRiscvInstr::print() { + std::string riscv_instr = "\t\t"; + + // If equal or nequal instruction + bool eorne = false; + switch (icmp_op_) { + case ICmpInst::ICMP_EQ: + case ICmpInst::ICMP_NE: + eorne = true; + default: + break; + } + + if (eorne) { + riscv_instr += "SUB\t"; + riscv_instr += "t6"; + riscv_instr += ", "; + riscv_instr += operand_[0]->print(); + riscv_instr += ", "; + riscv_instr += operand_[1]->print(); + riscv_instr += "\n\t\t"; + } + + riscv_instr += ICmpOpSName.at(this->icmp_op_) + "\t"; + riscv_instr += this->result_->print(); + riscv_instr += ", "; + if (!eorne) { + riscv_instr += this->operand_[0]->print(); + riscv_instr += ", "; + riscv_instr += this->operand_[1]->print(); + riscv_instr += "\n"; + } else { + riscv_instr += "t6\n"; + } + return riscv_instr; +} + +std::string FCmpRiscvInstr::print() { + std::string riscv_instr = "\t\t"; + + riscv_instr += FCmpOpName.at(this->fcmp_op_) + "\t"; + riscv_instr += this->result_->print(); + riscv_instr += ", "; + riscv_instr += this->operand_[0]->print(); + riscv_instr += ", "; + riscv_instr += this->operand_[1]->print(); + riscv_instr += "\n"; + return riscv_instr; +} + +std::string StoreRiscvInst::print() { + std::string riscv_instr = "\t\t"; + + auto mem_addr = static_cast(operand_[1]); + bool overflow = mem_addr->overflow(); + + if (overflow) { + riscv_instr += "LI\tt6, " + std::to_string(mem_addr->shift_); + riscv_instr += "\n\t\t"; + riscv_instr += "ADD\tt6, t6, " + mem_addr->MemBaseName; + riscv_instr += "\n\t\t"; + } + + if (this->type.tid_ == Type::FloatTyID) + riscv_instr += "FSW\t"; + else if (this->type.tid_ == Type::IntegerTyID) + riscv_instr += "SW\t"; + else if (this->type.tid_ == Type::PointerTyID) + riscv_instr += "SD\t"; + else { + std::cerr << "[Error] Unknown store instruction type." << std::endl; + std::terminate(); + } + riscv_instr += this->operand_[0]->print(); + riscv_instr += ", "; + + if (overflow) { + riscv_instr += "(t6)"; + } else { + riscv_instr += this->operand_[1]->print(); + } + riscv_instr += "\n"; + return riscv_instr; +} + +// 简易版 load。如果不存在目标内存地址,本条指令不执行 +std::string LoadRiscvInst::print() { + if (this->operand_[0] == nullptr || this->operand_[1] == nullptr) + return ""; + std::string riscv_instr = "\t\t"; + + auto mem_addr = static_cast(operand_[1]); + bool overflow = mem_addr->overflow(); + + if (overflow) { + riscv_instr += "LI\tt6, " + std::to_string(mem_addr->shift_); + riscv_instr += "\n\t\t"; + riscv_instr += "ADD\tt6, t6, " + mem_addr->MemBaseName; + riscv_instr += "\n\t\t"; + } + + if (this->type.tid_ == Type::FloatTyID) + riscv_instr += "FLW\t"; + else if (this->type.tid_ == Type::IntegerTyID) + riscv_instr += "LW\t"; + else if (this->type.tid_ == Type::PointerTyID) + riscv_instr += "LD\t"; + else { + std::cerr << "[Error] Unknown load instruction type." << std::endl; + std::terminate(); + } + riscv_instr += this->operand_[0]->print(); + riscv_instr += ", "; + if (overflow) { + riscv_instr += "(t6)"; + } else { + riscv_instr += this->operand_[1]->print(); + } + riscv_instr += "\n"; + return riscv_instr; +} + +std::string MoveRiscvInst::print() { + // Optmize: 若两个操作数相等则忽略该指令 + if (this->operand_[0] == this->operand_[1]) + return ""; + std::string riscv_instr = "\t\t"; + // li 指令 + if (this->operand_[1]->tid_ == RiscvOperand::IntImm) + riscv_instr += "LI\t"; + // 寄存器传寄存器 + else if (this->operand_[1]->tid_ == RiscvOperand::IntReg) + riscv_instr += "MV\t"; + // 浮点数 + else + riscv_instr += "FMV.S\t"; + if (this->operand_[0]->print() == this->operand_[1]->print()) + return ""; + riscv_instr += this->operand_[0]->print(); + riscv_instr += ", "; + riscv_instr += this->operand_[1]->print(); + riscv_instr += "\n"; + return riscv_instr; +} + +std::string SiToFpRiscvInstr::print() { + std::string riscv_instr = "\t\tFCVT.S.W\t"; + riscv_instr += this->operand_[1]->print(); + riscv_instr += ", "; + riscv_instr += this->operand_[0]->print(); + riscv_instr += "\n"; + return riscv_instr; +} + +std::string FpToSiRiscvInstr::print() { + std::string riscv_instr = "\t\tFCVT.W.S\t"; + riscv_instr += this->operand_[1]->print(); + riscv_instr += ", "; + riscv_instr += this->operand_[0]->print(); + riscv_instr += ", "; + riscv_instr += "rtz"; // round to zero. + riscv_instr += "\n"; + return riscv_instr; +} + +std::string LoadAddressRiscvInstr::print() { + std::string riscv_instr = + "\t\tLA\t" + this->operand_[0]->print() + ", " + this->name_ + "\n"; + return riscv_instr; +} + +std::string BranchRiscvInstr::print() { + std::string riscv_instr = "\t\t"; + // If single label operand then force jump + if (operand_[0] != nullptr) { + riscv_instr += "BGTZ\t"; + riscv_instr += operand_[0]->print(); + riscv_instr += ", "; + riscv_instr += static_cast(operand_[1])->name_; + riscv_instr += "\n\t\t"; + } + riscv_instr += "J\t"; + riscv_instr += static_cast(operand_[2])->name_; + riscv_instr += "\n"; + return riscv_instr; +} diff --git a/compiler/src/riscv/instruction.h b/compiler/src/riscv/instruction.h new file mode 100644 index 0000000..b57a282 --- /dev/null +++ b/compiler/src/riscv/instruction.h @@ -0,0 +1,431 @@ +#ifndef INSTRUCTIONH +#define INSTRUCTIONH + +#include "ir.h" +#include "riscv.h" +#include "regalloc.h" + +// 语句块,也使用标号标识 +// 必须挂靠在函数下,否则无法正常生成标号 +// 可以考虑转化到riscv basic block做数据流分析,预留接口 +class RiscvBasicBlock : public RiscvLabel { +public: + RiscvFunction *func_; + std::vector instruction; + int blockInd_; // 表示了各个block之间的顺序 + RiscvBasicBlock(std::string name, RiscvFunction *func, int blockInd) + : RiscvLabel(Block, name), func_(func), blockInd_(blockInd) { + func->addBlock(this); + } + RiscvBasicBlock(std::string name, int blockInd) + : RiscvLabel(Block, name), func_(nullptr), blockInd_(blockInd) {} + void addFunction(RiscvFunction *func) { func->addBlock(this); } + std::string printname() { return name_; } + // void addOutBlock(RiscvBasicBlock *bb) { inB.push_back(bb); } + // void addInBlock(RiscvBasicBlock *bb) { outB.push_back(bb); } + void deleteInstr(RiscvInstr *instr) { + auto it = std::find(instruction.begin(), instruction.end(), instr); + if (it != instruction.end()) + instruction.erase( + std::remove(instruction.begin(), instruction.end(), instr), + instruction.end()); + } + void replaceInstr(RiscvInstr *oldinst, RiscvInstr *newinst) {} + // 在全部指令后面加入 + void addInstrBack(RiscvInstr *instr) { + if (instr == nullptr) + return; + instruction.push_back(instr); + } + // 在全部指令之前加入 + void addInstrFront(RiscvInstr *instr) { + if (instr == nullptr) + return; + instruction.insert(instruction.begin(), instr); + } + void addInstrBefore(RiscvInstr *instr, RiscvInstr *dst) { + if (instr == nullptr) + return; + auto it = std::find(instruction.begin(), instruction.end(), dst); + if (it != instruction.end()) + instruction.insert(it, instr); + else + addInstrBack(instr); + } + void addInstrAfter(RiscvInstr *instr, RiscvInstr *dst) { + if (instr == nullptr) + return; + auto it = std::find(instruction.begin(), instruction.end(), dst); + if (it != instruction.end()) { + if (next(it) == instruction.end()) + instruction.push_back(instr); + else + instruction.insert(next(it), instr); + } else { + addInstrBack(instr); + } + } + std::string print(); +}; + +// 传入寄存器编号以生成一条语句, +// 上层由basic block整合 +class RiscvInstr { +public: + // 指令类型,除分支语句外,其他对应到riscv + // 加立即数或者浮点加需要做区分 + enum InstrType { + ADD = 0, + ADDI, + SUB, + SUBI, + MUL, + DIV = 6, + REM, + FADD = 8, + FSUB = 10, + FMUL = 12, + FDIV = 14, + XOR = 16, + XORI, + AND, + ANDI, + OR, + ORI, + SW, + LW, + FSW = 30, + FLW, + ICMP, + FCMP, + PUSH, + POP, + CALL, + RET, + LI, + MOV, + FMV, + FPTOSI, + SITOFP, + JMP, + SHL, + LSHR, + ASHR, + SHLI = 52, + LSHRI, + ASHRI, + LA, + ADDIW, + BGT + }; + const static std::map RiscvName; + + InstrType type_; + RiscvBasicBlock *parent_; + std::vector operand_; + + RiscvInstr(InstrType type, int op_nums); + RiscvInstr(InstrType type, int op_nums, RiscvBasicBlock *bb); + ~RiscvInstr() = default; + + virtual std::string print() = 0; + + RiscvOperand *result_; + void setOperand(int ind, RiscvOperand *val) { + assert(ind >= 0 && ind < operand_.size()); + operand_[ind] = val; + } + void setResult(RiscvOperand *result) { result_ = result; } + void removeResult() { result_ = nullptr; } + // 消除一个操作数 + void removeOperand(int i) { operand_[i] = nullptr; } + // 清除指令 + void clear() { operand_.clear(), removeResult(); } + // 先操作数后返回值 + std::vector getOperandResult() { + std::vector ans(operand_); + if (result_ != nullptr) + ans.push_back(result_); + return ans; + } + RiscvOperand *getOperand(int i) const { return operand_[i]; } +}; + +// 二元指令 +class BinaryRiscvInst : public RiscvInstr { +public: + std::string print() override; + BinaryRiscvInst() = default; + // target = v1 op v2,后面接一个flag参数表示要不要加入到对应的basic block中 + BinaryRiscvInst(InstrType op, RiscvOperand *v1, RiscvOperand *v2, + RiscvOperand *target, RiscvBasicBlock *bb, bool flag = 0) + : RiscvInstr(op, 2, bb), word(flag) { + setOperand(0, v1); + setOperand(1, v2); + setResult(target); + this->parent_ = bb; + // Optimize: 若立即数为0,则改用寄存器zero。 + if(v2->getType() == v2->IntImm && static_cast(v2)->intval == 0){ + type_ = ADD; + setOperand(1, getRegOperand("zero")); + } + } + bool word; +}; + +// 一元指令 +class UnaryRiscvInst : public RiscvInstr { +public: + std::string print() override; + UnaryRiscvInst() = default; + // target = op v1,后面接一个flag参数表示要不要加入到对应的basic block中 + UnaryRiscvInst(InstrType op, RiscvOperand *v1, RiscvOperand *target, + RiscvBasicBlock *bb, bool flag = 0) + : RiscvInstr(op, 1, bb) { + setOperand(0, v1); + setResult(target); + this->parent_ = bb; + if (flag) + this->parent_->addInstrBack(this); + } +}; + +// 加入一个整型mov指令(LI) +class MoveRiscvInst : public RiscvInstr { +public: + std::string print() override; + MoveRiscvInst() = default; + MoveRiscvInst(RiscvOperand *v1, int Imm, RiscvBasicBlock *bb, bool flag = 0) + : RiscvInstr(InstrType::LI, 2, bb) { + RiscvOperand *Const = new RiscvConst(Imm); + setOperand(0, v1); + setOperand(1, Const); + this->parent_ = bb; + if (flag) + this->parent_->addInstrBack(this); + } + // v2->v1 + MoveRiscvInst(RiscvOperand *v1, RiscvOperand *v2, RiscvBasicBlock *bb, + bool flag = 0) + : RiscvInstr(InstrType::MOV, 2, bb) { + setOperand(0, v1); + setOperand(1, v2); + this->parent_ = bb; + if (flag) + this->parent_->addInstrBack(this); + } +}; + +// 注意压栈顺序问题!打印的时候严格按照lists内顺序 +class PushRiscvInst : public RiscvInstr { + int basicShift_; + +public: + PushRiscvInst(std::vector &lists, RiscvBasicBlock *bb, + int basicShift) + : RiscvInstr(InstrType::PUSH, lists.size(), bb), basicShift_(basicShift) { + for (int i = 0; i < lists.size(); i++) + setOperand(i, lists[i]); + } + std::string print() override; +}; + +// 打印的时候严格按照lists内顺序 +class PopRiscvInst : public RiscvInstr { + int basicShift_; + +public: + // 传入所有要pop的变量 + PopRiscvInst(std::vector &lists, RiscvBasicBlock *bb, + int basicShift) + : RiscvInstr(InstrType::POP, lists.size(), bb), basicShift_(basicShift) { + for (int i = 0; i < lists.size(); i++) + setOperand(i, lists[i]); + } + std::string print() override; +}; + +// call调用语句+压栈语句 +// 0作为函数名,1-n是函数各参数 +class CallRiscvInst : public RiscvInstr { +public: + CallRiscvInst(RiscvFunction *func, RiscvBasicBlock *bb) + : RiscvInstr(InstrType::CALL, 1, bb) { + setOperand(0, func); + } + virtual std::string print() override; +}; + +// 仅返回语句,返回参数由上层的block对应的function构造push语句和lw sw指令 +class ReturnRiscvInst : public RiscvInstr { +public: + ReturnRiscvInst(RiscvBasicBlock *bb) : RiscvInstr(InstrType::RET, 0, bb) {} + std::string print() override; +}; + +// Store 指令格式:sw source_value(reg), shift(base reg) +// 目的:source_value->M[base reg + shift] +// 传入源寄存器,目的寄存器和偏移地址(默认为0) +// 如果是直接寻址,则base填x0号寄存器 +class StoreRiscvInst : public RiscvInstr { + +public: + int shift_; // 地址偏移量 + Type type; // 到底是浮点还是整型 + StoreRiscvInst(Type *ty, RiscvOperand *source, RiscvOperand *target, + RiscvBasicBlock *bb, int shift = 0) + : RiscvInstr(InstrType::SW, 2, bb), shift_(shift), type(ty->tid_) { + setOperand(0, source); + setOperand(1, target); + this->parent_ = bb; + if (source->isRegister() == false) { + std::cerr << "[Fatal error] Invalid store instruction: " << print() + << std::endl; + std::terminate(); + } + } + std::string print() override; +}; + +// 指令传入格式同store +// 先dest 后base reg +// 目的:M[base reg + shift]->dest reg +// 需指明是浮点还是整型 +class LoadRiscvInst : public RiscvInstr { +public: + int shift_; // 地址偏移量 + Type type; // 到底是浮点还是整型 + LoadRiscvInst(Type *ty, RiscvOperand *dest, RiscvOperand *target, + RiscvBasicBlock *bb, int shift = 0) + : RiscvInstr(InstrType::LW, 2, bb), shift_(shift), type(ty->tid_) { + setOperand(0, dest); + setOperand(1, target); + if (target == nullptr) { + std::cerr << "[Fatal Error] Load Instruction's target is nullptr." + << std::endl; + std::terminate(); + } + this->parent_ = bb; + } + std::string print() override; +}; + +// 整型比较 +// 类型:cmpop val1, val2, true_link +// 传入参数:val1, val2, true_link, false_link(basic block指针形式) +// false_link如果为为下一条语句则不会发射j false_link指令 +class ICmpRiscvInstr : public RiscvInstr { +public: + static const std::map ICmpOpName; + static const std::map ICmpOpSName; + static const std::map ICmpOpEquiv; + ICmpRiscvInstr(ICmpInst::ICmpOp op, RiscvOperand *v1, RiscvOperand *v2, + RiscvBasicBlock *trueLink, RiscvBasicBlock *falseLink, + RiscvBasicBlock *bb) + : RiscvInstr(ICMP, 4, bb), icmp_op_(op) { + setOperand(0, v1); + setOperand(1, v2); + setOperand(2, trueLink); + setOperand(3, falseLink); + } + ICmpRiscvInstr(ICmpInst::ICmpOp op, RiscvOperand *v1, RiscvOperand *v2, + RiscvBasicBlock *trueLink, RiscvBasicBlock *bb) + : RiscvInstr(ICMP, 4, bb), icmp_op_(op) { + setOperand(0, v1); + setOperand(1, v2); + setOperand(2, trueLink); + setOperand(3, nullptr); + } + ICmpInst::ICmpOp icmp_op_; + std::string print() override; +}; + +class ICmpSRiscvInstr : public ICmpRiscvInstr { +public: + ICmpSRiscvInstr(ICmpInst::ICmpOp op, RiscvOperand *v1, RiscvOperand *v2, + RiscvOperand *target, RiscvBasicBlock *bb) + : ICmpRiscvInstr(op, v1, v2, nullptr, bb) { + setOperand(0, v1); + setOperand(1, v2); + setResult(target); + } + std::string print() override; +}; + + +// 浮点比较 +// 类型:cmpop val1, val2, true_link, false_link +// 假定basic block是顺序排布的,那么如果false_link恰好为下一个basic +// block,则不会发射j false_link指令 +class FCmpRiscvInstr : public RiscvInstr { +public: + static const std::map FCmpOpName; + static const std::map FCmpOpEquiv; + FCmpRiscvInstr(FCmpInst::FCmpOp op, RiscvOperand *v1, RiscvOperand *v2, + RiscvOperand *target, RiscvBasicBlock *bb) + : RiscvInstr(FCMP, 2, bb), fcmp_op_(op) { + setOperand(0, v1); + setOperand(1, v2); + setResult(target); + } + FCmpInst::FCmpOp fcmp_op_; + std::string print() override; +}; +class FpToSiRiscvInstr : public RiscvInstr { +public: + FpToSiRiscvInstr(RiscvOperand *Source, RiscvOperand *Target, + RiscvBasicBlock *bb) + : RiscvInstr(FPTOSI, 2, bb) { + setOperand(0, Source); + setOperand(1, Target); + } + virtual std::string print() override; +}; + +class SiToFpRiscvInstr : public RiscvInstr { +public: + SiToFpRiscvInstr(RiscvOperand *Source, RiscvOperand *Target, + RiscvBasicBlock *bb) + : RiscvInstr(SITOFP, 2, bb) { + setOperand(0, Source); + setOperand(1, Target); + } + virtual std::string print() override; +}; + +// LA rd, symbol ; x[rd] = &symbol +// `dest` : rd +// `name` : symbol +class LoadAddressRiscvInstr : public RiscvInstr { +public: + std::string name_; + LoadAddressRiscvInstr(RiscvOperand *dest, std::string name, + RiscvBasicBlock *bb) + : RiscvInstr(LA, 1, bb), name_(name) { + setOperand(0, dest); + } + virtual std::string print() override; +}; + +/** + * 分支指令类。 + * BEQ rs1, zero, label1 + * J label2 + */ +class BranchRiscvInstr : public RiscvInstr { +public: + + /// @brief 生成分支指令类。 + /// @param rs1 存储布尔值的寄存器 + /// @param trueLink 真值跳转基本块 + /// @param falseLink 假值跳转基本块 + BranchRiscvInstr(RiscvOperand *rs1, RiscvBasicBlock *trueLink, + RiscvBasicBlock *falseLink, RiscvBasicBlock *bb) + : RiscvInstr(BGT, 3, bb) { + setOperand(0, rs1); + setOperand(1, trueLink); + setOperand(2, falseLink); + } + virtual std::string print() override; +}; +#endif // !INSTRUCTIONH diff --git a/compiler/src/riscv/optimize.cpp b/compiler/src/riscv/optimize.cpp new file mode 100644 index 0000000..7a1b6d7 --- /dev/null +++ b/compiler/src/riscv/optimize.cpp @@ -0,0 +1,3 @@ +#include "optimize.h" + +void OptimizeBlock() {} \ No newline at end of file diff --git a/compiler/src/riscv/optimize.h b/compiler/src/riscv/optimize.h new file mode 100644 index 0000000..64fb6bd --- /dev/null +++ b/compiler/src/riscv/optimize.h @@ -0,0 +1,11 @@ +#ifndef OPTIMIZEH +#define OPTIMIZEH + +#include "riscv.h" +#include "ir.h" + +// 进行数据流的优化 +// 在此之前先分配各寄存器 +// 可选 +void OptimizeBlock(); +#endif // !OPTIMIZEH \ No newline at end of file diff --git a/compiler/src/riscv/regalloc.cpp b/compiler/src/riscv/regalloc.cpp new file mode 100644 index 0000000..140a785 --- /dev/null +++ b/compiler/src/riscv/regalloc.cpp @@ -0,0 +1,380 @@ +#include "regalloc.h" +#include "instruction.h" +#include "riscv.h" + +int IntRegID = 32, FloatRegID = 32; // 测试阶段使用 + +Register *NamefindReg(std::string reg) { + if (reg.size() > 4) + return nullptr; + + Register *reg_to_ret = new Register(Register::Int, 0); + + // Check if int registers + for (int i = 0; i < 32; i++) { + reg_to_ret->rid_ = i; + if (reg_to_ret->print() == reg) + return reg_to_ret; + } + + // Else then float registers + reg_to_ret->regtype_ = reg_to_ret->Float; + for (int i = 0; i < 32; i++) { + reg_to_ret->rid_ = i; + if (reg_to_ret->print() == reg) + return reg_to_ret; + } + + return nullptr; +} + +RiscvOperand *getRegOperand(std::string reg) { + for (auto regope : regPool) { + if (regope->print() == reg) + return regope; + } + assert(false); + return nullptr; +} + +RiscvOperand *getRegOperand(Register::RegType op_ty_, int id) { + Register *reg = new Register(op_ty_, id); + for (auto regope : regPool) { + if (regope->print() == reg->print()) { + delete reg; + return regope; + } + } + assert(false); + return nullptr; +} + +Type *getStoreTypeFromRegType(RiscvOperand *riscvReg) { + return riscvReg->getType() == RiscvOperand::OpTy::FloatReg + ? new Type(Type::TypeID::FloatTyID) + : new Type(Type::TypeID::IntegerTyID); +} + +RiscvOperand *RegAlloca::findReg(Value *val, RiscvBasicBlock *bb, + RiscvInstr *instr, int inReg, int load, + RiscvOperand *specified, bool direct) { + safeFindTimeStamp++; + val = this->DSU_for_Variable.query(val); + bool isGVar = dynamic_cast(val) != nullptr; + bool isAlloca = dynamic_cast(val) != nullptr; + bool isPointer = val->type_->tid_ == val->type_->PointerTyID; + + // If there is no register allocated for value then get a new one + if (specified != nullptr) + setPositionReg(val, specified, bb, instr); + else if (curReg.find(val) == curReg.end() || isAlloca || + val->is_constant()) { // Alloca and constant value is always unsafe. + bool found = false; + RiscvOperand *cur = nullptr; + IntRegID = 32; + FloatRegID = 32; + while (!found) { + if (val->type_->tid_ != Type::FloatTyID) { + ++IntRegID; + if (IntRegID > 27) + IntRegID = 18; + + cur = getRegOperand(Register::Int, IntRegID); + } else { + ++FloatRegID; + if (FloatRegID > 27) + FloatRegID = 18; + cur = getRegOperand(Register::Float, FloatRegID); + } + if (regFindTimeStamp.find(cur) == regFindTimeStamp.end() || + safeFindTimeStamp - regFindTimeStamp[cur] > SAFE_FIND_LIMIT) { + setPositionReg(val, cur, bb, instr); + found = true; + } + } + } else { + regFindTimeStamp[curReg[val]] = safeFindTimeStamp; + return curReg[val]; + } + + // ! Though all registers are considered unsafe, there is no way + // ! to writeback registers properly in findReg() for now. + // ! Therefore unsafe part below is not being executed for now. + // ! Maybe should consider using writeback() instead. + // For now, all registers are considered unsafe thus registers should always + // load from memory before using and save to memory after using. + auto mem_addr = findMem(val, bb, instr, 1); // Value's direct memory address + auto current_reg = curReg[val]; // Value's current register + auto load_type = val->type_; + + regFindTimeStamp[current_reg] = safeFindTimeStamp; // Update time stamp + if (load) { + // Load before usage. + if (mem_addr != nullptr) { + bb->addInstrBefore( + new LoadRiscvInst(load_type, current_reg, mem_addr, bb), instr); + } else if (val->is_constant()) { + // If value is a int constant, create a LI instruction. + auto cval = dynamic_cast(val); + if (cval != nullptr) + bb->addInstrBefore(new MoveRiscvInst(current_reg, cval->value_, bb), + instr); + else if (dynamic_cast(val) != nullptr) + bb->addInstrBefore( + new MoveRiscvInst(current_reg, this->findMem(val), bb), instr); + else { + std::cerr << "[Warning] Trying to find a register for unknown type of " + "constant value which is not implemented for now." + << std::endl; + } + } else if (isAlloca) { + bb->addInstrBefore( + new BinaryRiscvInst( + BinaryRiscvInst::ADDI, getRegOperand("fp"), + new RiscvConst(static_cast(pos[val])->shift_), + current_reg, bb), + instr); + // std::cerr << "[Debug] Get a alloca position <" << val->print() << ", " + // << static_cast(pos[val])->print() + // << "> into the register <" << current_reg->print() << ">" + // << std::endl; + } else { + std::cerr << "[Error] Unknown error in findReg()." << std::endl; + std::terminate(); + } + } + + return current_reg; +} + +RiscvOperand *RegAlloca::findMem(Value *val, RiscvBasicBlock *bb, + RiscvInstr *instr, bool direct) { + val = this->DSU_for_Variable.query(val); + if (pos.count(val) == 0 && !val->is_constant()) { + std::cerr << "[Warning] Value " << std::hex << val << " (" << val->name_ + << ")'s memory map not found." << std::endl; + } + bool isGVar = dynamic_cast(val) != nullptr; + bool isPointer = val->type_->tid_ == val->type_->PointerTyID; + bool isAlloca = dynamic_cast(val) != nullptr; + // All float constant considered as global variables for now. + isGVar = isGVar || dynamic_cast(val) != nullptr; + // Always loading global variable's address into t5 when execute findMem(). + if (isGVar) { + if (bb == nullptr) { + std::cerr << "[Warning] Trying to add global var addressing " + "instruction, but basic block pointer is null." + << std::endl; + return nullptr; + } + bb->addInstrBefore( + new LoadAddressRiscvInstr(getRegOperand("t5"), pos[val]->print(), bb), + instr); + return new RiscvIntPhiReg("t5"); + } + // If not loading pointer's address directly, then use indirect addressing. + // Ignore alloca due to the instruction only being dealt by findReg() + if (isPointer && !isAlloca && !direct) { + if (bb == nullptr) { + std::cerr << "[Warning] Trying to add indirect pointer addressing " + "instruction, but basic block pointer is null." + << std::endl; + return nullptr; + } + + bb->addInstrBefore(new LoadRiscvInst(new Type(Type::PointerTyID), + getRegOperand("t4"), pos[val], bb), + instr); + return new RiscvIntPhiReg("t4"); + } + // Cannot access to alloca's memory directly. + else if (direct && isAlloca) + return nullptr; + + if (pos.find(val) == pos.end()) + return nullptr; + + return pos[val]; +} + +RiscvOperand *RegAlloca::findMem(Value *val) { + return findMem(val, nullptr, nullptr, true); +} + +RiscvOperand *RegAlloca::findNonuse(Type *ty, RiscvBasicBlock *bb, + RiscvInstr *instr) { + if (ty->tid_ == Type::IntegerTyID || ty->tid_ == Type::PointerTyID) { + ++IntRegID; + if (IntRegID > 27) + IntRegID = 18; + return getRegOperand(Register::Int, IntRegID); + } else { + ++FloatRegID; + if (FloatRegID > 27) + FloatRegID = 18; + return getRegOperand(Register::Float, FloatRegID); + } +} + +void RegAlloca::setPosition(Value *val, RiscvOperand *riscvVal) { + val = this->DSU_for_Variable.query(val); + if (pos.find(val) != pos.end()) { + // std::cerr << "[Warning] Trying overwriting memory address map of value " + // << std::hex << val << " (" << val->name_ << ") [" + // << riscvVal->print() << " -> " << pos[val]->print() << "]" + // << std::endl; + // std::terminate(); + } + + // std::cerr << "[Debug] [RegAlloca] Map value <" << val->print() + // << "> to operand <" << riscvVal->print() << ">" << std::endl; + + pos[val] = riscvVal; +} + +RiscvOperand *RegAlloca::findSpecificReg(Value *val, std::string RegName, + RiscvBasicBlock *bb, RiscvInstr *instr, + bool direct) { + val = this->DSU_for_Variable.query(val); + RiscvOperand *retOperand = getRegOperand(RegName); + + return findReg(val, bb, instr, 0, 1, retOperand, direct); +} + +void RegAlloca::setPositionReg(Value *val, RiscvOperand *riscvReg, + RiscvBasicBlock *bb, RiscvInstr *instr) { + val = this->DSU_for_Variable.query(val); + Value *old_val = getRegPosition(riscvReg); + RiscvOperand *old_reg = getPositionReg(val); + if (old_val != nullptr && old_val != val) + writeback(riscvReg, bb, instr); + if (old_reg != nullptr && old_reg != riscvReg) + writeback(old_reg, bb, instr); + setPositionReg(val, riscvReg); +} + +void RegAlloca::setPositionReg(Value *val, RiscvOperand *riscvReg) { + val = this->DSU_for_Variable.query(val); + if (riscvReg->isRegister() == false) { + std::cerr << "[Fatal error] Trying to map value " << std::hex << val + << " to not a register operand." << std::endl; + std::terminate(); + } + + // std::cerr << "[Debug] Map register <" << riscvReg->print() << "> to value <" + // << val->print() << ">\n"; + + curReg[val] = riscvReg; + regPos[riscvReg] = val; + regUsed.insert(riscvReg); +} + +RiscvInstr *RegAlloca::writeback(RiscvOperand *riscvReg, RiscvBasicBlock *bb, + RiscvInstr *instr) { + Value *value = getRegPosition(riscvReg); + if (value == nullptr) + return nullptr; // Value not found in map + + value = this->DSU_for_Variable.query(value); + + // std::cerr << "[Debug] [RegAlloca] Writeback register <" << riscvReg->print() + // << "> to value <" << value->print() << ">.\n"; + + // Erase map info + regPos.erase(riscvReg); + regFindTimeStamp.erase(riscvReg); + curReg.erase(value); + + RiscvOperand *mem_addr = findMem(value); + + if (mem_addr == nullptr) { + // std::cerr << "[Debug] [RegAlloca] Writeback ignore alloca pointer direct " + // "access and immvalue.\n"; + return nullptr; // Maybe an immediate value or dicrect accessing alloca + } + + auto store_type = value->type_; + auto store_instr = new StoreRiscvInst(value->type_, riscvReg, mem_addr, bb); + + // Write store instruction + if (instr != nullptr) + bb->addInstrBefore(store_instr, instr); + else + bb->addInstrBack(store_instr); + + return store_instr; +} + +RegAlloca::RegAlloca() { + // 初始化寄存器对象池。 + if (regPool.size() == 0) { + for (int i = 0; i < 32; i++) + regPool.push_back(new RiscvIntReg(new Register(Register::Int, i))); + for (int i = 0; i < 32; i++) + regPool.push_back(new RiscvFloatReg(new Register(Register::Float, i))); + } + + // fp 的保护单独进行处理 + regUsed.insert(getRegOperand("ra")); + savedRegister.push_back(getRegOperand("ra")); // 保护 ra + // 保护 s1-s11 + for (int i = 1; i <= 11; i++) + savedRegister.push_back(getRegOperand("s" + std::to_string(i))); + // 保护 fs0-fs11 + for (int i = 0; i <= 11; i++) + savedRegister.push_back(getRegOperand("fs" + std::to_string(i))); +} + +RiscvInstr *RegAlloca::writeback(Value *val, RiscvBasicBlock *bb, + RiscvInstr *instr) { + auto reg = getPositionReg(val); + return writeback(reg, bb, instr); +} + +Value *RegAlloca::getRegPosition(RiscvOperand *reg) { + if (regPos.find(reg) == regPos.end()) + return nullptr; + return this->DSU_for_Variable.query(regPos[reg]); +} + +RiscvOperand *RegAlloca::getPositionReg(Value *val) { + val = this->DSU_for_Variable.query(val); + if (curReg.find(val) == curReg.end()) + return nullptr; + return curReg[val]; +} + +RiscvOperand *RegAlloca::findPtr(Value *val, RiscvBasicBlock *bb, + RiscvInstr *instr) { + val = this->DSU_for_Variable.query(val); + if (ptrPos.find(val) == ptrPos.end()) { + std::cerr << "[Fatal Error] Value's pointer position not found." + << std::endl; + std::terminate(); + } + return ptrPos[val]; +} + +void RegAlloca::writeback_all(RiscvBasicBlock *bb, RiscvInstr *instr) { + std::vector regs_to_writeback; + for (auto p : regPos) + regs_to_writeback.push_back(p.first); + for (auto r : regs_to_writeback) + writeback(r, bb, instr); +} + +void RegAlloca::setPointerPos(Value *val, RiscvOperand *PointerMem) { + val = this->DSU_for_Variable.query(val); + assert(val->type_->tid_ == Type::TypeID::PointerTyID || + val->type_->tid_ == Type::TypeID::ArrayTyID); + // std::cerr << "SET POINTER: " << val->name_ << "!" << PointerMem->print() + // << "\n"; + this->ptrPos[val] = PointerMem; +} + +void RegAlloca::clear() { + curReg.clear(); + regPos.clear(); + safeFindTimeStamp = 0; + regFindTimeStamp.clear(); +} \ No newline at end of file diff --git a/compiler/src/riscv/regalloc.h b/compiler/src/riscv/regalloc.h new file mode 100644 index 0000000..dbbb44b --- /dev/null +++ b/compiler/src/riscv/regalloc.h @@ -0,0 +1,295 @@ +#ifndef REGALLOCH +#define REGALLOCH + +#include "riscv.h" +#include + +template class DSU { +private: + std::map father; + T getfather(T x) { + return father[x] == x ? x : (father[x] = getfather(father[x])); + } + +public: + DSU() = default; + T query(T id) { + // 不存在变量初值为自己 + if (father.find(id) == father.end()) { + // std::cerr << std::hex << "[Debug] [DSU] [" << this << "] New value " << + // id + // << std::endl; + return father[id] = id; + } else { + // std::cerr << std::hex << "[Debug] [DSU] [" << this << "] Find value " + // << id + // << std::endl; + return getfather(id); + } + } + + /** + * Merge DSU's node u to v. + * @param u child + * @param v father + */ + void merge(T u, T v) { + u = query(u), v = query(v); + assert(u != nullptr && v != nullptr); + if (u == v) + return; + // std::cerr << std::hex << "[Debug] [DSU] [" << this << "] Merge " << u + // << " to " << v << std::endl; + father[u] = v; + } +}; + +// 关于额外发射指令问题说明 +// 举例:如果当前需要使用特定寄存器(以a0为例)以存取返回值 +// 1. 如果当前变量在内存:a) +// a) +// 由regalloca发射一个sw指令(使用rbb中addInstrback函数)将现在的a0送回对应内存或栈上地址 +// b) 由regalloca发射一个lw指令(使用rbb中addInstrback函数)将该变量移入a0中 +// 2. 如果该变量在寄存器x中: +// a) +// 由regalloca发射一个sw指令(使用rbb中addInstrback函数)将现在的a0送回对应内存或栈上地址 +// b) 由regalloca发射一个mv指令(使用rbb中addInstrback函数)将该变量从x移入a0中 + +// 举例:为当前一个未指定寄存器,或当前寄存器堆中没有存放该变量的寄存器。现在需要为该变量找一个寄存器以进行运算 +// 存在空寄存器:找一个空闲未分配寄存器,然后返回一个寄存器指针riscvOperand* +// 不存在空寄存器: +// a) 找一个寄存器 +// b) +// 由regalloca发射一个sw指令(使用rbb中addInstrback函数)将现在该寄存器的数字送回对应内存或栈上地址 +// c) 返回该寄存器指针riscvOperand* + +// 注意区分指针类型(如*a0)和算数值(a0)的区别 + +// 每个变量会有一个固定的内存或栈地址,可能会被分配一个固定寄存器地址 + +extern int IntRegID, FloatRegID; // 测试阶段使用 + +Register *NamefindReg(std::string reg); + +// 辅助函数 +// 根据寄存器 riscvReg 的类型返回存储指令的类型 +Type *getStoreTypeFromRegType(RiscvOperand *riscvReg); + +// RegAlloca类被放置在**每个函数**内,每个函数内是一个新的寄存器分配类。 +// 因而约定x8-x9 x18-27、f8-9、f18-27 +// 是约定的所有函数都要保护的寄存器,用完要恢复原值 +// 其他的寄存器(除函数参数所用的a0-a7等寄存器)都视为是不安全的,可能会在之后的运算中发生变化 +// 在该类的实例生存周期内,使用到的需要保护的寄存器使用一个vector +// 存储 + +// 寄存器分配(IR变量到汇编变量地址映射) +// 所有的临时变量均分配在栈上(从当前函数开始的地方开始计算栈地址,相对栈偏移地址),所有的全局变量放置在内存中(首地址+偏移量形式) +// 当存在需要寄存器保护的时候,直接找回原地址去进行 +class RegAlloca { +public: + DSU DSU_for_Variable; + + /** + * 返回 Value 所关联的寄存器操作数。若 Value + * 未关联寄存器,则分配寄存器并进行关联,并处理相关的数据载入与写回。 + * @param val 需要查找寄存器的 Value + * @param bb 插入指令需要提供的基本块 + * @param instr 在 instr 之前插入指令(可选) + * @param inReg 是否一定返回一个寄存器操作数,若为 true 则是 + * @param load 是否将 Value 所对应的物理地址内容载入到寄存器中,若为 true 则是 + * @param specified 是否指定关联的寄存器,若为 nullptr + * 则不指定,若为一个寄存器操作数则在任何情况下都将 Value + * 与指定的寄存器进行强制关联。 + * @param direct 仅在 Value 为指针时生效,用于在载入物理地址时传递参数给 + * findMem + * @return 返回一个 IntegerReg* 或 FloatReg* 类型的操作数 rs 。 + * @attention 该函数将在 Value 为 Alloca 指令时进行特殊处理,在 load=1 时将 + * Alloca 指针所指向的地址载入到分配的寄存器中。 + * @attention 该函数将在 Value 为常量时进行特殊处理,在 load=1 时将常量通过 LI + * 指令载入到分配的寄存器中。 + * @attention 目前不应使用 direct 参数。 + */ + RiscvOperand *findReg(Value *val, RiscvBasicBlock *bb, + RiscvInstr *instr = nullptr, int inReg = 0, + int load = 1, RiscvOperand *specified = nullptr, + bool direct = true); + + /** + * 对传递过来的 Value 返回其所处的物理地址操作数 offset(rs) 。 + * @param val 需要查找物理地址的 Value + * @param bb 插入指令需要提供的基本块 + * @param instr 在 instr 之前插入指令(可选) + * @param direct 当 direct 为 false 时将使用间接寻址。若使用间接寻址且 + * Value 为指针 (getElementInstr) ,则 findMem() + * 将会将指针所指向的地址载入临时寄存器 t5,并返回操作数 0(t5) 。 + * @return 返回一个 IntegerPhiReg* 或 FloatPhiReg* 类型的操作数 offset(rs) + * 。若操作数偏移量溢出,则将会计算返回操作数所指代的地址 (t5)。 + */ + RiscvOperand *findMem(Value *val, RiscvBasicBlock *bb, RiscvInstr *instr, + bool direct); + /** + * 对传递过来的 Value 返回其所处的物理地址操作数 offset(rs) 。 + * @attention 这是一个重载形式的函数,其不支持间接寻址。 + * @param val 需要查找物理地址的 Value + */ + RiscvOperand *findMem(Value *val); + + // 实现一个函数,以找到一个当前尚未使用的寄存器以存放某个值。 + RiscvOperand *findNonuse(Type *ty, RiscvBasicBlock *bb, + RiscvInstr *instr = nullptr); + + /** + * 将 Value 与指定的寄存器强制关联并返回寄存器操作数。 + * @param val 需要查找寄存器的 Value + * @param RegName 需要强制关联的寄存器接口名称 + * @param bb 插入指令需要提供的基本块 + * @param instr 在 instr 之前插入指令(可选) + * @param direct 仅在 Value 为指针时生效,用于在载入物理地址时传递参数给 + * findMem + * @return 返回强制关联的寄存器操作数 rs 。 + * @note 该函数将在 Value 为 Alloca 指令时进行特殊处理,在 load=1 时将 + * Alloca 指针所指向的地址载入到分配的寄存器中。 + * @note 该函数将在 Value 为常量时进行特殊处理,在 load=1 时将常量通过 LI + * 指令载入到分配的寄存器中。 + * @attention 目前不应使用 direct 参数。 + */ + RiscvOperand *findSpecificReg(Value *val, std::string RegName, + RiscvBasicBlock *bb, + RiscvInstr *instr = nullptr, + bool direct = true); + + /** + * 将 Value 与指定的物理地址操作数 offset(rs) 相关联。 + * @param val 需要关联的 Value + * @param riscvVal 被关联的物理地址操作数 offset(rs) + * @attention 该函数也被用于关联 Alloca 指针与其指向的地址。 + */ + void setPosition(Value *val, RiscvOperand *riscvVal); + + /** + * 将 Value 与指定的寄存器 rs 相关联。若寄存器内已有值,则将值写回。 + * @param val 需要关联的 Value + * @param riscvReg 被关联的寄存器 rs + * @param bb 用于插入的基本块 + * @param instr 在 instr 之前插入指令(可选) + * @attention 在大多数情况下你不应直接使用此函数。作为替代,你应该使用 findReg + * 与 findSpecificReg 函数来进行关联。 + */ + void setPositionReg(Value *val, RiscvOperand *riscvReg, RiscvBasicBlock *bb, + RiscvInstr *instr = nullptr); + /** + * 将 Value 与指定的寄存器 rs 相关联。若寄存器内已有值,则将值写回。 + * @param val 需要关联的 Value + * @param riscvReg 被关联的寄存器 rs + * @attention 在大多数情况下你不应直接使用此函数。作为替代,你应该使用 findReg + * 与 findSpecificReg 函数来进行关联。 + */ + void setPositionReg(Value *val, RiscvOperand *riscvReg); + + /** + * 记录 getElementPtr 类型的 Value 所指向的常量相对物理地址操作数 offset(sp) + * 。 + * @param val 指针类型的 Value + * @param PointerMem 被关联的操作数 offset(sp) + * @attention offset 只能为一常量。 + */ + void setPointerPos(Value *val, RiscvOperand *PointerMem); + + /** + * 将寄存器内容写回到内存地址中,并移除该寄存器在pos中映射的地址。 + * @param riscvReg 将要写回的寄存器 + * @param bb 指令将要插入的基本块 + * @param instr 当前需要在哪一条指令前方插入sw指令 + * @return 返回最后写入的指令 + */ + RiscvInstr *writeback(RiscvOperand *riscvReg, RiscvBasicBlock *bb, + RiscvInstr *instr = nullptr); + /** + * 将 Value 所关联的寄存器写回到内存地址中,并移除该寄存器在pos中映射的地址。 + * @param val 将要写回的 Value + * @param bb 指令将要插入的基本块 + * @param instr 当前需要在哪一条指令前方插入sw指令 + * @return 返回最后写入的指令 + */ + RiscvInstr *writeback(Value *val, RiscvBasicBlock *bb, + RiscvInstr *instr = nullptr); + + /** + * 返回寄存器 reg 所对应的 Value 。 + */ + Value *getRegPosition(RiscvOperand *reg); + + /** + * 返回 Value 所对应的寄存器 reg 。 + */ + RiscvOperand *getPositionReg(Value *val); + + /** + * 保护的寄存器对象数组。 + */ + std::vector savedRegister; + + /** + * 初始化寄存器对象池,需要保护的寄存器对象等。 + */ + RegAlloca(); + + // 指针所指向的内存地址 + std::map ptrPos; + + /** + * 返回指针类型的 Value 所指向的常量相对物理地址操作数 offset(sp) 。 + * @attention 参数 bb, instr 目前不被使用。 + */ + RiscvOperand *findPtr(Value *val, RiscvBasicBlock *bb, + RiscvInstr *instr = nullptr); + + /** + * 写回所有与内存存在关联的寄存器并删除关联。 + * @param bb 被插入基本块 + * @param instr 在特定指令前插入 + */ + void writeback_all(RiscvBasicBlock *bb, RiscvInstr *instr = nullptr); + + /** + * 清空所有寄存器关系。 + */ + void clear(); + + /** + * 返回所有被使用过的寄存器集合。 + */ + std::set getUsedReg() { return regUsed; } + +private: + std::map pos, curReg; + std::map regPos; + /** + * 安全寄存器寻找。用于确保寄存器在被寻找之后的 SAFE_FIND_LIMIT + * 个时间戳内不被写回。 + */ + std::map regFindTimeStamp; + int safeFindTimeStamp = 0; + static const int SAFE_FIND_LIMIT = 3; + /** + * 被使用过的寄存器。 + */ + std::set regUsed; +}; + +/** + * 寄存器对象池。 + */ +static std::vector regPool; + +/** + * 根据提供的寄存器名,从寄存器池中返回操作数。 + */ +RiscvOperand *getRegOperand(std::string reg); + +/** + * 根据提供的寄存器类型与编号,从寄存器池中返回操作数。 + */ +RiscvOperand *getRegOperand(Register::RegType op_ty_, int id); + +#endif // !REGALLOCH diff --git a/compiler/src/riscv/riscv.cpp b/compiler/src/riscv/riscv.cpp new file mode 100644 index 0000000..972b3c6 --- /dev/null +++ b/compiler/src/riscv/riscv.cpp @@ -0,0 +1,137 @@ +#include "riscv.h" +#include "backend.h" +#include "ir.h" + +const int REG_NUMBER = 32; + +RiscvFunction::RiscvFunction(std::string name, int num_args, + OpTy Ty) // 返回值,无返回使用void类型 + : RiscvLabel(Function, name), num_args_(num_args), resType_(Ty), + base_(-VARIABLE_ALIGN_BYTE) { + regAlloca = new RegAlloca(); +} +// 输出函数对应的全部riscv语句序列 +// 由于一个函数可能有若干个出口,因而恢复现场的语句根据basic block +// 语句中的ret语句前面附带出现,因而不在此出现 +std::string RiscvFunction::print() { + // TODO: temporaily add '.global' to declare function + // Don't know if '.type' is needed + std::string riscvInstr = + ".global " + this->name_ + "\n" + this->name_ + ":\n"; // 函数标号打印 + // 对各个basic block进行拼接 + for (auto x : this->blk) + riscvInstr += x->print(); + return riscvInstr; +} + +std::string RiscvBasicBlock::print() { + std::string riscvInstr = this->name_ + ":\n"; + for (auto x : this->instruction) + riscvInstr += x->print(); + return riscvInstr; +} + +// 出栈顺序和入栈相反 +// 建议不使用pop语句,直接从栈中取值,最后直接修改sp的值即可 +// 使用一个单独的return block以防止多出口return +extern int LabelCount; + +RiscvOperand::OpTy RiscvOperand::getType() { return tid_; } + +bool RiscvOperand::isRegister() { return tid_ == FloatReg || tid_ == IntReg; } + +Type *findPtrType(Type *ty) { + while (ty->tid_ == Type::PointerTyID) { + ty = static_cast(ty)->contained_; + } + while (ty->tid_ == Type::ArrayTyID) { + ty = static_cast(ty)->contained_; + } + assert(ty->tid_ == Type::IntegerTyID || ty->tid_ == Type::FloatTyID); + return ty; +} + +std::string RiscvGlobalVariable::print(bool print_name, Constant *initVal) { + std::string code = ""; + // 如果在调用的第一层,初始化 initVal + if (print_name) { + code += this->name_ + ":\n"; + initVal = initValue_; + } + + if (initVal == nullptr) + return "\t.zero\t" + std::to_string(this->elementNum_ * 4) + "\n";; + + // 如果无初始值,或初始值为0(IR中有ConstZero类),则直接用zero命令 + if (dynamic_cast(initVal) != nullptr) { + code += "\t.zero\t" + std::to_string(calcTypeSize(initVal->type_)) + "\n"; + return code; + } + // 下面是非零的处理 + // 整型 + if (initVal->type_->tid_ == Type::TypeID::IntegerTyID) { + code += "\t.word\t" + std::to_string(dynamic_cast(initVal)->value_) + "\n"; + return code; + } + // 浮点 + else if (initVal->type_->tid_ == Type::TypeID::FloatTyID) { + std::string valString = dynamic_cast(initVal)->print32(); + while (valString.length() < 10) + valString += "0"; + code += "\t.word\t" + valString.substr(0, 10) + "\n"; + return code; + } + else if (initVal->type_->tid_ == Type::TypeID::ArrayTyID) { + ConstantArray *const_arr = dynamic_cast(initVal); + assert(const_arr != nullptr); + int zeroSpace = calcTypeSize(initVal->type_); + for (auto elements : const_arr->const_array) { + code += print(false, elements); + zeroSpace -= 4; + } + if (zeroSpace) + code += "\t.zero\t" + std::to_string(zeroSpace) + "\n"; + return code; + } else { + std::cerr + << "[Fatal Error] Unknown RiscvGlobalVariable::print() initValue type." + << std::endl; + std::terminate(); + } +} + +std::string RiscvGlobalVariable::print() { return print(true, nullptr); } + +RiscvFunction *createSyslibFunc(Function *foo) { + if (foo->name_ == "__aeabi_memclr4") { + auto *rfoo = createRiscvFunction(foo); + // 预处理块 + auto *bb1 = createRiscvBasicBlock(); + bb1->addInstrBack(new MoveRiscvInst(getRegOperand("t5"), + getRegOperand("a0"), bb1)); + bb1->addInstrBack(new MoveRiscvInst(getRegOperand("t6"), + getRegOperand("a1"), bb1)); + bb1->addInstrBack(new BinaryRiscvInst(RiscvInstr::ADD, getRegOperand("a0"), + getRegOperand("t6"), + getRegOperand("t6"), bb1)); + bb1->addInstrBack( + new MoveRiscvInst(getRegOperand("a0"), new RiscvConst(0), bb1)); + auto *bb2 = createRiscvBasicBlock(); + // 循环块 + // 默认clear为全0 + bb2->addInstrBack(new StoreRiscvInst( + new Type(Type::TypeID::IntegerTyID), getRegOperand("zero"), + new RiscvIntPhiReg(NamefindReg("t5")), bb2)); + bb2->addInstrBack(new BinaryRiscvInst(RiscvInstr::ADDI, getRegOperand("t5"), + new RiscvConst(4), + getRegOperand("t5"), bb1)); + bb2->addInstrBack(new ICmpRiscvInstr(ICmpInst::ICMP_SLT, + getRegOperand("t5"), + getRegOperand("t6"), bb2, bb2)); + bb2->addInstrBack(new ReturnRiscvInst(bb2)); + rfoo->addBlock(bb1); + rfoo->addBlock(bb2); + return rfoo; + } + return nullptr; +} \ No newline at end of file diff --git a/compiler/src/riscv/riscv.h b/compiler/src/riscv/riscv.h new file mode 100644 index 0000000..a5e8d97 --- /dev/null +++ b/compiler/src/riscv/riscv.h @@ -0,0 +1,343 @@ +#ifndef RISCVH +#define RISCVH + +class RiscvLabel; +class RiscvBasicBlock; +class RiscvInstr; +class RegAlloca; +class Register; +class RiscvOperand; + +const int VARIABLE_ALIGN_BYTE = 8; + +#include "ir.h" +#include "string.h" + +class RiscvOperand { +public: + enum OpTy { + Void = 0, // 空类型,为无函数返回值专用的类型 + IntImm, // 整型立即数 + FloatImm, // 浮点立即数 + IntReg, // 数值直接保存在整型寄存器 + FloatReg, // 数值直接保存在浮点寄存器 + IntMem, // 整型M[R(rd)+shift],无寄存器可用x0,无偏移可用shift=0 + FloatMem, // 浮点,同上 + Function, // 调用函数 + Block // 基本语句块标号 + }; + OpTy tid_; + explicit RiscvOperand(OpTy tid) : tid_(tid) {} + ~RiscvOperand() = default; + virtual std::string print() = 0; + OpTy getType(); + + // If this operand is a register, return true. + bool isRegister(); +}; + +// 寄存器堆 +class Register { + +public: + Register() = default; + ~Register() = default; + enum RegType { + Int = 1, // 整型 + Float, // 浮点 + Stack, // 栈专用 + Zero // 零寄存器 + }; + RegType regtype_; + int rid_; // 寄存器编号 + Register(RegType regtype, int rid) : regtype_(regtype), rid_(rid) {} + std::string print() { + using std::to_string; + if (this->regtype_ == Float) { + if (this->rid_ <= 7) + return "ft" + to_string(rid_); + else if (this->rid_ <= 9) + return "fs" + to_string(rid_ - 8); + else if (this->rid_ <= 17) + return "fa" + to_string(rid_ - 10); + else if (this->rid_ <= 27) + return "fs" + to_string(rid_ - 18 + 2); + else if (this->rid_ <= 31) + return "ft" + to_string(rid_ - 28 + 8); + else + return "wtf"; + } + // 整型各类输出 + switch (this->rid_) { + case 0: + return "zero"; + case 1: + return "ra"; + case 2: + return "sp"; + case 3: + return "gp"; + case 4: + return "tp"; + case 5: + case 6: + case 7: + return "t" + to_string(this->rid_ - 5); + case 8: + return "fp"; // another name: s0 + case 9: + return "s1"; + } + if (this->rid_ >= 10 && this->rid_ <= 17) + return "a" + to_string(this->rid_ - 10); + if (this->rid_ >= 18 && this->rid_ <= 27) + return "s" + to_string(this->rid_ - 16); + return "t" + to_string(this->rid_ - 25); + } +}; + +extern const int REG_NUMBER; + +// 常数 +class RiscvConst : public RiscvOperand { + +public: + int intval; + float floatval; + RiscvConst() = default; + explicit RiscvConst(int val) : RiscvOperand(IntImm), intval(val) {} + explicit RiscvConst(float val) : RiscvOperand(FloatImm), floatval(val) {} + std::string print() { + if (this->tid_ == IntImm) + return std::to_string(intval); + else + return std::to_string(floatval); + } +}; + +// 整型寄存器直接存储 +class RiscvIntReg : public RiscvOperand { + +public: + Register *reg_; + RiscvIntReg(Register *reg) : RiscvOperand(IntReg), reg_(reg) { + assert(reg_->regtype_ == Register::Int); // 判断整型寄存器存储 + } + std::string print() { return reg_->print(); } +}; + +class RiscvFloatReg : public RiscvOperand { + +public: + Register *reg_; + RiscvFloatReg(Register *reg) : RiscvOperand(FloatReg), reg_(reg) { + assert(reg_->regtype_ == Register::Float); // 判断整型寄存器存储 + } + std::string print() { return reg_->print(); } +}; + +// 需间接寻址得到的数据,整型 +class RiscvIntPhiReg : public RiscvOperand { + +public: + int shift_; + int isGlobalVariable; + Register *base_; + std::string MemBaseName; + RiscvIntPhiReg(Register *base, int shift = 0, int isGVar = false) + : RiscvOperand(IntMem), base_(base), shift_(shift), + isGlobalVariable(isGVar), MemBaseName(base_->print()) {} + // 内存以全局形式存在的变量(常量) + RiscvIntPhiReg(std::string s, int shift = 0, int isGVar = false) + : RiscvOperand(IntMem), base_(nullptr), shift_(shift), MemBaseName(s), + isGlobalVariable(isGVar) {} + std::string print() { + std::string ans = ""; + if (base_ != nullptr) + ans += "(" + base_->print() + ")"; + else { + if (isGlobalVariable) + return MemBaseName; // If global variable, use direct addressing + else + ans += "(" + MemBaseName + ")"; + } + if (shift_) + ans = std::to_string(shift_) + ans; + return ans; + } + /** + * Return if shift value overflows. + */ + bool overflow() { return std::abs(shift_) >= 1024; } +}; + +// 需间接寻址得到的数据,浮点 +class RiscvFloatPhiReg : public RiscvOperand { + +public: + int shift_; + Register *base_; + std::string MemBaseName; + int isGlobalVariable; + RiscvFloatPhiReg(Register *base, int shift = 0, int isGVar = false) + : RiscvOperand(FloatMem), base_(base), shift_(shift), + isGlobalVariable(isGVar), MemBaseName(base_->print()) {} + // 内存以全局形式存在的变量(常量) + RiscvFloatPhiReg(std::string s, int shift = 0, int isGVar = false) + : RiscvOperand(FloatMem), base_(nullptr), shift_(shift), MemBaseName(s), + isGlobalVariable(isGVar) {} + std::string print() { + std::string ans = ""; + if (base_ != nullptr) + ans += "(" + base_->print() + ")"; + else { + if (isGlobalVariable) + return MemBaseName; // If global variable, use direct addressing + else + ans += "(" + MemBaseName + ")"; + } + if (shift_) + ans = std::to_string(shift_) + ans; + return ans; + } + /** + * Return if shift value overflows. + */ + bool overflow() { return std::abs(shift_) >= 1024; } +}; + +class RiscvLabel : public RiscvOperand { +public: + std::string name_; // 标号名称 + ~RiscvLabel() = default; + RiscvLabel(OpTy Type, std::string name) : RiscvOperand(Type), name_(name) { + // std::cout << "CREATE A LABEL:" << name << "\n"; + } + virtual std::string print() = 0; +}; + +// 全局变量 +// 需指明是浮点还是整型 +// 最后拼装 +class RiscvGlobalVariable : public RiscvLabel { +public: + bool isConst_; + bool isData; // 是否是给定初值的变量 + int elementNum_; + Constant *initValue_; + // 对于一般单个全局变量的定义 + RiscvGlobalVariable(OpTy Type, std::string name, bool isConst, + Constant *initValue) + : RiscvLabel(Type, name), isConst_(isConst), initValue_(initValue), + elementNum_(1) {} + // 对于数组全局变量的定义 + RiscvGlobalVariable(OpTy Type, std::string name, bool isConst, + Constant *initValue, int elementNum) + : RiscvLabel(Type, name), isConst_(isConst), initValue_(initValue), + elementNum_(elementNum) {} + // 输出全局变量定义 + // 根据ir中全局变量定义转化 + // 问题在于全局变量如果是数组有初值如何处理 + std::string print(); + std::string print(bool print_name, Constant *initVal); +}; + +// 用标号标识函数 +// 函数挂靠在module下,接入若干条instruction,以及function不设置module指针 +// 默认不保护现场,如果当寄存器不够的时候再临时压栈 +// 父函数调用子函数的参数算在子函数的栈空间内,子函数结束后由子函数清除这部分栈空间 +class RiscvFunction : public RiscvLabel { +public: + RegAlloca *regAlloca; + int num_args_; + OpTy resType_; + std::vector args; + RiscvFunction(std::string name, int num_args, + OpTy Ty); // 返回值,无返回使用void类型 + void setArgs(int ind, RiscvOperand *op) { + assert(ind >= 0 && ind < args.size()); + args[ind] = op; + } + void deleteArgs(int ind) { + assert(ind >= 0 && ind < args.size()); + args[ind] = nullptr; + } + ~RiscvFunction() = default; + std::string printname() { return name_; } + std::vector blk; + bool is_libfunc() { + if (name_ == "putint" || name_ == "putch" || name_ == "putarray" || + name_ == "_sysy_starttime" || name_ == "_sysy_stoptime" || + name_ == "__aeabi_memclr4" || name_ == "__aeabi_memset4" || + name_ == "__aeabi_memcpy4" || name_ == "getint" || name_ == "getch" || + name_ == "getarray" || name_ == "getfloat" || name_ == "getfarray" || + name_ == "putfloat" || name_ == "putfarray" || + name_ == "llvm.memset.p0.i32") { + return true; + } else + return false; + } + std::map + argsOffset; // 函数使用到的参数(含调用参数、局部变量和返回值)在栈中位置。需满足字对齐(4的倍数) + // 届时将根据该函数的参数情况决定sp下移距离 + void addArgs(RiscvOperand *val) { // 在栈上新增操作数映射 + if (argsOffset.count(val) == 0) { + argsOffset[val] = base_; + base_ -= VARIABLE_ALIGN_BYTE; + } + } + int querySP() { return base_; } + void setSP(int SP) { base_ = SP; } + void addTempVar(RiscvOperand *val) { + addArgs(val); + tempRange += VARIABLE_ALIGN_BYTE; + } + void shiftSP(int shift_value) { base_ += shift_value; } + void storeArray(int elementNum) { + if(elementNum & 7) { + elementNum += 8 - (elementNum & 7); // Align to 8 byte. + } + base_ -= elementNum; + } + void deleteArgs(RiscvOperand *val) { argsOffset.erase(val); } // 删除一个参数 + // 默认所有寄存器不保护 + // 如果这个时候寄存器不够了,则临时把其中一个寄存器对应的值压入栈上,等函数结束的时候再恢复 + // 仅考虑函数内部SP相对关系而不要计算其绝对关系 + void saveOperand(RiscvOperand *val) { + storedEnvironment[val] = base_; + argsOffset[val] = base_; + base_ -= VARIABLE_ALIGN_BYTE; + } + int findArgs(RiscvOperand *val) { // 查询栈上位置 + if (argsOffset.count(val) == 0) + addArgs(val); + return argsOffset[val]; + } + void ChangeBlock(RiscvBasicBlock *bb, int ind) { + assert(ind >= 0 && ind < blk.size()); + blk[ind] = bb; + } + void addBlock(RiscvBasicBlock *bb) { blk.push_back(bb); } + std::string + print(); // 函数语句,需先push保护现场,然后pop出需要的参数,再接入各block +private: + int base_; + int tempRange; // 局部变量的数量,需要根据这个数量进行栈帧下移操作 + std::map + storedEnvironment; // 栈中要保护的地址。该部分需要在函数结束的时候全部恢复 +}; + +class RiscvModule { +public: + std::vector func_; + std::vector globalVariable_; + void addFunction(RiscvFunction *foo) { func_.push_back(foo); } + void addGlobalVariable(RiscvGlobalVariable *g) { + globalVariable_.push_back(g); + } +}; + +Type *findPtrType(Type *ty); + +RiscvFunction *createSyslibFunc(Function *foo); +#endif // !RISCVH \ No newline at end of file