summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--asm/assembler.h4
-rw-r--r--asm/encode.cpp6
-rw-r--r--asm/encode.h14
-rw-r--r--cpp.cpp31
-rw-r--r--cpp.h11
6 files changed, 52 insertions, 15 deletions
diff --git a/Makefile b/Makefile
index a50edc2..3d3575f 100644
--- a/Makefile
+++ b/Makefile
@@ -47,6 +47,7 @@ endif
PROGSRC=\
asm/assembler.cpp \
asm/chunk.cpp \
+ asm/encode.cpp \
asm/intel64/add.cpp \
asm/intel64/int.cpp \
asm/intel64/jmp.cpp \
diff --git a/asm/assembler.h b/asm/assembler.h
index f301d60..832a78e 100644
--- a/asm/assembler.h
+++ b/asm/assembler.h
@@ -1,3 +1,5 @@
+// Helper Functions for assembling
+
#pragma once
#include "chunk.h"
@@ -86,7 +88,7 @@ public:
std::string m_name;
};
-};
+}; // class Args
} // namespace Asm
diff --git a/asm/encode.cpp b/asm/encode.cpp
new file mode 100644
index 0000000..ea50cb7
--- /dev/null
+++ b/asm/encode.cpp
@@ -0,0 +1,6 @@
+#include "encode.h"
+
+void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment)
+{
+}
+
diff --git a/asm/encode.h b/asm/encode.h
new file mode 100644
index 0000000..d9e5674
--- /dev/null
+++ b/asm/encode.h
@@ -0,0 +1,14 @@
+// Convert: Abstract FlowGraph to Machine dependent Code
+
+#pragma once
+
+#include "flowgraph/graph.h"
+#include "segment.h"
+
+namespace Asm {
+
+// in: graph
+// out: segment
+void toMachineCode(const FlowGraph::Graph& graph, Segment& segment);
+
+} // namespace Asm
diff --git a/cpp.cpp b/cpp.cpp
index 2184a39..c988b5d 100644
--- a/cpp.cpp
+++ b/cpp.cpp
@@ -1,9 +1,10 @@
#include "cpp.h"
-#include "flowgraph/node.h"
+#include "asm/encode.h"
#include "bnf.h"
#include "cppbnf.h"
#include "debug.h"
+#include "flowgraph/node.h"
#include "lexer.h"
#include "grammer.h"
#include "minicc.h"
@@ -101,7 +102,7 @@ std::string CPP::valueOfNode(index_t node_index) const
if (!pos0)
throw std::runtime_error("ICE: Node value not available");
- return m_code.substr(*pos0, m_tokens[last_index].location.pos - *pos0) + m_tokens[last_index].value;
+ return m_source.substr(*pos0, m_tokens[last_index].location.pos - *pos0) + m_tokens[last_index].value;
};
std::string CPP::typeOfNode(index_t node_id) const
@@ -572,25 +573,36 @@ void CPP::translate()
// Phase 8: Instantiate objects
void CPP::instantiate()
{
- // TODO
+ // TODO: template instantiation
+
+ Asm::toMachineCode(mCPPContext.graph, mSegment);
}
// Phase 9: Link libraries
void CPP::link()
{
// TODO
+
+ // mSegment -> elf
+#if 0
+ return {
+ 0x48, 0xc7, 0xc0, 0x3c, 0x00, 0x00, 0x00, // mov $0x3c,%rax # syscall 60
+ 0x48, 0x31, 0xff, // xor %rdi,%rdi # exit code 0
+ 0x0f, 0x05, // syscall
+ };
+#endif
}
// phases of translation, according to standard
-void CPP::compile(const std::string& code)
+void CPP::compile(const std::string& source)
{
- m_code = code;
+ m_source = source;
source_charset_map(); // phase 1
backslash_escape(); // phase 2
- auto pp_tokens = preprocessing_tokenize(code); // phase 3
+ auto pp_tokens = preprocessing_tokenize(m_source); // phase 3
preprocess(); // phase 4
@@ -609,12 +621,7 @@ void CPP::compile(const std::string& code)
std::vector<uint8_t> CPP::getCode()
{
- // TODO
- return {
- 0x48, 0xc7, 0xc0, 0x3c, 0x00, 0x00, 0x00, // mov $0x3c,%rax # syscall 60
- 0x48, 0x31, 0xff, // xor %rdi,%rdi # exit code 0
- 0x0f, 0x05, // syscall
- };
+ return mCode;
}
std::vector<uint8_t> CPP::getData()
diff --git a/cpp.h b/cpp.h
index 267aa9d..8258011 100644
--- a/cpp.h
+++ b/cpp.h
@@ -1,5 +1,6 @@
#pragma once
+#include "asm/segment.h"
#include "flowgraph/graph.h"
#include "grammer.h"
#include "minicc.h"
@@ -39,13 +40,13 @@ public:
void link(); // phase 9
// all phases of translation
- void compile(const std::string& code);
+ void compile(const std::string& source);
std::vector<uint8_t> getCode();
std::vector<uint8_t> getData();
private:
- std::string m_code; // input from compile()
+ std::string m_source; // input from compile()
std::vector<Token> m_tokens; // result of phase 7.a
std::vector<Gram::TreeNode> m_nodes; // result of phase 7.b
@@ -70,5 +71,11 @@ private:
void getValueOfToken(index_t index);
void getValueOfNode(index_t index);
void visitRecursive(index_t node_id);
+
+ // phase 8: instantiate: instantiate templates; flowgraph->asm
+ Segment mSegment;
+
+ // phase 9: link
+ std::vector<uint8_t> mCode;
};