Browse code

(uxnasm) Combine byte literals

neauoire authored on 27/11/2021 21:55:33
Showing 1 changed files
... ...
@@ -47,6 +47,7 @@ typedef struct {
47 47
 } Program;
48 48
 
49 49
 Program p;
50
+static int litlast = 0;
50 51
 
51 52
 /* clang-format off */
52 53
 
... ...
@@ -184,6 +185,7 @@ writebyte(Uint8 b, int lit)
184 185
 	if(lit) writebyte(findopcode("LIT"), 0);
185 186
 	p.data[p.ptr++] = b;
186 187
 	p.length = p.ptr;
188
+	litlast = 0;
187 189
 }
188 190
 
189 191
 static void
... ...
@@ -194,6 +196,22 @@ writeshort(Uint16 s, int lit)
194 196
 	writebyte(s & 0xff, 0);
195 197
 }
196 198
 
199
+static void
200
+writelitbyte(Uint8 b)
201
+{
202
+	if(litlast) { /* combine literals */
203
+		Uint8 hb = p.data[p.ptr - 1];
204
+		p.ptr -= 2;
205
+		writeshort((hb << 8) + b, 1);
206
+		litlast = 0;
207
+		return;
208
+	}
209
+	p.data[p.ptr++] = findopcode("LIT");
210
+	p.data[p.ptr++] = b;
211
+	p.length = p.ptr;
212
+	litlast = 1;
213
+}
214
+
197 215
 static char *
198 216
 sublabel(char *src, char *scope, char *name)
199 217
 {
... ...
@@ -216,11 +234,9 @@ prefill(char *scope, char *label, Uint16 addr)
216 234
 static int
217 235
 tokenize(char *w, FILE *f)
218 236
 {
219
-	char word[64];
220
-	char subw[64];
221
-	char c;
222
-	Macro *m;
223 237
 	int i = 0;
238
+	char word[64], subw[64], c;
239
+	Macro *m;
224 240
 	if(slen(w) >= 63)
225 241
 		return error("Invalid token", w);
226 242
 	switch(w[0]) {
... ...
@@ -259,14 +275,17 @@ tokenize(char *w, FILE *f)
259 275
 		if(!sihx(w + 1) || (slen(w) != 3 && slen(w) != 5))
260 276
 			return error("Invalid hex literal", w);
261 277
 		if(slen(w) == 3)
262
-			writebyte(shex(w + 1), 1);
278
+			writelitbyte(shex(w + 1));
263 279
 		else if(slen(w) == 5)
264 280
 			writeshort(shex(w + 1), 1);
265 281
 		break;
266 282
 	case '.': /* literal byte zero-page */
283
+		prefill(p.scope, w, p.ptr - litlast);
284
+		writelitbyte(0xff);
285
+		break;
267 286
 	case ',': /* literal byte relative */
268
-		prefill(p.scope, w, p.ptr);
269
-		writebyte(0xff, 1);
287
+		prefill(p.scope, w, p.ptr - litlast);
288
+		writelitbyte(0xff);
270 289
 		break;
271 290
 	case ';': /* literal short absolute */
272 291
 		prefill(p.scope, w, p.ptr);