课程回顾

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

参考资料

inlab1

[swarthmore cs75] inlab1 — Tiny Compiler的更多相关文章

  1. [swarthmore cs75] Compiler 6 – Garbage Snake

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第9次大作业. 赋值的副作用:循环元组 下面的代码展示了Python3是如何处理循环列表(pri ...

  2. [swarthmore cs75] Compiler 6 – Fer-de-lance

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第8次大作业. First-class function: It treats function ...

  3. [swarthmore cs75] Compiler 5 – Egg-eater

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第7次大作业. 抽象语法: 存储方式: 栈中的数据如果最后三位(tag bits)是001表示元 ...

  4. [swarthmore cs75] Compiler 4 – Diamondback

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第6次大作业. 函数声明 增加函数声明.函数调用的抽象语法:在转换成anf之前还要检查函数声明和 ...

  5. [swarthmore cs75] Compiler 3 – Cobra

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第5次大作业. 增加了bool数据表示和比较运算符的支持,具体语法参考下图: 第一种int和bo ...

  6. [swarthmore cs75] Compiler 2 – Boa

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第4次大作业. A-Normal Form 在80年代,函数式语言编译器主要使用Continua ...

  7. [swarthmore cs75] Compiler 1 – Adder

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第3次大作业. 编译的过程:首先解析(parse)源代码,然后成抽象语法树(AST),再生成汇编 ...

  8. [swarthmore cs75] Lab 1 — OCaml Tree Programming

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第2大次作业. 比较两个lists的逻辑: let rec cmp l ll = match ( ...

  9. [swarthmore cs75] Lab 0 Warmup & Basic OCaml

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第1次大作业. 什么是编译 编译就是执行Program->Program'转换的过程,如下 ...

随机推荐

  1. H5页面移动端IOS键盘收起焦点错位

    出现场景:IOS端,在弹出层点击input时调起键盘页面会被顶上去document.body.scrollOffset大于0,收起键盘时scrollOffset不变,造成焦点错位. 注:安卓手机点击时 ...

  2. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

  3. [Solution] 985. Sum of Even Numbers After Queries

    Difficulty: Easy Question We have an array A of integers, and an array queries of queries. For the i ...

  4. 通信导论-IP数据网络基础(3)

    ICMP(IP辅助协议)--网际控制报文协议 ICMP报文种类:ICMP差错报文(终点不可达.时间超过等5种)和ICMP询问报文(回送请求和回答请求.时间戳请求和回答报文2种) ICMP是一种集差错报 ...

  5. 配置java-jdk

    我将jdk安装在 /usr/local/jdk目录下 安装好之后 cd /etc/ ls vim profile export JAVA_HOME=/usr/jdk1.8.0_121  #你的jdk所 ...

  6. CentOS-07安装Redis学习笔记

    CentOS-07安装Redis 下载 http://download.redis.io/releases/redis-3.0.0.tar.gz 安装第一步:将下载的Redis源码包上传大奥Linux ...

  7. 类的反射及xml文件的解析

    类的反射 xml文件的解析 .properties||.xml配置文件的创建及读取内容 //创建对象 Properties properties = new Properties(); //存储 pr ...

  8. SpringCloud微服务基础

    1.传统项目架构  传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一个项目中.适合于个人或者小团队开发,不适合大团队开发. 2.分布式项目架构(解耦方案) 根据业务需求进行拆分成N个子 ...

  9. 在Linux 安装Python3.5.6详细文档!!!!

    在Linux 安装Python3.5.6详细文档!!!! 1.安装相关依赖库(工具包) yum install gcc patch libffi-devel python-devel  zlib-de ...

  10. git hub命令,上传到github

    git hub命令,上传到github   1,git  init;      初始化 2,git   config   --global   user.email  " ....@.... ...