stack_operations.py revision 5176:43fb805e1b85
1# Copyright (c) 2007 The Hewlett-Packard Development Company
2# All rights reserved.
3#
4# Redistribution and use of this software in source and binary forms,
5# with or without modification, are permitted provided that the
6# following conditions are met:
7#
8# The software must be used only for Non-Commercial Use which means any
9# use which is NOT directed to receiving any direct monetary
10# compensation for, or commercial advantage from such use.  Illustrative
11# examples of non-commercial use are academic research, personal study,
12# teaching, education and corporate research & development.
13# Illustrative examples of commercial use are distributing products for
14# commercial advantage and providing services using the software for
15# commercial advantage.
16#
17# If you wish to use this software or functionality therein that may be
18# covered by patents for commercial use, please contact:
19#     Director of Intellectual Property Licensing
20#     Office of Strategy and Technology
21#     Hewlett-Packard Company
22#     1501 Page Mill Road
23#     Palo Alto, California  94304
24#
25# Redistributions of source code must retain the above copyright notice,
26# this list of conditions and the following disclaimer.  Redistributions
27# in binary form must reproduce the above copyright notice, this list of
28# conditions and the following disclaimer in the documentation and/or
29# other materials provided with the distribution.  Neither the name of
30# the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
31# contributors may be used to endorse or promote products derived from
32# this software without specific prior written permission.  No right of
33# sublicense is granted herewith.  Derivatives of the software and
34# output created using the software may be prepared, but only for
35# Non-Commercial Uses.  Derivatives of the software may be shared with
36# others provided: (i) the others agree to abide by the list of
37# conditions herein which includes the Non-Commercial Use restrictions;
38# and (ii) such Derivatives of the software include the above copyright
39# notice to acknowledge the contribution from this software where
40# applicable, this list of conditions and the disclaimer below.
41#
42# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53#
54# Authors: Gabe Black
55
56microcode = '''
57def macroop POP_R {
58    # Make the default data size of pops 64 bits in 64 bit mode
59    .adjust_env oszIn64Override
60
61    ld reg, ss, [1, t0, rsp]
62    addi rsp, rsp, dsz
63};
64
65def macroop POP_M {
66    # Make the default data size of pops 64 bits in 64 bit mode
67    .adjust_env oszIn64Override
68
69    ld t1, ss, [1, t0, rsp]
70    # Check stack address
71    addi rsp, rsp, dsz
72    st t1, seg, sib, disp
73};
74
75def macroop POP_P {
76    # Make the default data size of pops 64 bits in 64 bit mode
77    .adjust_env oszIn64Override
78
79    rdip t7
80    ld t1, ss, [1, t0, rsp]
81    # Check stack address
82    addi rsp, rsp, dsz
83    st t1, seg, riprel, disp
84};
85
86def macroop PUSH_R {
87    # Make the default data size of pops 64 bits in 64 bit mode
88    .adjust_env oszIn64Override
89
90    # This needs to work slightly differently from the other versions of push
91    # because the -original- version of the stack pointer is what gets pushed
92    stupd reg, ss, [1, t0, rsp], "-env.dataSize"
93};
94
95def macroop PUSH_I {
96    # Make the default data size of pops 64 bits in 64 bit mode
97    .adjust_env oszIn64Override
98
99    limm t1, imm
100    stupd t1, ss, [1, t0, rsp], "-env.dataSize"
101};
102
103def macroop PUSH_M {
104    # Make the default data size of pops 64 bits in 64 bit mode
105    .adjust_env oszIn64Override
106
107    ld t1, seg, sib, disp
108    stupd t1, ss, [1, t0, rsp], "-env.dataSize"
109};
110
111def macroop PUSH_P {
112    # Make the default data size of pops 64 bits in 64 bit mode
113    .adjust_env oszIn64Override
114
115    rdip t7
116    ld t1, seg, riprel, disp
117    stupd t1, ss, [1, t0, rsp], "-env.dataSize"
118};
119
120def macroop PUSHA {
121    # Check all the stack addresses.
122    stupd rax, ss, [1, t0, rsp], "-env.dataSize"
123    stupd rcx, ss, [1, t0, rsp], "-env.dataSize"
124    stupd rdx, ss, [1, t0, rsp], "-env.dataSize"
125    stupd rbx, ss, [1, t0, rsp], "-env.dataSize"
126    stupd rsp, ss, [1, t0, rsp], "-env.dataSize"
127    stupd rbp, ss, [1, t0, rsp], "-env.dataSize"
128    stupd rsi, ss, [1, t0, rsp], "-env.dataSize"
129    stupd rdi, ss, [1, t0, rsp], "-env.dataSize"
130};
131
132def macroop POPA {
133    # Check all the stack addresses.
134    ld rdi, ss, [1, t0, rsp], "0 * env.dataSize"
135    ld rsi, ss, [1, t0, rsp], "1 * env.dataSize"
136    ld rbp, ss, [1, t0, rsp], "2 * env.dataSize"
137    ld rbx, ss, [1, t0, rsp], "4 * env.dataSize"
138    ld rdx, ss, [1, t0, rsp], "5 * env.dataSize"
139    ld rcx, ss, [1, t0, rsp], "6 * env.dataSize"
140    ld rax, ss, [1, t0, rsp], "7 * env.dataSize"
141    addi rsp, rsp, "8 * env.dataSize"
142};
143
144def macroop LEAVE {
145    # Make the default data size of pops 64 bits in 64 bit mode
146    .adjust_env oszIn64Override
147
148    mov t1, t1, rbp
149    ld rbp, ss, [1, t0, t1]
150    mov rsp, rsp, t1
151    addi rsp, rsp, dsz
152};
153
154def macroop ENTER_I_I {
155    # This needs to check all the addresses it writes to before it actually
156    # writes any values.
157
158    # Pull the different components out of the immediate
159    limm t1, imm
160    zext t2, t1, 16, dataSize=2
161    srl t1, t1, 16
162    zext t1, t1, 6
163    # t1 is now the masked nesting level, and t2 is the amount of storage.
164
165    # Push rbp.
166    stupd rbp, ss, [1, t0, rsp], "-env.dataSize"
167
168    # Save the stack pointer for later
169    mov t6, t6, rsp
170
171    # If the nesting level is zero, skip all this stuff.
172    subi t0, t1, t0, flags=(EZF,), dataSize=2
173    bri t0, label("skipLoop"), flags=(CEZF,)
174
175    # If the level was 1, only push the saved rbp
176    subi t0, t1, 1, flags=(EZF,)
177    bri t0, label("bottomOfLoop"), flags=(CEZF,)
178
179    limm t4, "ULL(-1)", dataSize=8
180topOfLoop:
181    ld t5, ss, [dsz, t4, rbp]
182    stupd t5, ss, [1, t0, rsp], "-env.dataSize"
183
184    # If we're not done yet, loop
185    subi t4, t4, 1, dataSize=8
186    add t0, t4, t1, flags=(EZF,)
187    bri t0, label("topOfLoop"), flags=(nCEZF,)
188
189bottomOfLoop:
190    # Push the old rbp onto the stack
191    stupd t6, ss, [1, t0, rsp], "-env.dataSize"
192
193skipLoop:
194    sub rsp, rsp, t2
195    mov rbp, rbp, t6
196};
197'''
198