stack_operations.py revision 7087:fb8d5786ff30
1# Copyright (c) 2007-2008 The Hewlett-Packard Development Company
2# All rights reserved.
3#
4# The license below extends only to copyright in the software and shall
5# not be construed as granting a license to any other intellectual
6# property including but not limited to intellectual property relating
7# to a hardware implementation of the functionality of the software
8# licensed hereunder.  You may use the software subject to the license
9# terms below provided that you ensure that this notice is replicated
10# unmodified and in its entirety in all distributions of the software,
11# modified or unmodified, in source code or in binary form.
12#
13# Redistribution and use in source and binary forms, with or without
14# modification, are permitted provided that the following conditions are
15# met: redistributions of source code must retain the above copyright
16# notice, this list of conditions and the following disclaimer;
17# redistributions in binary form must reproduce the above copyright
18# notice, this list of conditions and the following disclaimer in the
19# documentation and/or other materials provided with the distribution;
20# neither the name of the copyright holders nor the names of its
21# contributors may be used to endorse or promote products derived from
22# this software without specific prior written permission.
23#
24# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35#
36# Authors: Gabe Black
37
38microcode = '''
39def macroop POP_R {
40    # Make the default data size of pops 64 bits in 64 bit mode
41    .adjust_env oszIn64Override
42
43    ld t1, ss, [1, t0, rsp], dataSize=ssz
44    addi rsp, rsp, ssz, dataSize=asz
45    mov reg, reg, t1
46};
47
48def macroop POP_M {
49    # Make the default data size of pops 64 bits in 64 bit mode
50    .adjust_env oszIn64Override
51
52    ld t1, ss, [1, t0, rsp], dataSize=ssz
53    cda seg, sib, disp, dataSize=ssz
54    addi rsp, rsp, ssz, dataSize=asz
55    st t1, seg, sib, disp, dataSize=ssz
56};
57
58def macroop POP_P {
59    # Make the default data size of pops 64 bits in 64 bit mode
60    .adjust_env oszIn64Override
61
62    rdip t7
63    ld t1, ss, [1, t0, rsp], dataSize=ssz
64    cda seg, sib, disp, dataSize=ssz
65    addi rsp, rsp, ssz, dataSize=asz
66    st t1, seg, riprel, disp, dataSize=ssz
67};
68
69def macroop PUSH_R {
70    # Make the default data size of pops 64 bits in 64 bit mode
71    .adjust_env oszIn64Override
72
73    stupd reg, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
74};
75
76def macroop PUSH_I {
77    # Make the default data size of pops 64 bits in 64 bit mode
78    .adjust_env oszIn64Override
79
80    limm t1, imm
81    stupd t1, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
82};
83
84def macroop PUSH_M {
85    # Make the default data size of pops 64 bits in 64 bit mode
86    .adjust_env oszIn64Override
87
88    ld t1, seg, sib, disp, dataSize=ssz
89    stupd t1, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
90};
91
92def macroop PUSH_P {
93    # Make the default data size of pops 64 bits in 64 bit mode
94    .adjust_env oszIn64Override
95
96    rdip t7
97    ld t1, seg, riprel, disp, dataSize=ssz
98    stupd t1, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
99};
100
101def macroop PUSHA {
102    # Check all the stack addresses. We'll assume that if the beginning and
103    # end are ok, then the stuff in the middle should be as well.
104    cda ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
105    cda ss, [1, t0, rsp], "-8 * env.stackSize", dataSize=ssz
106    stupd rax, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
107    stupd rcx, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
108    stupd rdx, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
109    stupd rbx, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
110    stupd rsp, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
111    stupd rbp, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
112    stupd rsi, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
113    stupd rdi, ss, [1, t0, rsp], "-env.stackSize", dataSize=ssz
114};
115
116def macroop POPA {
117    # Check all the stack addresses. We'll assume that if the beginning and
118    # end are ok, then the stuff in the middle should be as well.
119    ld t1, ss, [1, t0, rsp], "0 * env.stackSize", dataSize=ssz
120    ld t2, ss, [1, t0, rsp], "7 * env.stackSize", dataSize=ssz
121    mov rdi, rdi, t1, dataSize=ssz
122    ld rsi, ss, [1, t0, rsp], "1 * env.stackSize", dataSize=ssz
123    ld rbp, ss, [1, t0, rsp], "2 * env.stackSize", dataSize=ssz
124    ld rbx, ss, [1, t0, rsp], "4 * env.stackSize", dataSize=ssz
125    ld rdx, ss, [1, t0, rsp], "5 * env.stackSize", dataSize=ssz
126    ld rcx, ss, [1, t0, rsp], "6 * env.stackSize", dataSize=ssz
127    mov rax, rax, t2, dataSize=ssz
128    addi rsp, rsp, "8 * env.stackSize", dataSize=asz
129};
130
131def macroop LEAVE {
132    # Make the default data size of pops 64 bits in 64 bit mode
133    .adjust_env oszIn64Override
134
135    mov t1, t1, rbp, dataSize=ssz
136    ld rbp, ss, [1, t0, t1], dataSize=ssz
137    mov rsp, rsp, t1, dataSize=ssz
138    addi rsp, rsp, ssz, dataSize=ssz
139};
140
141def macroop ENTER_I_I {
142    .adjust_env oszIn64Override
143    # This needs to check all the addresses it writes to before it actually
144    # writes any values.
145
146    # Pull the different components out of the immediate
147    limm t1, imm, dataSize=8
148    zexti t2, t1, 15, dataSize=8
149    srli t1, t1, 16, dataSize=8
150    zexti t1, t1, 5, dataSize=8
151    # t1 is now the masked nesting level, and t2 is the amount of storage.
152
153    # Push rbp.
154    stupd rbp, ss, [1, t0, rsp], "-env.dataSize"
155
156    # Save the stack pointer for later
157    mov t6, t6, rsp
158
159    # If the nesting level is zero, skip all this stuff.
160    sub t0, t1, t0, flags=(EZF,), dataSize=2
161    br label("skipLoop"), flags=(CEZF,)
162
163    # If the level was 1, only push the saved rbp
164    subi t0, t1, 1, flags=(EZF,)
165    br label("bottomOfLoop"), flags=(CEZF,)
166
167    limm t4, "ULL(-1)", dataSize=8
168topOfLoop:
169    ld t5, ss, [dsz, t4, rbp]
170    stupd t5, ss, [1, t0, rsp], "-env.dataSize"
171
172    # If we're not done yet, loop
173    subi t4, t4, 1, dataSize=8
174    add t0, t4, t1, flags=(EZF,)
175    br label("topOfLoop"), flags=(nCEZF,)
176
177bottomOfLoop:
178    # Push the old rbp onto the stack
179    stupd t6, ss, [1, t0, rsp], "-env.dataSize"
180
181skipLoop:
182    sub rsp, rsp, t2, dataSize=ssz
183    mov rbp, rbp, t6
184};
185'''
186