What I learned from doing the OpenRISC GCC port, defining the stack frame
This is a continuation on my notes of things I learned while working on the
OpenRISC GCC backend port. The stack frame layout is very important to get
right when implementing an architecture’s calling conventions.
If not we may have a compiler that works fine with code it compiles but cannot
interoperate with libraries produced by another compiler.
For me I figured this would be most difficult as I am horrible with off by
one bugs. However, after
learning the whole picture I was able to get it working.
In this post we will go over the two main stack frame concepts:
Registers - The GCC internal and hard register numbers pointing into the stack
Stack Layout - How memory layout of the stack is defined
Registers
Stack registers are cpu registers dedicated to point to different locations in
the stack. The content of these registers is updated during function epilogue
and prologue. In the above diagram we can see the pointed out as AP, HFP,
FP and SP.
Virtual Registers
GCC’s first glimpse of the stack.
These are created during the expand and eliminated during vreg pass.
By looking at these we cat understand the whole picture: Offsets, outgoing
arguments, incoming arguments etc.
The virtual registers are GCC’s canonical view of the stack frame. During the vregs
pass they will be replaced with architecture specific registers. See details on
this in my discussion on GCC important passes.
Macro
GCC
OpenRISC
VIRTUAL_INCOMING_ARGS_REGNUM
Points to incoming arguments. ARG_POINTER_REGNUM + FIRST_PARM_OFFSET.
default
VIRTUAL_STACK_VARS_REGNUM
Points to local variables. FRAME_POINTER_REGNUM + TARGET_STARTING_FRAME_OFFSET.
default
VIRTUAL_STACK_DYNAMIC_REGNUM
STACK_POINTER_REGNUM + STACK_DYNAMIC_OFFSET.
default
VIRTUAL_OUTGOING_ARGS_REGNUM
Points to outgoing arguments. STACK_POINTER_REGNUM + STACK_POINTER_OFFSET.
default
Real Registers (Sometimes)
The stack pointer will pretty much always be a real register that shows up in the final
assembly. Other registers will be like virtuals and eliminated during some pass.
Macro
GCC
OpenRISC
STACK_POINTER_REGNUM
The hard stack pointer register, not defined where it should point
Points to the last data on the current stack frame. i.e. 0(r1) points next function arg[0]
FRAME_POINTER_REGNUM (FP)
Points to automatic/local variable storage
Points to the first local variable. i.e. 0(FP) points to local variable[0].
HARD_FRAME_POINTER_REGNUM
The hard frame pointer, not defined where it should point
Points to the same location as the previous functions SP. i.e. 0(r2) points to current function arg[0]
ARG_POINTER_REGNUM
Points to current function incoming arguments
For OpenRISC this is the same as HARD_FRAME_POINTER_REGNUM.
Stack Layout
Stack layout defines how the stack frame is placed in memory.
Eliminations
Eliminations provide the rules for which registers can be eliminated by
replacing them with another register and a calculated offset. The offset is
calculated by looking at data collected by the TARGET_COMPUTE_FRAME_LAYOUT
macro function.
On OpenRISC we have defined these below. We allow the frame pointer and
argument pointer to be eliminated. They will be replaced with either the stack
pointer register or the hard frame pointer. In OpenRISC there is no argument
pointer so it will always need to be eliminated. Also, the frame pointer is a
placeholder, when elimination is done it will be eliminated.
Note GCC knows that at some optimization levels the hard frame pointer will be
omitted. In these cases HARD_FRAME_POINTER_REGNUM will not selected as the
elimination target register. We don’t need to define any hard frame pointer
eliminations.
Macro
GCC
OpenRISC
ELIMINABLE_REGS
Sets of registers from, to which we can eliminate by calculating the difference between them.
We eliminate Argument Pointer and Frame Pointer.
INITIAL_ELIMINATION_OFFSET
Function to compute the difference between eliminable registers.
Some sections of the stack frame may contain multiple variables, for example we
may have multiple outgoing arguments or local variables. The order in which
these are stored in memory is defined by these macros.
Note On OpenRISC the local variables definition changed during implementation
from upwards to downwards. These are local only to the current function so does
not impact calling conventions.
For a new port is recommended to define FRAME_GROWS_DOWNWARD as 1 as it is
usually not critical to the target calling conventions and defining it also
enables the Stack Protector
feature. The stack protector can be turned on in gcc using -fstack-protector,
during build ensure to --enable-libssp which is enabled by default.
Macro
GCC
OpenRISC
STACK_GROWS_DOWNWARD
Define true if new stack frames decrease towards memory address 0x0.
1
FRAME_GROWS_DOWNWARD
Define true if increasing local variables are at negative offset from FP. Define this to enable the GCC stack protector feature.
1
ARGS_GROW_DOWNWARD
Define true if increasing function arguments are at negative offset from AP for incoming args and SP for outgoing args.
0 (default)
Stack Section Offsets
Offsets may be required if an architecture has extra offsets between the
different register pointers and the actual variable data. In OpenRISC we have
no such offsets.
Macro
GCC
OpenRISC
STACK_POINTER_OFFSET
See VIRTUAL_OUTGOING_ARGS_REGNUM
0
FIRST_PARM_OFFSET
See VIRTUAL_INCOMING_ARGS_REGNUM
0
STACK_DYNAMIC_OFFSET
See VIRTUAL_STACK_DYNAMIC_REGNUM
0
TARGET_STARTING_FRAME_OFFSET
See VIRTUAL_OUTGOING_ARGS_REGNUM
0
Outgoing Arguments
When a function calls another function sometimes the arguments to that function
will need to be stored to the stack before making the function call. For
OpenRISC this is when we have more arguments than fit in argument registers or
when we have variadic arguments. The outgoing
arguments for all child functions need to be accounted for and the space will be
allocated on the stack.
On some architectures outgoing arguments are pushed onto and popped off the
stack. For OpenRISC we do not do this we simply, allocate the required memory in
the prologue.
Macro
GCC
OpenRISC
ACCUMULATE_OUTGOING_ARGS
If defined, don’t push args just store in crtl->outgoing_args_size. Our prologue should allocate this space relative to the SP (as per ARGS_GROW_DOWNWARD).
1
CUMULATIVE_ARGS
A C type used for tracking args in the TARGET_FUNCTION_ARG_* macros.
int
INIT_CUMULATIVE_ARGS
Initializes a newly created CUMULALTIVE_ARGS type.
Sets the int variable to 0
TARGET_FUNCTION_ARG
Return a reg RTX or Zero to indicate when to start to pass outgoing args on the stack.
See implementation
FUNCTION_ARG_REGNO_P
Returns true of the given register number is used for passing outgoing function arguments.
r3 to r8 are OK for arguments
TARGET_FUNCTION_ARG_ADVANCE
This is called during iterating through outgoing function args to account for the next function arg size.
See implementation
Further Reading
These references were very helpful in getting our calling conventions right:
I realized early on that trouble shooting issues requires understanding the purpose
of some important compiler passes. It was difficult to understand what
all of the compiler passes were. There are so many, 200+, but after some time I found
there are a few key passes to be concerned about; lets jump in.
Quick Tips
When debugging compiler problems use the -fdump-rtl-all-all and
-fdump-tree-all-all flags to debug where things go wrong.
To understand which passes are run for different -On optimization levels
you can use -fdump-passes.
The numbers in the dump output files indicate the order in which passes were run. For
example between test.c.235r.vregs and test.c.234r.expand the expand pass is run
before vregs, and there were no passes run inbetween.
The debug options-S -dp are also helpful for tying RTL up with the output assembly.
The -S option tells the compiler to dump the assembler output, and -dp
enables annotation comments showing the RTL instruction id, name and other useful
statistics.
Glossary Terms
We may see cfg thoughout the gcc source, this is not configuration, but
control flow graph.
Spilling is performed when there are not enough registers available during register
allocation to store all scope variables, one variable in a register is chosen
and spilled by saving it to memory; thus freeing up a register for allocation.
IL is a GCC intermediate language i.e. GIMPLE or RTL. During porting we are
mainly concerned with RTL.
Lowering are operations done by passes to take higher level language
and graph representations and make them more simple/lower level in preparation
for machine assembly conversion.
Predicates part of the RTL these are used to facilitate instruction
matching the. Having these more specific reduces the work that reload needs to
do and generates better code.
Constraints part of the RTL and used during reload, these are associated
with assembly instructions used to resolved the target instruction.
Passes
Passes are the core of the compiler. To start, there are basically two types of
compiler passes in gcc:
Tree - Passes working on GIMPLE.
RTL - Passes working on Register Transfer Language.
The expand pass is defined in gcc/cfgexpand.c.
It will take the instruction names like addsi3 and movsi and expand them to
RTL instructions which will be refined by further passes.
Expand Input
Before RTL generation we have GIMPLE. Below is the content of func.c.232t.optimized the last
of the tree passes before RTL conversion.
An important tree pass is Static Single Assignment
(SSA) I don’t go into it here, but it is what makes us have so many variables, note that
each variable will be assigned only once, this helps simplify the tree for analysis
and later RTL steps like register allocation.
Expand Output
After expand we can first see the RTL. Each statement of the gimple above will
be represented by 1 or more RTL expressions. I have simplified the RTL a bit and
included the GIMPLE inline for clarity.
Tip Reading RTL. RTL is a lisp dialect. Each statement has the form (type id prev next n (statement)).
Here we can see that the previously seen variables stored to the frame at
virtual-stack-vars memory locations are now being stored to memory offsets of
an architecture specifc register. After the Virtual Registers pass all of the
virtual-* registers will be eliminated.
For OpenRISC we see ?fp, a fake register which we defined with macro
FRAME_POINTER_REGNUM. We use this as a placeholder as OpenRISC’s frame
pointer does not point to stack variables (it points to the function incoming
arguments). The placeholder is needed by GCC but it will be eliminated later.
On some arechitecture this will be a real register at this point.
The Split and Combine Passes
The Split passes use define_split definitions to look for RTL expressions
which cannot be handled by a single instruction on the target architecture.
These instructions are split into multiple RTL instructions. Splits patterns
are defined in our machine description file.
The Combine pass does the opposite. It looks for instructions that can be combined
into a signle instruction. Having tightly defined predicates will ensure incorrect
combines don’t happen.
The combine pass code is about 15,000 lines of code.
14950 gcc/combine.c
The IRA Pass
The IRA and LRA passes are some of the most complicated passes, they are
responsible to turning the psuedo register allocations which have been used up
to this point and assigning real registers.
We do not see many changes during the IRA pass in this example but it has prepared
us for the next step, LRA/reload.
The LRA Pass (Reload)
The Local Register
Allocator
pass replaced the reload pass which is still used by some targets. OpenRISC and
other modern ports use only LRA. The purpose of LRA/reload is to make sure each
RTL instruction has real registers and a real instruction to use for output. If
the criteria for an instruction is not met LRA/reload has some tricks to change
and instruction and “reload” it in order to get it to match the criteria.
During LRA/reload constraints are used to match the real target inscrutions, i.e.
"r" or "m" or target speciic ones like "O".
Before and after LRA/reload predicates are used to match RTL expressions, i.e
general_operand or target specific ones like reg_or_s16_operand.
If we look at a test.c.278r.reload dump file we will a few sections.
Local
Pseudo live ranges
Inheritance
Assignment
Repeat
********** Local #1: **********
...
0 Non-pseudo reload: reject+=2
0 Non input pseudo reload: reject++
Cycle danger: overall += LRA_MAX_REJECT
alt=0,overall=609,losers=1,rld_nregs=1
0 Non-pseudo reload: reject+=2
0 Non input pseudo reload: reject++
alt=1: Bad operand -- refuse
0 Non-pseudo reload: reject+=2
0 Non input pseudo reload: reject++
alt=2: Bad operand -- refuse
0 Non-pseudo reload: reject+=2
0 Non input pseudo reload: reject++
alt=3: Bad operand -- refuse
alt=4,overall=0,losers=0,rld_nregs=0
Choosing alt 4 in insn 2: (0) m (1) rO {*movsi_internal}
...
The above snippet of the Local phase of the LRA/reload pass shows the contraints
matching loop for RTL insn 2.
To understand what is going on we should look at what is insn 2, from our
input. This is a set instruction having a destination of memory and a source
of register type, or "m,r".
RTL from .md file of our *movsi_internal instruction. The alternatives are the
constraints, i.e. "=r,r,r,r, m,r".
The constraints matching interates over the alternatives. As we remember from above we are trying to match "m,r". We can see:
alt=0 - this shows 1 loser because alt 0 r,r vs m,r has one match and
one mismatch.
alt=1 - is indented and says Bad operand meaning there is no match at all with r,M vs m,r
alt=2 - is indented and says Bad operand meaning there is no match at all with r,K vs m,r
alt=3 - is indented and says Bad operand meaning there is no match at all with r,I vs m,r
alt=4 - is as win as we match m,rO vs m,r
After this we know exactly which target instructions for each RTL expression is neded.
End of Reload (LRA)
Finally we can see here at the end of LRA/reload all registers are real. The output
at this point is pretty much ready for assembly output.
Conclusion
We have walked some of the passes of GCC to better understand how it works.
During porting most of the problems will show up around expand, vregs and
reload passes. Its good to have a general idea of what these do and how
to read the dump files when troubleshooting. I hope the above helps.
News flash, the OpenRISC GCC port now can run “Hello World”
After about 4 months of development on the OpenRISC GCC port rewrite
I have hit my first major milestone, the “Hello World” program is working. Over those
4 months I spent about 2 months working on my from scratch dummy SMH port
then 2 months to get the OpenRISC port
to this stage.
Next Steps
There are still many todo items before this will be ready for general use, including:
Milestone 2 items
Investigate and Fix test suite failures, see below
Write OpenRISC specific test cases
Ensure all memory layout and calling conventions are within spec
Ensure sign extending, overflow, and carry flag arithmetic is correct
Fix issues with GDB debugging target remote is working OK, target sim is having issues.
Implement stubbed todo items, see below
Support for C++, I haven’t even tried to compile it yet
Milestone 3 items
Support for position independent code (PIC)
Support for thread local storage (TLS)
Support for floating point instructions (FPU)
Support for Atomic Builtins
Somewhere between milestone 2 and 3 I will start to work on getting the port
reviewed on the GCC and OpenRISC mailing lists. If anyone wants to review right
now please feel free to send feedback.
Test Suite Results
Running the gcc testsuite right now shows the following results. Many of these
look to be related to internal compiler errors.
=== gcc Summary ===
# of expected passes 84301
# of unexpected failures 5096
# of unexpected successes 3
# of expected failures 211
# of unresolved testcases 2821
# of unsupported tests 2630
/home/shorne/work/gnu-toolchain/build-gcc/gcc/xgcc version 9.0.0 20180426 (experimental) (GCC)
Stubbed TODO Items
Some of the stubbed todo items include:
Trampoline Handling
In gcc/config/or1k/or1k.h implement trampoline hooks for nested functions.
In gcc/config/or1k/or1k.c ensure what I am doing is right, on other targets
they copy the address onto the stack before returning.
/* TODO, do we need to just set to r9? or should we put it to where r9
is stored on the stack? */
void
or1k_expand_eh_return (rtx eh_addr)
{
emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), eh_addr);
}
I am working on an OpenRISC GCC port rewrite, here’s why.
For the past few years I have been working as a contributor to the
OpenRISC CPU project. My work has mainly been focused on
developing interest in the project by keeping the toolchains and software
maintained and pushing outstanding patches upstream.
I have made way getting Linux SMP support,
the GDB port, QEMU fixes and other
patches written, reviewed and committed to the upstream repositories.
However there is one project that has been an issue from the beginning; GCC.
OpenRISC has a mature GCC port started in early 2000s.
The issue is it is not upstream due to one early contributor not having signed
over his copyright. I decided to start with the rewrite. To do this I will:
Write a SMH dummy
architecture port following the ggx porting guide (moxie) guide.
Use that basic knowledge to start on the or1k port.
If you are interested please reach out on IRC or E-mail.
For the last year or so I have been working on getting a gdb port
upstreamed for OpenRISC. One thing one sometimes has to do when working
on gdb is to debug it. Debugging gdb with gdb could be a bit
confusing; hopefully these tips will help.
Setting the Prompt
Setting the prompt of the parent gdb will help so you know which gdb you
are in by looking at the command line. I do that with set prompt
(master:gdb) , (having space after the (master:gdb) is recommended).
Handling SIGINT
Handling ctrl-c is another thing we need to consider. If you are in your
inferior gdb and you press ctrl-c which gdb will you stop? The parent
gdb or the inferior gdb?
The parent gdb will be stopped. If we then continue the inferior will
continue. If we want to have the inferior stop as well we can set handle
SIGINT pass.
All together
An example session may look like the following
$ gdb or1k-elf-gdb
(gdb) set prompt (master:gdb)
(master:gdb) handle SIGINT
SIGINT is used by the debugger.
Are you sure you want to change it? (y or n) y
Signal Stop Print Pass to program Description
SIGINT Yes Yes No Interrupt
(master:gdb) handle SIGINT pass
SIGINT is used by the debugger.
Are you sure you want to change it? (y or n) y
Signal Stop Print Pass to program Description
SIGINT Yes Yes Yes Interrupt
(master:gdb) run
Starting program: /usr/local/or1k/bin/or1k-elf-gdb
(gdb) file loop.nelib
Reading symbols from loop.nelib...done.
(gdb) target sim
Connected to the simulator.
(gdb) load
Loading section .vectors, size 0x2000 lma 0x0
Loading section .init, size 0x28 lma 0x2000
Loading section .text, size 0x4f88 lma 0x2028
Loading section .fini, size 0x1c lma 0x6fb0
Loading section .rodata, size 0x18 lma 0x6fcc
Loading section .eh_frame, size 0x4 lma 0x8fe4
Loading section .ctors, size 0x8 lma 0x8fe8
Loading section .dtors, size 0x8 lma 0x8ff0
Loading section .jcr, size 0x4 lma 0x8ff8
Loading section .data, size 0xc74 lma 0x8ffc
Start address 0x100
Transfer rate: 254848 bits in <1 sec.
(gdb) run
Starting program: /home/shorne/work/openrisc/loop.nelib
loop
^C
Program received signal SIGINT, Interrupt.
or1k32bf_engine_run_fast (current_cpu=0x7fffee59c010) at mloop.c:577
577 if (! CPU_IDESC_SEM_INIT_P (current_cpu))
Missing separate debuginfos, use: dnf debuginfo-install expat-2.2.0-1.fc25.x86_64 libgcc-6.3.1-1.fc25.x86_64 libstdc++-6.3.1-1.fc25.x86_64 ncurses-libs-6.0-6.20160709.fc25.x86_64 python-libs-2.7.13-1.fc25.x86_64 xz-libs-5.2.2-2.fc24.x86_64 zlib-1.2.8-10.fc24.x86_64
(master:gdb) bt
#0 or1k32bf_engine_run_fast (current_cpu=0x7fffee59c010) at mloop.c:577
#1 0x0000000000654395 in engine_run_1 (fast_p=1, max_insns=<optimized out>, sd=0xd68a60) at ../../../binutils-gdb/sim/or1k/../common/cgen-run.c:191
#2 sim_resume (sd=0xd68a60, step=0, siggnal=<optimized out>) at ../../../binutils-gdb/sim/or1k/../common/cgen-run.c:108
#3 0x00000000004392d1 in gdbsim_wait (ops=<optimized out>, ptid=..., status=0x7fffffffc910, options=<optimized out>) at ../../binutils-gdb/gdb/remote-sim.c:1015
#4 0x0000000000600c6d in delegate_wait (self=<optimized out>, arg1=..., arg2=<optimized out>, arg3=<optimized out>) at ../../binutils-gdb/gdb/target-delegates.c:138
#5 0x000000000060ff64 in target_wait (ptid=..., status=status@entry=0x7fffffffc910, options=options@entry=0) at ../../binutils-gdb/gdb/target.c:2292
#6 0x000000000057e9d9 in do_target_wait (ptid=..., status=status@entry=0x7fffffffc910, options=0) at ../../binutils-gdb/gdb/infrun.c:3618
#7 0x0000000000589658 in fetch_inferior_event (client_data=<optimized out>) at ../../binutils-gdb/gdb/infrun.c:3910
#8 0x0000000000548b1c in check_async_event_handlers () at ../../binutils-gdb/gdb/event-loop.c:1064
#9 gdb_do_one_event () at ../../binutils-gdb/gdb/event-loop.c:326
#10 0x0000000000548c05 in gdb_do_one_event () at ../../binutils-gdb/gdb/common/common-exceptions.h:221
#11 start_event_loop () at ../../binutils-gdb/gdb/event-loop.c:371
#12 0x000000000059be78 in captured_command_loop (data=data@entry=0x0) at ../../binutils-gdb/gdb/main.c:325
#13 0x000000000054ab73 in catch_errors (func=func@entry=0x59be50 <captured_command_loop(void*)>, func_args=func_args@entry=0x0, errstring=errstring@entry=0x711a00 "", mask=mask@entry=RETURN_MASK_ALL)
at ../../binutils-gdb/gdb/exceptions.c:236
#14 0x000000000059cda6 in captured_main (data=0x7fffffffca60) at ../../binutils-gdb/gdb/main.c:1150
#15 gdb_main (args=args@entry=0x7fffffffcb90) at ../../binutils-gdb/gdb/main.c:1160
#16 0x000000000040c265 in main (argc=<optimized out>, argv=<optimized out>) at ../../binutils-gdb/gdb/gdb.c:32
(master:gdb) c
Continuing.
Program received signal SIGINT, Interrupt.
main () at loop.c:22
22 while (1) { ; }
(gdb) bt
#0 main () at loop.c:22
(gdb) l
17 tdata.str = "loop";
18 foo(tdata);
19
20 while (1) {
21 printf("%s\n", tdata.str);
22 while (1) { ; }
23 }
24 return 0;
25 }
(gdb) q
A debugging session is active.
Inferior 1 [process 42000] will be killed.
Quit anyway? (y or n) y
[Inferior 1 (process 24876) exited normally]
(master:gdb) q
Other Options
You could also remote debug gdb from a different terminal by using attach
to attach to and debug the secondary. But I find having everything in one
terminal nice.