stack_operations.py revision 5661:443e6f925027
1# Copyright (c) 2007-2008 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 t1, ss, [1, t0, rsp], dataSize=ssz
62    addi rsp, rsp, ssz, dataSize=asz
63    mov reg, reg, t1
64};
65
66def macroop POP_M {
67    # Make the default data size of pops 64 bits in 64 bit mode
68    .adjust_env oszIn64Override
69
70    ld t1, ss, [1, t0, rsp], dataSize=ssz
71    cda seg, sib, disp, dataSize=ssz
72    addi rsp, rsp, ssz, dataSize=asz
73    st t1, seg, sib, disp, dataSize=ssz
74};
75
76def macroop POP_P {
77    # Make the default data size of pops 64 bits in 64 bit mode
78    .adjust_env oszIn64Override
79
80    rdip t7
81    ld t1, ss, [1, t0, rsp], dataSize=ssz
82    cda seg, sib, disp, dataSize=ssz
83    addi rsp, rsp, ssz, dataSize=asz
84    st t1, seg, riprel, disp, dataSize=ssz
85};
86
87def macroop PUSH_R {
88    # Make the default data size of pops 64 bits in 64 bit mode
89    .adjust_env oszIn64Override
90
91    stupd reg, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
92};
93
94def macroop PUSH_I {
95    # Make the default data size of pops 64 bits in 64 bit mode
96    .adjust_env oszIn64Override
97
98    limm t1, imm
99    stupd t1, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
100};
101
102def macroop PUSH_M {
103    # Make the default data size of pops 64 bits in 64 bit mode
104    .adjust_env oszIn64Override
105
106    ld t1, seg, sib, disp, dataSize=ssz
107    stupd t1, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
108};
109
110def macroop PUSH_P {
111    # Make the default data size of pops 64 bits in 64 bit mode
112    .adjust_env oszIn64Override
113
114    rdip t7
115    ld t1, seg, riprel, disp, dataSize=ssz
116    stupd t1, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
117};
118
119def macroop PUSHA {
120    # Check all the stack addresses. We'll assume that if the beginning and
121    # end are ok, then the stuff in the middle should be as well.
122    cda ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
123    cda ss, [1, t0, rsp], "-8 * env.stackSize", dataSize=ssz
124    stupd rax, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
125    stupd rcx, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
126    stupd rdx, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
127    stupd rbx, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
128    stupd rsp, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
129    stupd rbp, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
130    stupd rsi, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
131    stupd rdi, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
132};
133
134def macroop POPA {
135    # Check all the stack addresses. We'll assume that if the beginning and
136    # end are ok, then the stuff in the middle should be as well.
137    ld t1, ss, [1, t0, rsp], "0 * env.stackSize", dataSize=ssz
138    ld t2, ss, [1, t0, rsp], "7 * env.stackSize", dataSize=ssz
139    mov rdi, rdi, t1, dataSize=ssz
140    ld rsi, ss, [1, t0, rsp], "1 * env.stackSize", dataSize=ssz
141    ld rbp, ss, [1, t0, rsp], "2 * env.stackSize", dataSize=ssz
142    ld rbx, ss, [1, t0, rsp], "4 * env.stackSize", dataSize=ssz
143    ld rdx, ss, [1, t0, rsp], "5 * env.stackSize", dataSize=ssz
144    ld rcx, ss, [1, t0, rsp], "6 * env.stackSize", dataSize=ssz
145    mov rax, rax, t2, dataSize=ssz
146    addi rsp, rsp, "8 * env.stackSize", dataSize=asz
147};
148
149def macroop LEAVE {
150    # Make the default data size of pops 64 bits in 64 bit mode
151    .adjust_env oszIn64Override
152
153    mov t1, t1, rbp, dataSize=asz
154    ld rbp, ss, [1, t0, t1], dataSize=ssz
155    mov rsp, rsp, t1, dataSize=asz
156    addi rsp, rsp, ssz, dataSize=asz
157};
158
159def macroop ENTER_I_I {
160    # This needs to check all the addresses it writes to before it actually
161    # writes any values.
162
163    # Pull the different components out of the immediate
164    limm t1, imm
165    zexti t2, t1, 15, dataSize=2
166    srl t1, t1, 16
167    zexti t1, t1, 5
168    # t1 is now the masked nesting level, and t2 is the amount of storage.
169
170    # Push rbp.
171    stupd rbp, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
172
173    # Save the stack pointer for later
174    mov t6, t6, rsp, dataSize=asz
175
176    # If the nesting level is zero, skip all this stuff.
177    subi t0, t1, t0, flags=(EZF,), dataSize=2
178    br label("skipLoop"), flags=(CEZF,)
179
180    # If the level was 1, only push the saved rbp
181    subi t0, t1, 1, flags=(EZF,)
182    br label("bottomOfLoop"), flags=(CEZF,)
183
184    limm t4, "ULL(-1)", dataSize=8
185topOfLoop:
186    ld t5, ss, [ssz, t4, rbp], dataSize=ssz
187    stupd t5, ss, [1, t0, rsp], "-env.stackSize"
188
189    # If we're not done yet, loop
190    subi t4, t4, 1, dataSize=8
191    add t0, t4, t1, flags=(EZF,)
192    br label("topOfLoop"), flags=(nCEZF,)
193
194bottomOfLoop:
195    # Push the old rbp onto the stack
196    stupd t6, ss, [1, t0, rsp], "-env.stackSize"
197
198skipLoop:
199    sub rsp, rsp, t2, dataSize=asz
200    mov rbp, rbp, t6, dataSize=asz
201};
202'''
203