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