`

Notes on Translating Three-Address Code to Assembly Code for the SPARC

阅读更多

http://www.cs.nmsu.edu/~jeffery/courses/370/code-sparc.html

Notes on Translating Three-Address Code to Assembly Code for the SPARC

Notes on the SPARC Architecture

General Information

The SPARC, invented by Sun and licensed and manufactured by Sun, T.I., and others, is one of the first widely used commercial RISC architectures. Generating code for it involves understanding the instruction set as well as the syntax of the Sun assembler used to take ascii assembly language code and produce machine code object files.

Assembly code files should end with the suffix ".s" . Such a file can be assembled to executable code by invoking the C compiler:

% cc -o foo foo.s

Note: running "as foo.s " will not produce an executable module, it will produce an object module (.o) that requires linking. For examples of the assembly code produced by the C compiler on the Sun, use "cc -S ". Your compiler should behave in the same manner as the standard C compiler, calling the assembler and linker by default. You may wish to leave .s files around after assembly for debugging purposes, rather than deleting them by default.

Memory Alignment

Sparc architecture has strict alignment requirements on memory access; 16-bit values can only be loaded and stored to even addresses, while 32-bit values can only be loaded and stored to addresses that are multiples of 4.

Registers and Register Windows

The Sparc is a load/store architecture with many registers, and hardware support for register mapping which greatly reduces the cost of normal procedure calls. At any one time, there are 32 registers available to machine instructions, although the typical SPARC has upwards of 128 registers on the chip. The assembler's names for registers all begin with a percent sign % to distinguish them from ordinary symbols. The registers' names are %g0-%g7 , %i0-%i7 , %o0-%o7 , and %l0-%l7

.

The SPARC registers are organized into register windows (also called register files) that define the set of registers that are addressable at any given point during execution. Generally, each time a procedure is called, 16 registers that were visible in the previous procedure become invisible, and 16 new registers become addressable. 8 registers which were visible in the caller as %o0-%o7 become the called routine's %i0-%i8. The register window set is circular and wraps around. When no more registers are available for a call, a register spill occurs, in which a hardware trap to the operating system saves some registers onto the stack in order to make them available for the new procedure. This register window mapping is done by save/restore instructions, and can be avoided for leaf routines that do not call other routines.

The Stack

The stack grows from high addresses towards low addresses. The stack pointer register is %o6, a.k.a. %sp. The frame pointer register is %i6, a.k.a. %fp. The return value is normally a small offset of %i7, since the current program counter is stored in %o7 by the call instruction. A stack frame has the following structure:

 

Code Generation

Identifiers

Global identifier id in the source program translates directly to an identifier id in the assembly code generated. Local identifier id translates into an offset from the frame pointer. Be careful with negative offsets, since the memory addressed still refers to bytes extending in a positive direction from the byte referenced. For example, a 4-byte load from %fp-4 gives addresses -4, -3, -2, and -1 from the current frame pointer, not bytes at -4, -5, -6, and -7. A load from %fp+4 would indeed give memory at offsets 4, 5, 6, and 7.

Assembler Directives

Space for global variables is allocated in a data segment:

 .section  ".data"
 global variables

 .section  ".text"

Space for global variables is generated one identifier at a time. An identifier id that occupies n bytes of storage with alignment multiple m is allocated as

.common id, n, m

Examples:

C code				 assembler directive
				.section	".data"
int x, a[12]; 			.common		x,4,4
				.common		a,48,4
char y;				.common		y,1,1
				.seg		".text"
Code should all be generated in the "text" segment (.seg "text"
).
Before starting a portion of code for a function foo, generate
 .align 4
 .global foo
 .type foo,#function
 .proc 020
foo:


Code and data can be intermixed, by switching segments back and forth.


Parameters



Parameters all occupy space on the stack and are generally pushed from
back to front; the first six parameters are, if they fit into 32 bits,
passed in registers instead. Data smaller than 4 bytes is passed in a
4 byte position on the stack; data larger than 4 bytes per parameter
is longword aligned.



Accessing an actual parameter from within the called function consists
of accessing register %in-1 ,
for parameters 1 through 6, or
loading memory [%fp + k], where k is 64 + the offset of the parameter
(64 + 4 * i for parameter i, if all parameters are passed as 4 bytes).

Size Conversions



The instruction ``ldsb src, reg'' loads and converts the source byte
in memory into a 32-bit register value. The value is sign-extended
(there is a corresponding unsigned operation).



A corresponding instruction, ``stdb reg, dest'' stores the low byte of
a 32-bit value into a single byte of memory, accomplishing the
conversion in the other direection.

Translating Assignment Statements



Note: for simplicity, the operations below are all translated in terms
of 32-bit operands. For character operations a ``b'' is appended to
the instruction; any C language mixed-type arithmetic should result in
explicit size conversions per the preceding section.



Most of these operands are using temporary registers, usually the %o
registers.

Global operands



Global values have addresses computed as constants at compile times;
such 32-bit values are loaded into a register

x := y + z set y, reg1
ld [reg1 ], reg1
set z, reg2
ld [reg2 ], reg2
add reg2 , reg1
set x, reg2 (if x is a global)
st reg1 , [reg2 ] (or mark reg1 as holding x)



Local operands



If in memory, locals and temporaries are accessed by displacing off of
%fp; x, y, and z in the code below are constants the compiler
computs and defines. Frequently, locals are already in registers
and require no load instructions.



x := y + z ld [%fp + y], reg1
ld [%fp + z], reg2
add reg2 , reg1
st reg1 , [%fp + x] (or mark reg1 as holding x)



Translating Jumps



A label is simply an identifier. They can be stored as integers in
intermediate code, and written out prefixed by "L". Goto's are simply
"branch always" instructions. Sparcs' extensive pipelining causes the
instruction following a jump (in the position called a delay slot) to be
executed before the jump takes place. For this reason nop instructions are
inserted into the delay slots after jumps, calls, and returns. Filling
these delay slots with productive computation instead of no-ops is often
easy; in particular, return instructions are normally followed by restore
instructions that reset the register window.



goto L ba L
if x op y goto L ld x, reg1
ld y, reg2
cmp reg1 , reg2
bcc L
nop




where cc is a condition code, one of



e equal
ne not equal
l less than
le less than or equal
ge greater than or equal
g greater




(The SPARC also has unsigned versions of these condition codes)

Arrays



Global Arrays



The value of the ith element x[i] of global array x, whose elements
are n bytes wide, is given (if i is also a global) by



x[i] set i, reg1
ld [reg1 ], reg1
sll reg1 ,2,reg1
set x, reg2
ld [reg2 + reg1]


Local Arrays



Computing the address of a local array is done by adding an offset to
the frame pointer.



x[i] set i, reg1
ld [reg1 ], reg1
sll reg1 ,2,reg1
add %fp,x,reg2
add reg1 ,reg2 ,reg1
ld [reg2 + reg1 ]



Procedures



Entering a Procedure



On entering a procedure, the first instruction is generally a save
instruction, enclosed in a cryptic !#PROLOGUE# directive.

 !#PROLOGUE# 0
 save %sp,-n,%sp
 !#PROLOGUE# 1


where n is the space need by the stack frame, computed as 64
+ space for locals and temporaries --- typically some number like -112.



Sun compilers also store something in %g1, GNU C does not.

Calling a Procedure



First, the parameters are loaded, from right to left, onto the stack
and (more frequently) into the %o registers. After parameters, a call
instruction followed by a nop do the actual call. For a call such as
f(c, i) the code is:



param i set i,%o2
ld [%o2], %o2
set c,%o1
ldsb [%o1], %o1
call f
nop


Return From a Procedure



On the SPARC, the return value is stored in %o0 (from the view of the
caller; the callee assigns to his %i0 in order to get it there).



return ret
restore
return x ld [%fp+x],%i0
& ret
& restore
分享到:
评论

相关推荐

    UML for the C programming language

    The paper by Bruce Powel Douglass, PhD, from IBM, delves into three main strategies for using UML with C: 1. **Object-Oriented Modeling** 2. **Object-Based Modeling** 3. **Functional-Based Modeling**...

    translating-orm-to-ar-online-web-sp-000

    目标:了解ActiveRecord如何为您抽象功能强大的方法。确定如何继承模型。本实验旨在向您展示ActiveRecord 。在spec/dog_spec.rb查看您的测试套件,现在有八个测试都失败了。过去,您必须单独编写每种方法才能通过每...

    tactics-for-translating-Neologisms资料PPT课件.ppt

    【标题】:“tactics-for-translating-Neologisms资料PPT课件.ppt”讨论的是新词翻译的策略 【描述】:该文件重点强调了汉译英在翻译领域中的重要性和短缺,以及新词(Neologisms)在语言发展中的角色 【标签】:...

    The Indispensable PC Hardware Book - rar - part1. (1/7)

    l CPU's from the 8086/8088 to the Pentium III and Athlon l Real, protected and virtual models l Windows and plug&play devices l CPU Clones from all major manufacturers l Chipsets and support chips...

    active-record-translating-from-orm-to-ar

    从ORM转换为活动记录 目标: 了解ActiveRecord如何为您抽象功能强大的方法。 确定如何继承模型。 指示 本实验旨在向您展示Active Record的强大功能。 在spec/dog_spec.rb查看您的测试套件,现在有八个测试都失败了...

    TsiLang Components Suite 5.2.3

    多语言支持控件 Five reasons why would you need TsiLang Components Suite: - You need to localize or internationalize your applications - You don‘t want to spend a lot of time for translating your ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: The TFlexPanel.FindControlAtPoint method maked virtual to realize RealTime-capability when on mouse cursor moving the flex-object search not occurs. - FIX: After deleting the selected points ...

    SAM分割模型onnx导出模型问题:Exporting the operator repeat-interleave to ON

    Exporting the operator repeat_interleave to ONNX opset version 11 is not supported TypeError: 'torch._C.Value' object is not iterable (Occurred when translating repeat_interleave). 问题解决: 1....

    Yahoo Messenger Translating Proxy-开源

    【Yahoo Messenger Translating Proxy-开源】 Yahoo Messenger Translating Proxy(YTP)是一个创新的开源项目,旨在为用户提供跨语言通信的能力。这个代理服务与Yahoo! Messenger 集成,实现了消息的自动翻译功能...

    Learning Swift - Second Edition 2016 PDF

    On the surface, Swift is easy to jump into, but it has complex underpinnings that are critical to becoming proficient at turning an idea into reality. This book is an approachable, step-by-step ...

    Translating UPC to EPC

    全球统一商品编码(Universal Product Code,简称UPC)是一种在零售点用来识别产品的条码系统,广泛应用于商品销售过程中。尽管UPC条码包含大量信息,但为了提高供应链的效率,行业正逐渐转向使用射频识别(RFID)...

    Decompiling Android.pdf

    2. **Chapter 4: Tools of the Trade** - An overview of popular decompilation tools and their capabilities, along with guidance on how to choose the right tool for specific needs. 3. **Chapter 5: ...

    cucu-a compiler you can understand

    In the realm of computer science and programming, compilers play a crucial role in translating human-readable source code into machine-executable binary code. CUCU, as described in the given document,...

    TECS-Software-Suite-2_5\Book\chapter00.pdf

    This sets the stage for what the book aims to achieve – guiding readers on a journey of understanding how computers work, breaking down complex problems into manageable modules, and developing large...

    Lerner -- Python Workout. 50 Essential Exercises -- 2020.pdf

    - **Objective:** Measure the time taken to execute a specific piece of code. - **Key Concepts:** - Using the `time` module to record start and end times. - Calculating elapsed time using ...

    Dede(反汇编Delphi)

    - Use the opcode-to-asm tool for translating intel opcode to assembler - Use RVA-to-PhysOffset tool for fast converting physical and RVA addresses - Use the DCU Dumper (view dcu2int.txt for more ...

Global site tag (gtag.js) - Google Analytics