diff options
| author | Roland Reichwein <mail@reichwein.it> | 2020-11-08 16:38:30 +0100 | 
|---|---|---|
| committer | Roland Reichwein <mail@reichwein.it> | 2020-11-08 16:38:30 +0100 | 
| commit | db0654fa48ddc07e6bcaaaeddfa301a32806dadc (patch) | |
| tree | ea611c24a41ea8d9dc1e2116b64b9760f26708e0 | |
| parent | dd2a994fbbe946fa751b689e92c85696469e5e5c (diff) | |
Prepare encoding and linking
| -rw-r--r-- | Makefile | 1 | ||||
| -rw-r--r-- | asm/assembler.h | 4 | ||||
| -rw-r--r-- | asm/encode.cpp | 6 | ||||
| -rw-r--r-- | asm/encode.h | 14 | ||||
| -rw-r--r-- | cpp.cpp | 31 | ||||
| -rw-r--r-- | cpp.h | 11 | 
6 files changed, 52 insertions, 15 deletions
| @@ -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 @@ -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() @@ -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;  }; | 
