Skip to content

Commit

Permalink
prepare for code-gen
Browse files Browse the repository at this point in the history
next finish semant and translate.
next x86 code gen
  • Loading branch information
rechardchen committed May 1, 2017
1 parent 7b233fe commit 61902a8
Show file tree
Hide file tree
Showing 16 changed files with 452 additions and 18 deletions.
111 changes: 111 additions & 0 deletions allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#pragma once
#include "utils.h"

#include <cstdlib>
#include <cstdint>
#include <vector>

namespace tiger {
template <typename DerivedT> class AllocatorBase {
public:
void *Allocate(size_t size, size_t align) {
return static_cast<DerivedT*>(this)->Allocate(size, align);
}
void Deallocate(const void* ptr, size_t size) {
static_cast<DerivedT>(this)->Deallocate(ptr, size);
}
template <typename T>
T* Allocate(size_t num = 1) {
return static_cast<T*>(Allocate(num * sizeof(T), alignof(T)));
}
template <typename T>
void Deallocate(const T* ptr, size_t num = 1) {
Deallocate(static_cast<const void*>(ptr), num * sizeof(T));
}
};

class MallocAllocator : public AllocatorBase<MallocAllocator> {
public:
void *Allocate(size_t size, size_t) {
return malloc(size);
}
void Deallocate(const void* ptr, size_t) {
free(const_cast<void*>(ptr));
}
};

template <typename AllocatorT = MallocAllocator, size_t SlabSize = 4096,
size_t SizeThreshold = SlabSize>
class BumpPtrAllocatorImpl : public AllocatorBase<BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold>> {

static_assert(SizeThreshold <= SlabSize, "SizeThreshold must not be greater than SlabSize.");

public:
//BumpPtrAllocatorImpl();
~BumpPtrAllocatorImpl() {
for (auto ptr : mSlabs) {
mRawAllocator.Deallocate(ptr, SlabSize);
}
mSlabs.clear();
for (auto& entry : mCustomizedBlocks) {
mRawAllocator.Deallocate(entry.first, entry.second);
}
mCustomizedBlocks.clear();
}

void* Allocate(size_t size, size_t align) {
size_t adjust = alignAdjustment(mCurPtr, align);

if (adjust + size <= size_t(mEndPtr - mCurPtr)) {
char* alignedPtr = mCurPtr + adjust;
mCurPtr = alignedPtr + size;
return alignedPtr;
}
size_t paddedSize = size + align - 1;
if (paddedSize > SizeThreshold) {
char* ptr = (char*)mRawAllocator.Allocate(paddedSize, 0);
mCustomizedBlocks.push_back({ ptr, paddedSize });
size_t adjust = alignAdjustment(ptr, align);
return ptr + adjust;
}
StartNewSlab();
adjust = alignAdjustment(mCurPtr, align);
char* alignedPtr = mCurPtr + adjust;
mCurPtr = alignedPtr + size;
return alignedPtr;
}

using AllocatorBase::Allocate;

void Deallocate(const void* ptr, size_t size) {
//empty
}

protected:
void StartNewSlab() {
char *ptr = (char*)mRawAllocator.Allocate(SlabSize, 0);
mSlabs.push_back(ptr);
mCurPtr = ptr;
mEndPtr = ptr + SlabSize;
}

AllocatorT mRawAllocator;

char* mCurPtr = nullptr;
char* mEndPtr = nullptr;
std::vector<char*> mSlabs;
std::vector<std::pair<char*, size_t>> mCustomizedBlocks;
};

typedef BumpPtrAllocatorImpl<> BumpPtrAllocator;
}

template<typename AllocatorT, size_t SlabSize, size_t SizeThreshold>
void * operator new(size_t size, tiger::BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold>& allocator) {
return nullptr;
}

template<typename AllocatorT, size_t SlabSize, size_t SizeThreshold>
void operator delete(const void* ptr, tiger::BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold>& allocator) {

}
1 change: 1 addition & 0 deletions driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//tiger compiler driver
1 change: 1 addition & 0 deletions frame.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once
1 change: 1 addition & 0 deletions semant.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once
24 changes: 14 additions & 10 deletions symbol.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "symbol.h"
#include "allocator.h"
#include <cstring>
#include <unordered_set>

Expand All @@ -22,21 +23,24 @@ namespace tiger
size_t(*)(const char*),
bool(*)(const char*, const char*)> s_symbols(100, &hash_string, &equal_string);

Symbol::Symbol(const std::string& name) {
if (!name.empty()) {
auto iter = s_symbols.find(name.c_str());
if (iter == s_symbols.end()) {
auto l = name.length();
auto tmp = new char[l + 1];
strncpy(tmp, name.c_str(), l);
tmp[l] = '\0';
static BumpPtrAllocator s_symbolPool;

const char* Symbol::intern(const char* str) {
size_t n = strlen(str);
if (n != 0) {
auto iter = s_symbols.find(str);
if (iter == s_symbols.end()) {
auto tmp = s_symbolPool.Allocate<char>(n + 1);
memcpy(tmp, str, n);
tmp[n] = '\0';
s_symbols.insert(tmp);
mName = tmp;
return tmp;
}
else {
mName = *iter;
return *iter;
}
}
return nullptr;
}

}
64 changes: 61 additions & 3 deletions symbol.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,74 @@
#pragma once
#include <string>
#include <unordered_map>
#include <vector>

namespace tiger
{
class Symbol
{
public:
Symbol(const std::string& name);
const char* name() const { return mName; }
bool operator==(const Symbol& rhs) const { return mName == rhs.mName;}
Symbol() : mName(nullptr) {}

Symbol(const std::string& name) {
mName = intern(name.data());
}
Symbol(const char* name) {
mName = intern(name);
}
inline const char* Name() const {
return mName;
}
inline bool operator==(const Symbol& rhs) const {
return mName == rhs.mName;
}

public:
struct Hasher {
size_t operator() (const Symbol& s) const{
//#if sizeof(size_t) == sizeof(const char*)
// return size_t(s.mName);
//#else
// return std::hash<const char*>()(s.mName);
//#endif
return size_t(s.mName);
//return std::hash<const char*>()(s.mName);
}
};

protected:
const char* intern(const char* str);
const char* mName = nullptr;
};

//Symbol Table Implementation(Simple)
template <typename T>
class SymbolTable {
typedef std::unordered_map<Symbol, T*, Symbol::Hasher> TableType;

public:
void BeginScope() {
mTableStack.emplace_back(TableType());
}
void EndScope() {
mTableStack.pop_back();
}

void Enter(Symbol sym, T* value) {
auto& table = mTableStack.back();
table[sym] = value;
}

T* Look(Symbol sym) {
T* val = nullptr;
for (auto iter = mTableStack.rbegin(); iter != mTableStack.rend(); ++iter) {
auto& t = *iter;
auto it = t.find(sym);
if (it != t.end()) return it->second;
}
return val;
}
protected:
std::vector<TableType> mTableStack;
};
}
29 changes: 24 additions & 5 deletions tc.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
#include "antlr4-runtime.h"
#include "absyn.h"
#include "symbol.h"
#include <iostream>
#include <fstream>
#include <map>

using namespace std;
using namespace tiger;

#pragma pack(push, 1)
struct C {
char c;
union {
double d;
long double ld;
long long ll;
void* p;
}x;
};
#pragma pack(pop)
int main()
{
ifstream in("../samples/test49.tig");
/*ifstream in("../samples/test49.tig");
stringstream ss;
ss << in.rdbuf();
dumpAST(parseAST(ss.str()));
char c;
cin >> c;
dumpAST(parseAST(ss.str()));*/
//Symbol s("rechardchen"), t("rechardchen");
cout << sizeof(C) << endl;
cout << offsetof(C, x) << endl;
cout << alignof(C) << endl;

char _c;
cin >> _c;

}
23 changes: 23 additions & 0 deletions temp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "temp.h"

//TODO: refactor this c-style code
namespace tiger {
namespace temp {
static int regStart = 100;
static int labelStart = 0;

Temp newTemp() {
return Temp(regStart++);
}

Label newLabel() {
char label[100] = { 0 };
sprintf(label, "L%d", labelStart++);
return Label(label);
}

Label newNamedLabel(const char* l) {
return Label(l);
}
}
}
14 changes: 14 additions & 0 deletions temp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include "symbol.h"

namespace tiger {
typedef int Temp;
typedef Symbol Label;

namespace temp {
Temp newTemp();
Label newLabel();
Label newNamedLabel(const char* l);
}

}
11 changes: 11 additions & 0 deletions tiger/tiger.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,31 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\absyn.cpp" />
<ClCompile Include="..\driver.cpp" />
<ClCompile Include="..\grammar\tigerBaseVisitor.cpp" />
<ClCompile Include="..\grammar\tigerLexer.cpp" />
<ClCompile Include="..\grammar\tigerParser.cpp" />
<ClCompile Include="..\grammar\tigerVisitor.cpp" />
<ClCompile Include="..\symbol.cpp" />
<ClCompile Include="..\tc.cpp" />
<ClCompile Include="..\temp.cpp" />
<ClCompile Include="..\type.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\absyn.h" />
<ClInclude Include="..\allocator.h" />
<ClInclude Include="..\frame.h" />
<ClInclude Include="..\grammar\tigerBaseVisitor.h" />
<ClInclude Include="..\grammar\tigerLexer.h" />
<ClInclude Include="..\grammar\tigerParser.h" />
<ClInclude Include="..\grammar\tigerVisitor.h" />
<ClInclude Include="..\semant.h" />
<ClInclude Include="..\symbol.h" />
<ClInclude Include="..\temp.h" />
<ClInclude Include="..\translate.h" />
<ClInclude Include="..\tree.h" />
<ClInclude Include="..\type.h" />
<ClInclude Include="..\utils.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{F70B671E-FBA9-497F-BE76-EDA7E286C851}</ProjectGuid>
Expand Down
11 changes: 11 additions & 0 deletions tiger/tiger.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<ClCompile Include="..\absyn.cpp" />
<ClCompile Include="..\tc.cpp" />
<ClCompile Include="..\symbol.cpp" />
<ClCompile Include="..\type.cpp" />
<ClCompile Include="..\temp.cpp" />
<ClCompile Include="..\driver.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\grammar\tigerBaseVisitor.h">
Expand All @@ -37,5 +40,13 @@
</ClInclude>
<ClInclude Include="..\absyn.h" />
<ClInclude Include="..\symbol.h" />
<ClInclude Include="..\allocator.h" />
<ClInclude Include="..\utils.h" />
<ClInclude Include="..\type.h" />
<ClInclude Include="..\semant.h" />
<ClInclude Include="..\translate.h" />
<ClInclude Include="..\frame.h" />
<ClInclude Include="..\tree.h" />
<ClInclude Include="..\temp.h" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions translate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once
Loading

0 comments on commit 61902a8

Please sign in to comment.