[swarthmore cs75] inlab1 — Tiny Compiler
课程回顾
Swarthmore学院16年开的编译系统课,总共10次大作业。本随笔记录了inlab1的实践过程。
tiny compiler
这个迷你的编译器可以将一个源文件,编译成可执行的二进制代码。它包括以下文件:
87.int:源代码只包括一个整数
87
compiler.ml:将.int的源文件编译为.s的汇编文件
open Printf
let compile (program: int) : string =
sprintf "
section .text
global our_code_starts_here
our_code_starts_here:
mov eax, %d
ret\n" program;;
let () =
let input_file = (open_in (Sys.argv.(1))) in
let input_program = int_of_string (input_line input_file) in
let program = (compile input_program) in
printf "%s" program;;
命令行执行:
⤇ ocaml compiler.ml 87.int > 87.s
main.c:执行汇编代码并打印到控制台。
#include <stdio.h>
extern int our_code_starts_here() asm("our_code_starts_here");
int main(int argc, char** argv)
{
int result = our_code_starts_here();
printf("%d\n", result);
return 0;
}
命令行执行:
⤇ nasm -f macho -o 87.o 87.s
⤇ clang -m32 -o 87.run main.c 87.o
Makefile:将编译过程写成make文件
%.run: %.o
clang -m32 -o $@ main.c $<
%.o: %.s
nasm -f macho -o $@ $<
%.s: %.int
ocaml compiler.ml $< > $@
命令行执行:
⤇ make 87.run
\⤇ ./87.run
87
可能会出现的错误:
- The macOS 10.14 SDK no longer contains support for compiling 32-bit applications. If developers need to compile for i386, Xcode 9.4 or earlier is required. (39858111)
⤇ clang -g -m32 -o our_code main.c our_code.o
ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/lib/libSystem.tbd, missing required architecture i386 in file /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/lib/libSystem.tbd
Undefined symbols for architecture i386:
"_printf", referenced from:
_main in main-246508.o
ld: symbol(s) not found for architecture i386
安装 Xcode 9.4.1 ,然后执行:
⤇ sudo xcode-select -s /Applications/Xcode\ 9.4.1.app
验证目前使用的clang:
⤇ xcodebuild -find clang
/Applications/Xcode 9.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
- 找不到'stdio.h'头文件
⤇ clang -g -m32 -o our_code main.c our_code.o
main.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
安装headers文件:
⤇ cd /Library/Developer/CommandLineTools/Packages
⤇ open macOS_SDK_headers_for_macOS_10.14.pkg
参考资料
[swarthmore cs75] inlab1 — Tiny Compiler的更多相关文章
- [swarthmore cs75] Compiler 6 – Garbage Snake
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第9次大作业. 赋值的副作用:循环元组 下面的代码展示了Python3是如何处理循环列表(pri ...
- [swarthmore cs75] Compiler 6 – Fer-de-lance
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第8次大作业. First-class function: It treats function ...
- [swarthmore cs75] Compiler 5 – Egg-eater
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第7次大作业. 抽象语法: 存储方式: 栈中的数据如果最后三位(tag bits)是001表示元 ...
- [swarthmore cs75] Compiler 4 – Diamondback
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第6次大作业. 函数声明 增加函数声明.函数调用的抽象语法:在转换成anf之前还要检查函数声明和 ...
- [swarthmore cs75] Compiler 3 – Cobra
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第5次大作业. 增加了bool数据表示和比较运算符的支持,具体语法参考下图: 第一种int和bo ...
- [swarthmore cs75] Compiler 2 – Boa
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第4次大作业. A-Normal Form 在80年代,函数式语言编译器主要使用Continua ...
- [swarthmore cs75] Compiler 1 – Adder
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第3次大作业. 编译的过程:首先解析(parse)源代码,然后成抽象语法树(AST),再生成汇编 ...
- [swarthmore cs75] Lab 1 — OCaml Tree Programming
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第2大次作业. 比较两个lists的逻辑: let rec cmp l ll = match ( ...
- [swarthmore cs75] Lab 0 Warmup & Basic OCaml
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第1次大作业. 什么是编译 编译就是执行Program->Program'转换的过程,如下 ...
随机推荐
- 在类文件中创建 写入Json文件
由于业务需要 今天写了一个方法能够定时更新Json文件 即定时从数据库中查询数据 然后转化为Json对象 如果有数据的话 删掉之前的Json文件 重新创建一个文件 然后写入Json对象 中间走了很多弯 ...
- JAVA获取客户端请求的当前网络ip地址(附:Nginx反向代理后获取客户端请求的真实IP)
1. JAVA获取客户端请求的当前网络ip地址: /** * 获取客户端请求的当前网络ip * @param request * @return */ public static String get ...
- linux命令——wc
wc 统计文件里面有多少单词,多少行,多少字符. wc语法 [root@www ~]# wc [-lwm] 选项与参数: -l :仅列出行: -w :仅列出多少字(英文单字): -m :多少字符: 默 ...
- html转化为图片下载
业务需求:按照客户要求把排课表转化为图片下载到本地.一个月到排课有很多.所以图片会很大 <!DOCTYPE html> <html lang="en"> & ...
- [BZOJ2252]矩阵距离(BFS)
题意 输入矩阵m行n列(m<=500,n<=500),只含0.1,输出离每个元素距离最近的1的距离,其中距离定义为D(aij,akl)=abs(i-k)+abs(j-l). 示例: 输入: ...
- easyUI添加修改tab页(toolbar)
代码: <div id="editdialos" class="easyui-dialog" title="虚机配置修改" data- ...
- wpf 寻找TreeView的子元素,并对其进行操作
//itemsControl 开始为指定的TreeView控件 item为TreeView子元素 private void PareItems(ItemsControl itemsControl, ...
- Checked Uncheckd异常
Checked : 你可以在写代码的时候就throw 或者try catch 的 Unchecked : Error + RuntimeException .提前无法预测的 http://www ...
- OAuth2.0 协议的理解
OAuth(Open Authorization)协议就是为用户资源的授权提供了一个安全.开放.简易的标准. OAuth在第三方应用与服务提供商之间设置了一个授权层,第三方应用通过授权层获取令牌,再通 ...
- 20170506计划-----(基于python查询oracle语句)
在日常的工作中,经常接到开发同事查询生产SQL的请求,公司又不允许对开发开放查询SQL的权限,并且查询的堡垒机又很慢,计划做一个可以自动查询SQL的小工具,一周内完成吧. 大概功能实现了,一些涉及敏感 ...