A Pascal compiler in Haskell that compiles to LLVM

栏目: IT技术 · 发布时间: 4年前

内容简介:A (reduced) Pascal compiler in Haskell that compiles to LLVMOnce the executable is built, it can be used to compile Pascal source files to llvm-ir, or internal IR used by the compiler:The compiler is complemented with the

Paskell

A (reduced) Pascal compiler in Haskell that compiles to LLVM

Features

  • Declarations: var, type (aliases)
  • Types: integer, boolean, string, char, real
  • Control Flow: if, while, for
  • Functions/Procedures
  • Pass by reference
  • Basic Typecasting
  • Nested Functions/Procedures (Not finished yet)
  • I/O: Write/Writeln

Progress

Usage

Once the executable is built, it can be used to compile Pascal source files to llvm-ir, or internal IR used by the compiler:

paskell -c src compile to llvm-ir

paskell -c src dest compile to llvm-ir and save in dest

paskell -ir src produce internal IR

paskell -x src execute pascal source. Equivalent to paskell -c src | lli

paskell -h (for help)

Demo:

The compiler is complemented with the llvm utilities

$ paskell -c fib.pas fib.ll

Since the output is llvm-ir, we can leverage the many tools LLVM provide to:

  • execute it using the llvm interpreter
    $ lli fib.ll
  • convert it to bitcode llvm assembly (.bc)
    $ llvm-as fib.ll -o fib.bc
  • optimize the code using various settings, for example
    $ opt -mem2reg fib.bc
  • translate it to a native assembly executable of a specific architecture (x86, ARM, etc)
    $ llc -march=x86-64 fib.bc -o fib.s
  • link many modules into one program

Building

With docker

$ make bash

to build the compiler and launch a shell session where the compiler and llvm utitlies are in $PATH and ready out-of-the-box.

Alternatively

$ make build

will build the same image tagged paskell which can be used with docker run and volumes. For example:

$ docker run -v /path/to/original_file.pas:/path/to/file.pas paskell paskell -c /path/to/file.pas

Without docker

You need to have llvm installed

$ sudo apt-get install llvm-5.0

lli should be in $PATH to be able to execute Pascal programs

Then, you can use Cabal or Stack.

To build using Cabal:

$ cd Paskell/
$ cabal install -j

this will install all dependencies and produce an executable in dist/build/Paskell/

You can also build using Stack.

Tests

$ make test

to run the test suite using docker.

Implementation

This is a 4-pass compiler:

pass 1: lex/parsing

pass 2: type checking

pass 3: constructing IR: type-annotation, type resolution, (future: identifier-renaming, nested-function extraction)

pass 4: code generation

TODO

  • finish nested functions/procedures:
    this only requires pulling nested functions to global scope
    and renaming them during the type-annotation pass
  • constants: trivial to implement
  • Read/Readln IO statements
  • records
  • arrays
  • case statements
  • forward declaration

Contributions

Bug reports, added features, etc are welcome

References


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构与算法分析

数据结构与算法分析

[美]Mark Allen Weiss / 张怀勇 / 人民邮电出版社 / 2007年 / 49.00元

《数据结构与算法分析:C++描述(第3版)》是数据结构和算法分析的经典教材,书中使用主流的程序设计语言C++作为具体的实现语言。书的内容包括表、栈、队列、树、散列表、优先队列、排序、不相交集算法、图论算法、算法分析、算法设计、摊还分析、查找树算法、k-d树和配对堆等。《数据结构与算法分析:C++描述(第3版)》适合作为计算机相关专业本科生的数据结构课程和研究生算法分析课程的教材。本科生的数据结构课......一起来看看 《数据结构与算法分析》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具