自制编译器 青木峰郎 笔记 Ch15 编译表达式和语句

15.1 确认编译结果

cbc --print-asm -fverbose-asm xx.cb可以看到汇编代码。gcc -S -o - xx.c也可以。

root@cf43f429204e:/# cbc --print-asm -fverbose-asm if_test.cb 
.file	"if_test.cb"
	.section	.rodata
.LC0:
	.string	"OK"
.LC1:
	.string	"NO"
.LC2:
	.string	"ERR"
	.text
.globl main
	.type	main,@function
main:
	# ---- Stack Frame Layout -----------
	# (%ebp): return address
	# 4(%ebp): saved %ebp
	# 8(%ebp): argc
	# 12(%ebp): argv
	# -----------------------------------
	pushl	%ebp
	movl	%esp, %ebp
	# line 4: if(1) if (2) { puts("OK"); }
	# Int {
	movl	$1, %eax
	# }
	testl	%eax, %eax
	jnz	.L0
	jmp	.L1
.L0:
	# line 4: if (2) { puts("OK"); }
	# Int {
	movl	$2, %eax
	# }
	testl	%eax, %eax
	jnz	.L2
	jmp	.L3
.L2:
	# line 4: puts("OK"); }
	# Call {
	  # Str {
	movl	$.LC0, %eax
	  # }
	pushl	%eax
	call	puts
	addl	$4, %esp
	# }
	jmp	.L4
.L3:
	# line 5: if(3) {puts("NO");}
	# Int {
	movl	$3, %eax
	# }
	testl	%eax, %eax
	jnz	.L5
	jmp	.L6
.L5:
	# line 5: puts("NO");}
	# Call {
	  # Str {
	movl	$.LC1, %eax
	  # }
	pushl	%eax
	call	puts
	addl	$4, %esp
	# }
	jmp	.L7
.L6:
	# line 6: puts("ERR");}
	# Call {
	  # Str {
	movl	$.LC2, %eax
	  # }
	pushl	%eax
	call	puts
	addl	$4, %esp
	# }
.L7:
.L4:
.L1:
	# line 7: return 0;}
	# Int {
	movl	$0, %eax
	# }
	jmp	.L8
.L8:
	movl	%ebp, %esp
	popl	%ebp
	ret
	.size	main,.-main

15.2 x86汇编的对象与DSL

原文地址:https://www.cnblogs.com/xuesu/p/14385848.html