Skip to content

Instantly share code, notes, and snippets.

@rhysd
Last active February 15, 2017 09:58
Show Gist options
  • Save rhysd/4e9624fa8ff49fe332329b75d0d8b7fc to your computer and use it in GitHub Desktop.
Save rhysd/4e9624fa8ff49fe332329b75d0d8b7fc to your computer and use it in GitHub Desktop.
変遷の様子
let a = 10 in
let rec f x = if x < 1 then 0 else x + (f (x - 1)) in
print_int (f a)
<let:let>(1:1:0-1:4:3)
<IDENT:a>(1:5:4-1:6:5)
<=:=>(1:7:6-1:8:7)
<INT:10>(1:9:8-1:11:10)
<in:in>(1:12:11-1:14:13)
<let:let>(2:1:14-2:4:17)
<rec:rec>(2:5:18-2:8:21)
<IDENT:f>(2:9:22-2:10:23)
<IDENT:x>(2:11:24-2:12:25)
<=:=>(2:13:26-2:14:27)
<if:if>(2:15:28-2:17:30)
<IDENT:x>(2:18:31-2:19:32)
<<:<>(2:20:33-2:21:34)
<INT:1>(2:22:35-2:23:36)
<then:then>(2:24:37-2:28:41)
<INT:0>(2:29:42-2:30:43)
<else:else>(2:31:44-2:35:48)
<IDENT:x>(2:36:49-2:37:50)
<+:+>(2:38:51-2:39:52)
<(:(>(2:40:53-2:41:54)
<IDENT:f>(2:41:54-2:42:55)
<(:(>(2:43:56-2:44:57)
<IDENT:x>(2:44:57-2:45:58)
<-:->(2:46:59-2:47:60)
<INT:1>(2:48:61-2:49:62)
<):)>(2:49:62-2:50:63)
<):)>(2:50:63-2:51:64)
<in:in>(2:52:65-2:54:67)
<IDENT:print_int>(3:1:68-3:10:77)
<(:(>(3:11:78-3:12:79)
<IDENT:f>(3:12:79-3:13:80)
<IDENT:a>(3:14:81-3:15:82)
<):)>(3:15:82-3:16:83)
<EOF:>(4:1:84-4:1:84)
AST for foo.ml:
- Let (a) (1:1-3:15)
- - Int (1:9-1:11)
- - LetRec (fun f x) (2:1-3:15)
- - - If (2:15-2:41)
- - - - Less (2:18-2:22)
- - - - - VarRef (x) (2:18-2:19)
- - - - - Int (2:22-2:23)
- - - - Int (2:29-2:30)
- - - - Add (2:36-2:41)
- - - - - VarRef (x) (2:36-2:37)
- - - - - Apply (2:41-2:48)
- - - - - - VarRef (f) (2:41-2:42)
- - - - - - Sub (2:44-2:48)
- - - - - - - VarRef (x) (2:44-2:45)
- - - - - - - Int (2:48-2:49)
- - - Apply (3:1-3:15)
- - - - VarRef (print_int) (3:1-3:10)
- - - - Apply (3:12-3:15)
- - - - - VarRef (f) (3:12-3:13)
- - - - - VarRef (a) (3:14-3:15)
f$t2 = fun x$t3 ; type=int -> int
BEGIN: body (f$t2)
$k3 = int 1 ; type=int
$k4 = binary < x$t3 $k3 ; type=bool
$k13 = if $k4 ; type=int
BEGIN: then
$k5 = int 0 ; type=int
END: then
BEGIN: else
$k9 = int 1 ; type=int
$k10 = binary - x$t3 $k9 ; type=int
$k11 = app f$t2 $k10 ; type=int
$k12 = binary + x$t3 $k11 ; type=int
END: else
END: body (f$t2)
BEGIN: program
a$t1 = int 10 ; type=int
$k17 = app f$t2 a$t1 ; type=int
$k18 = appx print_int $k17 ; type=()
END: program
; ModuleID = 'foo.ml'
source_filename = "foo.ml"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-darwin15.6.0"
declare void @print_int(i64)
define i64 @"f$t2"(i64 %"x$t3") {
entry:
%less = icmp slt i64 %"x$t3", 1
br i1 %less, label %if.then, label %if.else
if.then: ; preds = %entry
br label %if.end
if.else: ; preds = %entry
%sub = sub i64 %"x$t3", 1
%0 = call i64 @"f$t2"(i64 %sub)
%add = add i64 %"x$t3", %0
br label %if.end
if.end: ; preds = %if.else, %if.then
%if.merge = phi i64 [ 0, %if.then ], [ %add, %if.else ]
ret i64 %if.merge
}
define i32 @main() {
entry:
%0 = call i64 @"f$t2"(i64 10)
call void @print_int(i64 %0)
ret i32 0
}
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 11
.globl _f$t2
.p2align 4, 0x90
_f$t2:
.cfi_startproc
pushq %rbx
Lcfi0:
.cfi_def_cfa_offset 16
Lcfi1:
.cfi_offset %rbx, -16
movq %rdi, %rbx
testq %rbx, %rbx
jle LBB0_1
leaq -1(%rbx), %rdi
callq _f$t2
addq %rbx, %rax
popq %rbx
retq
LBB0_1:
xorl %eax, %eax
popq %rbx
retq
.cfi_endproc
.globl _main
.p2align 4, 0x90
_main:
.cfi_startproc
pushq %rax
Lcfi2:
.cfi_def_cfa_offset 16
movl $10, %edi
callq _f$t2
movq %rax, %rdi
callq _print_int
xorl %eax, %eax
popq %rcx
retq
.cfi_endproc
.subsections_via_symbols
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment