Haskell 如何将G代码转换为LLVM?
我正在跟随这本书,基本上已经完成了一个使用G-Machine的核心编译器和解释器。在章节的描述中,据说G代码可以翻译成机器代码,但是如何翻译呢?G代码(指令)旨在运行状态转换机,操作多个堆栈和关联列表。如何使用LLVM-general/LLVM-general-pure包在LLVM中完成类似的操作?最后,我想让这个编译器输出一个可执行文件 以下是状态和说明的样子:Haskell 如何将G代码转换为LLVM?,haskell,compilation,llvm,code-generation,Haskell,Compilation,Llvm,Code Generation,我正在跟随这本书,基本上已经完成了一个使用G-Machine的核心编译器和解释器。在章节的描述中,据说G代码可以翻译成机器代码,但是如何翻译呢?G代码(指令)旨在运行状态转换机,操作多个堆栈和关联列表。如何使用LLVM-general/LLVM-general-pure包在LLVM中完成类似的操作?最后,我想让这个编译器输出一个可执行文件 以下是状态和说明的样子: type GmState = (GmOutput, -- current output GmCode,
type GmState = (GmOutput, -- current output
GmCode, -- current instruction stream
GmStack, -- current stack
GmDump, -- a stack for WHNF reductions
GmHeap, -- heap of nodes
GmGlobals, -- global addresses in heap
GmStats) -- statistics
type GmOutput = [Char]
type GmCode = [Instruction]
type GmStack = [Addr]
type GmDump = [GmDumpItem]
type GmDumpItem = (GmCode, GmStack)
type GmHeap = Heap Node
type GmGlobals = ASSOC Name Addr
type GmStats = Int
data Instruction = Unwind -- unravels the spine of the evaluation tree (such as evaluating the next super combinator)
| Pushglobal Name -- push address of global to stack
| Pushint Int -- allocate node in heap, add address to stack
| Push Int -- pushes the nth address in the stack to the top of the stack
| Mkap -- takes first 2 pointers on stack and adds a new address to the top of the stack to both addresses
| Update Int -- updates the nth address in the stack in the heap, takes the address of the top of the stack
| Pop Int -- pops the first n addresses from the stack
| Slide Int -- like pop, but keeps the first address on top of the stack
| Alloc Int -- allocates n nodes in the heap and puts them on top of the stack
| Eval -- takes current code and puts it in the dump, evaluates the top address by unwinding
| Add | Sub | Mul | Div | Neg
| Eq | Ne | Lt | Le | Gt | Ge
| Cond GmCode GmCode
| Pack Int Int -- creates a new datatype by adding a unique name to the heap
| Casejump [(Int, GmCode)] -- evaluates a number of cases by matching alts with datatypes in the heap
| Split Int -- looks up first address of stack in heap, if exists then takes the list of addresses found and appends to stack
| Print -- write to GmOutput
我想知道的是:是否有任何LLVM数据结构可以模仿GMachine,如果有,是什么,LLVM中的堆栈机是正确的方法?问题有点广泛。也许你对一些说明有一些想法,只需要一些帮助,或者。。。?我的意思是,我们不会坐下来为您实现一个完整的编译器。一个选择可能是查看GHC LLVM代码生成器。@DanielWagner让你们完成所有工作不是我的意图。这是一个类似的问题,可能有助于@DanielWagner。我想更具体地说,我想知道如何在LLVM中模拟与G机器相同的堆栈机器,或者至少是可以做相同事情的东西。由于我不熟悉LLVM或它在Hackage上的包,我想知道是否有任何功能可以让我实现这一点。如果没有的话,我正在寻找一个正确的方向来实现一个可以实现的功能。试着抓住我(Lennart Augustsson)和Thomas Johnsson在80年代写的一些早期论文。它们包含了一些信息。