-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathop.cpp
170 lines (144 loc) · 3.79 KB
/
mathop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2010-2021 Chris Spiegel.
//
// This file is part of Bocfel.
//
// Bocfel is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version
// 2 or 3, as published by the Free Software Foundation.
//
// Bocfel is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Bocfel. If not, see <http://www.gnu.org/licenses/>.
#include "mathop.h"
#include "branch.h"
#include "process.h"
#include "stack.h"
#include "types.h"
#include "util.h"
#include "zterp.h"
void zinc()
{
store_variable(zargs[0], variable(zargs[0]) + 1);
}
void zdec()
{
store_variable(zargs[0], variable(zargs[0]) - 1);
}
void znot()
{
store(~zargs[0]);
}
void zdec_chk()
{
int16_t newval;
int16_t val = as_signed(zargs[1]);
zdec();
// The z-spec 1.1 requires indirect variable references to the stack not to push/pop
if (zargs[0] == 0) {
newval = as_signed(*stack_top_element());
} else {
newval = as_signed(variable(zargs[0]));
}
branch_if(newval < val);
}
void zinc_chk()
{
int16_t newval;
int16_t val = as_signed(zargs[1]);
zinc();
// The z-spec 1.1 requires indirect variable references to the stack not to push/pop
if (zargs[0] == 0) {
newval = as_signed(*stack_top_element());
} else {
newval = as_signed(variable(zargs[0]));
}
branch_if(newval > val);
}
void ztest()
{
branch_if((zargs[0] & zargs[1]) == zargs[1]);
}
void zor()
{
store(zargs[0] | zargs[1]);
}
void zand()
{
store(zargs[0] & zargs[1]);
}
void zadd()
{
store(zargs[0] + zargs[1]);
}
void zsub()
{
store(zargs[0] - zargs[1]);
}
void zmul()
{
store(static_cast<uint32_t>(zargs[0]) * static_cast<uint32_t>(zargs[1]));
}
void zdiv()
{
ZASSERT(zargs[1] != 0, "divide by zero");
store(as_signed(zargs[0]) / as_signed(zargs[1]));
}
void zmod()
{
ZASSERT(zargs[1] != 0, "divide by zero");
store(as_signed(zargs[0]) % as_signed(zargs[1]));
}
void zlog_shift()
{
int16_t places = as_signed(zargs[1]);
// Shifting more than 15 bits is undefined (as of Standard 1.1), but
// do the most sensible thing.
if (places < -15 || places > 15) {
store(0);
return;
}
if (places < 0) {
store(zargs[0] >> -places);
} else {
store(zargs[0] << places);
}
}
void zart_shift()
{
int16_t number = as_signed(zargs[0]), places = as_signed(zargs[1]);
// Shifting more than 15 bits is undefined (as of Standard 1.1), but
// do the most sensible thing.
if (places < -15 || places > 15) {
store(number < 0 ? -1 : 0);
return;
}
// Shifting a negative value in C++ has some consequences:
// • Shifting a negative value left is undefined.
// • Shifting a negative value right is implementation defined.
//
// Thus these are done by hand. The Z-machine requires a right-shift
// of a negative value to propagate the sign bit. This is easily
// accomplished by complementing the value (yielding a positive
// number), shifting it right (zero filling), and complementing again
// (flip the shifted-in zeroes to ones).
//
// For a left-shift, the result should presumably be the same as a
// logical shift, so do that.
if (number < 0) {
if (places < 0) {
store(~(~number >> -places));
} else {
store(zargs[0] << places);
}
} else {
if (places < 0) {
store(zargs[0] >> -places);
} else {
store(zargs[0] << places);
}
}
}