`

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
分享到:
评论

相关推荐

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

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

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

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

    The Design and Implementation of an ASN. 1-C

    the protocol implementors from the arduous work of translating protocol- defined data-types and constructing their encoders and decoders. Given an ASN.l protocol specification, CASNl automatically ...

    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 ...

    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 ...

    Cross-platform.Localization.for.Native.Mobile.Apps.with.Xamarin

    the author demonstrates how to adapt it for the Android, iOS, and Windows platforms, showing how to handle the localization with resource files and how to handle internationalization on each platform...

    The Works of Archimedes - Netz

    经典数学著作-Archimeds数学原著,Netz翻译(In 1996 when as a postdoc Reviel Netz launched his project of ..., was ideal preparation for working on the palimpsest. “I was incredibly lucky,” he says.)

    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 ...

    delphi反编译工具 dede3.99

    - 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 ...

    Yahoo Messenger Translating Proxy-开源

    Yahoo翻译HTTP代理(YTP)是与Yahoo!一起使用的双向翻译器。 Messenger可以将您键入的消息和回复与各种语言进行互译。 它支持英语,德语,西班牙语,法语,意大利语和葡萄牙语。

    Learning.Swift.1784392502

    Discover and optimize the features of Swift to write cleaner and better code This is a step-by-step guide full of practical examples to create efficient IOS applications Who This Book Is For If you ...

    A pracitcal introduction to hardware software codesign

    Part I Basic Concepts 1 The Nature of Hardware and Software . . . . . . . . ....1.1 Introducing ...3.5 Application: Translating C to Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

    端口查看工具

    to automatically resize the columns according to the text length on every refresh. * Version 1.81: o Added more include/exclude filter options in the context menu of CurrPorts. * Version 1.80: ...

    Packt.Swift.Developing.iOS.Applications.2016

    Develop an app running on the cloud to act as an API server for your client's apps Dive into the core components of Swift 2 including operators, collections, control flow, and functions Create and use...

    Introduction to JVM Languages

    Anyone who knows software development knows about the Java Virtual Machine (JVM), it is responsible for interpreting Java byte code and translating it into actions. In the beginning, Java was the only...

    Practical IPv6 for Windows Administrators

    Practical IPv6 for Windows Administrators is a handy guide to implementing IPv6 in a Microsoft Windows environment. This is the book you need if you are a Microsoft Windows Administrator confronted ...

Global site tag (gtag.js) - Google Analytics