aboutsummaryrefslogtreecommitdiff
path: root/lua.stx
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-10-06 12:51:11 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-10-06 12:51:11 -0200
commitf6a9cc9a673298dc7ee7a416fec08848e464b227 (patch)
tree4e060d0a52f8a3eecf2fe4850e7124340e21daf7 /lua.stx
parent28d47a0aaa646f8762085cc7fcf8953b62df0927 (diff)
downloadlua-f6a9cc9a673298dc7ee7a416fec08848e464b227.tar.gz
lua-f6a9cc9a673298dc7ee7a416fec08848e464b227.tar.bz2
lua-f6a9cc9a673298dc7ee7a416fec08848e464b227.zip
jumps are relative to next instruction
Diffstat (limited to 'lua.stx')
-rw-r--r--lua.stx68
1 files changed, 38 insertions, 30 deletions
diff --git a/lua.stx b/lua.stx
index 8fd89517..28df6f36 100644
--- a/lua.stx
+++ b/lua.stx
@@ -1,6 +1,6 @@
1%{ 1%{
2/* 2/*
3** $Id: lua.stx,v 1.6 1997/09/26 15:02:26 roberto Exp roberto $ 3** $Id: lua.stx,v 1.7 1997/10/01 20:05:34 roberto Exp roberto $
4** Syntax analizer and code generator 4** Syntax analizer and code generator
5** See Copyright Notice in lua.h 5** See Copyright Notice in lua.h
6*/ 6*/
@@ -135,31 +135,6 @@ static void code_word (int n)
135} 135}
136 136
137 137
138static int fix_opcode (int pc, OpCode op, int n)
139{
140 if (n <= 255) {
141 currState->f->code[pc] = op;
142 currState->f->code[pc+1] = n;
143 return 0;
144 }
145 else {
146 check_pc(1); /* open space */
147 movecode_up(pc+1, pc, currState->pc-pc);
148 currState->pc++;
149 currState->f->code[pc] = op+1; /* opcode must be word variant */
150 code_word_at(pc+1, n);
151 return 1;
152 }
153}
154
155static int fix_jump (int pc, OpCode op, int n)
156{
157 n -= pc+1; /* jump is relative to position following jump opcode */
158 if (n > 255) n++; /* jump must be 1 bigger */
159 return fix_opcode(pc, op, n);
160}
161
162
163static void deltastack (int delta) 138static void deltastack (int delta)
164{ 139{
165 currState->stacksize += delta; 140 currState->stacksize += delta;
@@ -501,6 +476,39 @@ static int lua_codestore (int i, int left)
501} 476}
502 477
503 478
479static int fix_opcode (int pc, OpCode op, int n)
480{
481 if (n <= 255) {
482 currState->f->code[pc] = op;
483 currState->f->code[pc+1] = n;
484 return 0;
485 }
486 else {
487 check_pc(1); /* open space */
488 movecode_up(pc+1, pc, currState->pc-pc);
489 currState->pc++;
490 currState->f->code[pc] = op+1; /* opcode must be word variant */
491 code_word_at(pc+1, n);
492 return 1;
493 }
494}
495
496
497static int fix_jump (int pc, OpCode op, int n)
498{
499 /* jump is relative to position following jump instruction */
500 return fix_opcode(pc, op, n-(pc+JMPSIZE));
501}
502
503
504static void fix_upjmp (OpCode op, int pos)
505{
506 int delta = currState->pc+JMPSIZE - pos; /* jump is relative */
507 if (delta > 255) delta++;
508 code_opborw(op, delta, 0);
509}
510
511
504static void codeIf (int thenAdd, int elseAdd) 512static void codeIf (int thenAdd, int elseAdd)
505{ 513{
506 int elseinit = elseAdd+JMPSIZE; 514 int elseinit = elseAdd+JMPSIZE;
@@ -516,7 +524,7 @@ static void codeIf (int thenAdd, int elseAdd)
516 524
517static void code_shortcircuit (OpCode op, int pos) 525static void code_shortcircuit (OpCode op, int pos)
518{ 526{
519 int dist = currState->pc - (pos+1); 527 int dist = currState->pc - (pos+JMPSIZE);
520 if (dist > 255) 528 if (dist > 255)
521 luaY_error("and/or expression too long"); 529 luaY_error("and/or expression too long");
522 currState->f->code[pos] = op; 530 currState->f->code[pos] = op;
@@ -578,7 +586,6 @@ static void init_func (void)
578} 586}
579 587
580 588
581
582static TProtoFunc *close_func (void) 589static TProtoFunc *close_func (void)
583{ 590{
584 TProtoFunc *f = currState->f; 591 TProtoFunc *f = currState->f;
@@ -681,12 +688,13 @@ stat : IF cond THEN block SaveWord elsepart END
681 &currState->f->code[$2], expsize); 688 &currState->f->code[$2], expsize);
682 movecode_down($2, $3, currState->pc-$2); 689 movecode_down($2, $3, currState->pc-$2);
683 newpos += fix_jump($2, JMPB, currState->pc-expsize); 690 newpos += fix_jump($2, JMPB, currState->pc-expsize);
684 code_opborw(IFTUPJMPB, currState->pc+1 - newpos, 0); 691 fix_upjmp(IFTUPJMPB, newpos);
685 }} 692 }}
686 693
687 | REPEAT GetPC block UNTIL expr1 694 | REPEAT GetPC block UNTIL expr1
688 { 695 {
689 code_opborw(IFFUPJMPB, currState->pc+1 - $2, -1); 696 fix_upjmp(IFFUPJMPB, $2);
697 deltastack(-1); /* pops condition */
690 } 698 }
691 699
692 | varlist1 '=' exprlist1 700 | varlist1 '=' exprlist1