-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
next finish semant and translate. next x86 code gen
- Loading branch information
1 parent
7b233fe
commit 61902a8
Showing
16 changed files
with
452 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
//tiger compiler driver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#pragma once |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#pragma once |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#pragma once |
Oops, something went wrong.