summaryrefslogtreecommitdiff
path: root/old/toy/codegen.hpp
blob: 6db068e0fa54d8a36e590b0933618fa6ab5e6171 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef _CODEGEN_H_
#define _CODEGEN_H_

#include <llvm/Support/TargetSelect.h>
#include <llvm/IR/Value.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/ExecutionEngine/GenericValue.h>

#include <map>
#include <stack>
#include <string>

class Node;
class NBlock;
class NIdentifier;

// this is one generated block of LLVM code, containing also
// all local variables known in this scope
class CodeGenBlock
{
	public:
		llvm::BasicBlock *m_block;
		std::map<std::string, llvm::Value *> m_locals;
};

// this class we pass along our ADT structure
class CodeGenContext
{
	public:
		typedef std::map<std::string, llvm::Value *> Locals;
	private:
		std::stack<CodeGenBlock *> m_blocks;
		llvm::Module *m_module;
		llvm::Function *m_mainFunction;
		Locals m_locals;

	public:
		CodeGenContext( );
		void printGeneratedCode( );
		std::string getGeneratedCode( );
		void generateCode( NBlock& _root );
		llvm::GenericValue runCode( );
		static llvm::Type *variableType( const NIdentifier& _type );
		void pushBlock( llvm::BasicBlock *_block );
		void popBlock( );
		llvm::BasicBlock *currentBlock( );
		Locals& locals( ) { return m_blocks.top( )->m_locals; }
		llvm::Module *module( ) { return m_module; }
		
};

#endif // _CODEGEN_H_