Browse Source

Brevify

master
Přemysl Janouch 2 years ago
parent
commit
290e7087bb
Signed by: Přemysl Janouch <p.janouch@gmail.com> GPG Key ID: B715679E3A361BE6
1 changed files with 17 additions and 20 deletions
  1. 17
    20
      bfc-amd64-linux.c

+ 17
- 20
bfc-amd64-linux.c View File

@@ -163,6 +163,19 @@ optimize_assignment (struct instruction *irb, size_t irb_len)
163 163
 	return out;
164 164
 }
165 165
 
166
+// Add the offset of the LEFT/RIGHT instruction to the accumulator
167
+static bool
168
+add_direction_offset (struct instruction *irb, intptr_t *acc)
169
+{
170
+	if (irb->cmd == RIGHT)
171
+		*acc += irb->arg;
172
+	else if (irb->cmd == LEFT)
173
+		*acc -= (intptr_t) irb->arg;
174
+	else
175
+		return false;
176
+	return true;
177
+}
178
+
166 179
 // Add offsets to INC/DEC/SET stuck between LEFT/RIGHT
167 180
 // and compress the LEFT/RIGHT sequences
168 181
 static size_t
@@ -172,11 +185,7 @@ optimize_offseted_inc_dec (struct instruction *irb, size_t irb_len)
172 185
 	for (in = 0, out = 0; in < irb_len; in++, out++)
173 186
 	{
174 187
 		intptr_t dir = 0;
175
-		if (irb[in].cmd == RIGHT)
176
-			dir = irb[in].arg;
177
-		else if (irb[in].cmd == LEFT)
178
-			dir = -(intptr_t) irb[in].arg;
179
-		else
188
+		if (!add_direction_offset (&irb[in], &dir))
180 189
 		{
181 190
 			irb[out] = irb[in];
182 191
 			continue;
@@ -187,15 +196,9 @@ optimize_offseted_inc_dec (struct instruction *irb, size_t irb_len)
187 196
 			// An immediate offset has its limits on x86-64
188 197
 			if (dir < INT8_MIN || dir > INT8_MAX)
189 198
 				break;
190
-
191
-			intptr_t diff;
192
-			if (irb[in + 2].cmd == RIGHT)
193
-				diff = irb[in + 2].arg;
194
-			else if (irb[in + 2].cmd == LEFT)
195
-				diff = -(intptr_t) irb[in + 2].arg;
196
-			else
199
+			intptr_t diff = 0;
200
+			if (!add_direction_offset (&irb[in + 2], &diff))
197 201
 				break;
198
-
199 202
 			int cmd = irb[in + 1].cmd;
200 203
 			if (cmd != INC && cmd != DEC && cmd != SET)
201 204
 				break;
@@ -209,14 +212,8 @@ optimize_offseted_inc_dec (struct instruction *irb, size_t irb_len)
209 212
 		}
210 213
 
211 214
 		for (; in + 1 < irb_len; in++)
212
-		{
213
-			if (irb[in + 1].cmd == RIGHT)
214
-				dir += irb[in + 1].arg;
215
-			else if (irb[in + 1].cmd == LEFT)
216
-				dir -= (intptr_t) irb[in + 1].arg;
217
-			else
215
+			if (!add_direction_offset (&irb[in + 1], &dir))
218 216
 				break;
219
-		}
220 217
 
221 218
 		if (!dir)
222 219
 			out--;

Loading…
Cancel
Save