Aslan Askarov aslan@cs.au.dk
acknowledgments: E.Ernst, M.I.Schwartzbach, J. Midtgaard, G. Morrisett, S. Zdancewic
Aslan Askarov aslan@cs.au.dk acknowledgments: E.Ernst, - - PowerPoint PPT Presentation
____ _ _ _ _ / ___|___ _ __ ___ _ __ (_) | __ _| |_(_) ___ _ __ | | / _ \| '_ ` _ \| '_ \| | |/ _` | __| |/ _ \| '_ \ | |__| (_) | | | | | | |_) | | | (_| | |_| | (_) | | | | \____\___/|_| |_| |_|
acknowledgments: E.Ernst, M.I.Schwartzbach, J. Midtgaard, G. Morrisett, S. Zdancewic
with compiler-defined semantics
#include <stdio.h> #include <stdlib.h> long factorial(long X) { if (X == 0) return 1; return X*factorial(X-1); } int main(int argc, char **argv) { printf("%ld\n", factorial(10)); return 0; }
$ clang factorial.c -S -O3 -o-
… Ltmp9: .cfi_def_cfa_register %rbp leaq L_.str(%rip), %rdi movl $3628800, %esi ## imm = 0x375F00 xorl %eax, %eax callq _printf xorl %eax, %eax popq %rbp ret .cfi_endproc .section __TEXT,__cstring,cstring_literals L_.str: ## @.str .asciz "%ld\n"
Source C program Compiled assembly
High-level source code Low-level target code Lexing/Parsing Lowering Code generation Elaboration Optimization
String/Files Abstract Syntax Tree Tokens
keyword: "function" identifier: "printint" symbol: "(" identifier: "i" symbol: ":" identifier: "int" symbol: ")" symbol: "=" keyword: "let" keyword: "function" identifier: "f" symbol: "(" identifier: "i" symbol: ":" identifier: "int" symbol: ")" symbol: "=" keyword: "if" identifier: "i" symbol: ">" intliteral: "0" keyword: "then" symbol: "(" identifier: "f" symbol: "(" identifier: "i" symbol: "/" intliteral: "10" symbol: ")" symbol: ";" identifier: "print" symbol: "(" identifier: "chr" symbol: "(" identifier: "i" symbol: "-" identifier: "i" symbol: "/" intliteral: "10" symbol: "*" intliteral: "10" symbol: "+" identifier: "ord" symbol: "(" symbol: "\"" stringliteral: "0" symbol: "\"" symbol: ")" symbol: ")" symbol: ")" symbol: ")" keyword: "in" ... keyword: "end"
FunctionDec[ (printint,[ (i,true,int)], NONE, LetExp([ FunctionDec[ (f,[ (i,true,int)], NONE, IfExp( OpExp(GtOp, VarExp( SimpleVar(i)), IntExp(0)), SeqExp[ CallExp(f,[ OpExp(DivideOp, VarExp( SimpleVar(i)), IntExp(10))]), CallExp(print,[ CallExp(chr,[ OpExp(PlusOp, OpExp(MinusOp, VarExp( SimpleVar(i)), OpExp(TimesOp, OpExp(DivideOp, VarExp( SimpleVar(i)), IntExp(10)), IntExp(10))), CallExp(ord,[ StringExp("0")]))])])]))]], SeqExp[ IfExp( OpExp(LtOp, VarExp( SimpleVar(i)), IntExp(0)), SeqExp[ CallExp(print,[ StringExp("-")]), CallExp(f,[ OpExp(MinusOp, IntExp(0), VarExp( SimpleVar(i)))])], IfExp( OpExp(GtOp, VarExp( SimpleVar(i)), IntExp(0)), CallExp(f,[ VarExp( SimpleVar(i))]), CallExp(print,[ StringExp("0")])))]))]
function printint type NONE args int i let function f type NONE if args if call print string "-" if < simplevar i int call f var call print simplevar i string “0” int i > var int var seq call f call print / simplevar i simplevar i int 10 call chr +
string “0” var simplevar i * / int 10 int 10 var simplevar i seq var seq call f
var simplevar i > var simplevar i int
Typed Abstract Syntax Tree Untyped Abstract Syntax Tree
Intermediate Representation Typed Abstract Syntax Tree
Optimization Intermediate Representation Intermediate Representation
Machine Code Intermediate Representation
.text # PROCEDURE tigermain .globl tigermain .func tigermain .type tigermain, @function tigermain: # FRAME tigermain(1 formals, 4 locals) pushl %ebp movl %esp, %ebp subl $20, %esp # SP, FP, calleesaves, argregs have values L16_blocks: movl -4(%ebp), %ebx movl $123, %ebx movl %ebx, -4(%ebp) movl -4(%ebp), %ebx pushl %ebx pushl %ebp call L2_printint jmp L15_block_done L15_block_done: # FP, SP, RV, calleesaves still live leave ret .size tigermain, .-tigermain ...
0000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 0000020 01 00 03 00 01 00 00 00 00 00 00 00 00 00 00 00 0000040 84 04 00 00 00 00 00 00 34 00 00 00 00 00 28 00 0000060 0f 00 0c 00 55 89 e5 83 ec 14 8b 5d fc bb 7b 00 0000100 00 00 89 5d fc 8b 5d fc 53 55 e8 fc ff ff ff eb 0000120 00 c9 c3 55 89 e5 83 ec 40 8b 5d 0c 89 5d fc 8b 0000140 5d fc 83 fb 00 7c 3b eb 00 8b 5d 0c 89 5d f8 8b 0000160 5d f8 83 fb 00 7f 72 eb 00 8b 5d f4 bb 00 00 00 0000200 00 89 5d f4 8b 5d f4 53 8b 5d 08 89 5d f0 8b 5d 0000220 f0 8b 4b 08 89 4d ec 8b 5d ec 53 e8 fc ff ff ff ... 0005140 06 00 00 00 01 16 00 00 10 00 00 00 01 01 00 00 0005160
source lang target lang implementation lang (T-diagram)