软件工程作业-方舟学习笔记08-部分原理(续

这周看方舟编译器的优化算法,越看越觉得迷惑。今天回归一下本源。
MAPLE IR is the common representation for programs compiled from different programming languages, which include general purpose languages like C, C++ and Java. MAPLE IR is extensible. As additional languages, including domain-specific languages, are compiled into MAPLE IR, more constructs will be added to represent constructs unique to each language.
任何语言都可以翻译成Maple IR 语言。以下列语句为例:
C source:

int fact(int n) {
	if(n!=1)
		return n*fact(n-1);
	else return 1;
}

Maple IR:

func &fact (var %n i32) i32 {
	if(ne i32 (dread i32 %n, constval i32 1) {
		call $fact (sub i32 (dread i32 %n, constval i32 1))
		return (mul i32 (dread i32 %n, regread i32 %%retval))
	}
	return(constval i32 1)
}

Since MAPLE lR is one lR that can exist at multiple levels of semantics, the level of a MAPLE IR program is dictated by the constraints that it adheres to. These constraints are of the following two types:
Opcodes allowed - The higher the level, the more types of opcodes allowed, including opcodes generated only from specific languages. At the lowest level, only opcodes that correspond one-to-one to operations in a general purpose processor are allowed.
Code structure - The program structure is hierarchical at the higher levels. The hierarchical constructs become less and less as lowering proceeds. At the lowest level, the program structure is flat, consisting of sequences of primitive instructions consumed by the general purpose processor.
也就是说,Maple IR是不支持不同粒度的,各种编程语言都有自己的Code Structure 和Opcodes,在更高粒度,就是更接近各种编程语言个性化的程序结构,会允许各种个性化运算的Opcode。但编译到更低层次,就更接近于x86、arm、这种处理器的原生指令。
There are three kinds of executable nodes in MAPLE IR:
Leaf nodes - Also called terminal nodes, these nodes denote a value at execution time, which may be a constant or the value of a storage unit.
Expression nodes - An expression node performs an operation on its operands to compute a result. lts result is a function of the values of its operands and nothing else. Each operand can be either a leaf node or another expression node. Expression nodes are the internal nodes of expression trees. The type field in the expression node gives the type associated with the result of the operation.
Statement nodes - These represent the flow of control. Execution starts at the entry of the function and continues sequentially statement by statement until a control flow statement is executed. Apart from modifying control flow, statements can also modify data storage in the program. A statement nodes has operands that can be leaf,
expression or statement nodes.
一个编程语言可以抽象成一棵树,Maple IR写出来的语言有三个节点:
Leaf Node,也就是C语言里的一个数字常量或是一个存储变量而已。
Expression Node,显然就是各种Leaf node的加加减减,组成的表达式树。
Statement Node就是C语言里的一行语句。如果是if条件语句,会改变程序执行的流程。

dassign $a(
	add i32(dread i32 $b, dread i32 $c))
dassign $a(
	sub i32(
		add i32(dread i32 $b, dread i32 $c),
		dread i32 $d))

Opcode是这种编程语言的一种操作符,可以理解为一个函数,无非是制定了参数和返回值类型。
The general rules regarding line breaks are as follows:
Each expression or statement node must occupy its own line, and each line cannot contain more than one expression or statement node.
When there is at least one operand that is not a leaf node, then all the operands of the current expression or statement node must be specified in separate new lines, including operands that are leaf nodes.
Comments can be specified via the character ‘#’, which can be regarded as the end of line character by the lR parser.
For human-edited MAPLE lR files, the line breaks are not enforced for expressions, as they do not affect the correctness of the program, since the end of operand specification is indicated by the closing parenthesis. But there must not be more than one statement node per line, because we do not use the ';'character to delimit statement boundary.
每句话最后无需分号隔开,直接根据括号是否匹配来决定一行,每行最多一个表达式节点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值