stack_operations.py revision 5171:eab735dc951d
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    st reg, ss, [1, t0, rsp], "-env.dataSize"
93    subi rsp, rsp, dsz
94};
95
96def macroop PUSH_I {
97    # Make the default data size of pops 64 bits in 64 bit mode
98    .adjust_env oszIn64Override
99
100    limm t1, imm
101    st t1, ss, [1, t0, rsp], "-env.dataSize"
102    subi rsp, rsp, dsz
103};
104
105def macroop PUSH_M {
106    # Make the default data size of pops 64 bits in 64 bit mode
107    .adjust_env oszIn64Override
108
109    ld t1, seg, sib, disp
110    st t1, ss, [1, t0, rsp], "-env.dataSize"
111    subi rsp, rsp, dsz
112};
113
114def macroop PUSH_P {
115    # Make the default data size of pops 64 bits in 64 bit mode
116    .adjust_env oszIn64Override
117
118    rdip t7
119    ld t1, seg, riprel, disp
120    # Check stack address
121    subi rsp, rsp, dsz
122    st t1, ss, [1, t0, rsp]
123};
124
125def macroop PUSHA {
126    # Check all the stack addresses.
127    st rax, ss, [1, t0, rsp], "-0 * env.dataSize"
128    st rcx, ss, [1, t0, rsp], "-1 * env.dataSize"
129    st rdx, ss, [1, t0, rsp], "-2 * env.dataSize"
130    st rbx, ss, [1, t0, rsp], "-3 * env.dataSize"
131    st rsp, ss, [1, t0, rsp], "-4 * env.dataSize"
132    st rbp, ss, [1, t0, rsp], "-5 * env.dataSize"
133    st rsi, ss, [1, t0, rsp], "-6 * env.dataSize"
134    st rdi, ss, [1, t0, rsp], "-7 * env.dataSize"
135    subi rsp, rsp, "8 * env.dataSize"
136};
137
138def macroop POPA {
139    # Check all the stack addresses.
140    ld rdi, ss, [1, t0, rsp], "0 * env.dataSize"
141    ld rsi, ss, [1, t0, rsp], "1 * env.dataSize"
142    ld rbp, ss, [1, t0, rsp], "2 * env.dataSize"
143    ld rbx, ss, [1, t0, rsp], "4 * env.dataSize"
144    ld rdx, ss, [1, t0, rsp], "5 * env.dataSize"
145    ld rcx, ss, [1, t0, rsp], "6 * env.dataSize"
146    ld rax, ss, [1, t0, rsp], "7 * env.dataSize"
147    addi rsp, rsp, "8 * env.dataSize"
148};
149
150def macroop LEAVE {
151    # Make the default data size of pops 64 bits in 64 bit mode
152    .adjust_env oszIn64Override
153
154    mov t1, t1, rbp
155    ld rbp, ss, [1, t0, t1]
156    mov rsp, rsp, t1
157    addi rsp, rsp, dsz
158};
159
160def macroop ENTER_I_I {
161    # This needs to check all the addresses it writes to before it actually
162    # writes any values.
163
164    # Pull the different components out of the immediate
165    limm t1, imm
166    zext t2, t1, 16, dataSize=2
167    srl t1, t1, 16
168    zext t1, t1, 6
169    # t1 is now the masked nesting level, and t2 is the amount of storage.
170
171    # Push rbp.
172    st rbp, ss, [1, t0, rsp], "-env.dataSize"
173    subi rsp, rsp, dsz
174
175    # Save the stack pointer for later
176    mov t6, t6, rsp
177
178    # If the nesting level is zero, skip all this stuff.
179    subi t0, t1, t0, flags=(EZF,), dataSize=2
180    bri t0, label("skipLoop"), flags=(CEZF,)
181
182    # If the level was 1, only push the saved rbp
183    subi t0, t1, 1, flags=(EZF,)
184    bri t0, label("bottomOfLoop"), flags=(CEZF,)
185
186    limm t4, "ULL(-1)", dataSize=8
187topOfLoop:
188    ld t5, ss, [dsz, t4, rbp]
189    st t5, ss, [1, t0, rsp], "-env.dataSize"
190    subi rsp, rsp, dsz
191
192    # If we're not done yet, loop
193    subi t4, t4, 1, dataSize=8
194    add t0, t4, t1, flags=(EZF,)
195    bri t0, label("topOfLoop"), flags=(nCEZF,)
196
197bottomOfLoop:
198    # Push the old rbp onto the stack
199    st t6, ss, [1, t0, rsp], "-env.dataSize"
200    subi rsp, rsp, dsz
201
202skipLoop:
203    sub rsp, rsp, t2
204    mov rbp, rbp, t6
205};
206'''
207