Commit 4a232c57 authored by lxq's avatar lxq

modified project structure

parent 78a60f29
......@@ -49,5 +49,6 @@ include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_BINARY_DIR})
include_directories(include/lightir)
include_directories(include/optimization)
include_directories(include/codegen)
add_subdirectory(src)
add_subdirectory(tests)
......@@ -2,13 +2,17 @@
#define CODEGEN_HPP
#include "BasicBlock.h"
#include "Constant.h"
#include "Function.h"
#include "IRprinter.h"
#include "Instruction.h"
#include "Module.h"
#include "Value.h"
#include "liverange.hpp"
#include "logging.hpp"
#define __RO_PART__
#include <map>
#include <ostream>
#include <vector>
......@@ -23,7 +27,7 @@ using std::vector;
class CodeGen {
public:
CodeGen(Module *module) : m(module) {}
CodeGen(Module *m_) : m(m_), LRA(m_, phi_map) {}
string print() {
string result;
......@@ -143,12 +147,16 @@ class CodeGen {
std::map<Function *, std::map<int, int>> func_arg_off; // to $sp
std::map<Function *, int> func_arg_N; // total space for args
std::map<BasicBlock *, std::vector<std::pair<Value *, Value *>>> phi_map;
std::map<Constant *, std::string> ROdata;
unsigned int stackN; // function local vars and so on
Function *cur_func;
Module *m;
vector<string> output;
// register allocation
LiveRangeAnalyzer LRA;
};
#endif
#ifndef LIVERANGE_HPP
#define LIVERANGE_HPP
#include "BasicBlock.h"
#include "Function.h"
#include "Instruction.h"
#include "Module.h"
#include "Value.h"
#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <string>
using std::map;
using std::pair;
using std::set;
using std::string;
using std::vector;
#define __LRA_PRINT__
class LiveRangeAnalyzer {
friend class CodeGen;
using LiveSet = set<Value *>;
using PhiMap = map<BasicBlock *, vector<pair<Value *, Value *>>>;
public:
struct Interval {
Interval(int a, int b) : i(a), j(b) {}
Interval() = delete;
int i, j;
};
LiveRangeAnalyzer(Module *m_, PhiMap &phi_map_)
: m(m_), phi_map(phi_map_) {}
LiveRangeAnalyzer() = delete;
void run();
void clear();
void print(Function *func, bool printSet = true);
string print_liveSet(LiveSet &ls) {
string s = "[ ";
for (auto k : ls)
s += k->get_name() + " ";
s += "]";
return s;
}
private:
Module *m;
// Function *func;
map<Value *, Interval> liverange;
map<int, LiveSet> IN, OUT;
map<Value *, int> instr_id;
map<pair<Value *, Value *>, int> cpstmt_id;
const PhiMap &phi_map;
void make_id(Function *);
void run(Function *);
LiveSet joinFor(BasicBlock *bb);
void union_ip(LiveSet &dest, LiveSet &src) {
LiveSet res;
set_union(dest.begin(),
dest.end(),
src.begin(),
src.end(),
std::inserter(res, res.begin()));
dest = res;
}
// Require: out-set is already set
// Return: the in-set(will not set IN-map)
LiveSet transferFunction(Instruction *);
};
#endif
add_library(codegen STATIC
codegen.cpp
liverange.cpp
)
target_link_libraries(common)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment