Mercurial > vba-linux
diff src/lua/print.c @ 11:27763b933818
raise lua sources up one level
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 11:07:39 -0600 |
parents | src/lua/src/print.c@f9f4f1b99eed |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/lua/print.c Sat Mar 03 11:07:39 2012 -0600 1.3 @@ -0,0 +1,227 @@ 1.4 +/* 1.5 +** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $ 1.6 +** print bytecodes 1.7 +** See Copyright Notice in lua.h 1.8 +*/ 1.9 + 1.10 +#include <ctype.h> 1.11 +#include <stdio.h> 1.12 + 1.13 +#define luac_c 1.14 +#define LUA_CORE 1.15 + 1.16 +#include "ldebug.h" 1.17 +#include "lobject.h" 1.18 +#include "lopcodes.h" 1.19 +#include "lundump.h" 1.20 + 1.21 +#define PrintFunction luaU_print 1.22 + 1.23 +#define Sizeof(x) ((int)sizeof(x)) 1.24 +#define VOID(p) ((const void*)(p)) 1.25 + 1.26 +static void PrintString(const TString* ts) 1.27 +{ 1.28 + const char* s=getstr(ts); 1.29 + size_t i,n=ts->tsv.len; 1.30 + putchar('"'); 1.31 + for (i=0; i<n; i++) 1.32 + { 1.33 + int c=s[i]; 1.34 + switch (c) 1.35 + { 1.36 + case '"': printf("\\\""); break; 1.37 + case '\\': printf("\\\\"); break; 1.38 + case '\a': printf("\\a"); break; 1.39 + case '\b': printf("\\b"); break; 1.40 + case '\f': printf("\\f"); break; 1.41 + case '\n': printf("\\n"); break; 1.42 + case '\r': printf("\\r"); break; 1.43 + case '\t': printf("\\t"); break; 1.44 + case '\v': printf("\\v"); break; 1.45 + default: if (isprint((unsigned char)c)) 1.46 + putchar(c); 1.47 + else 1.48 + printf("\\%03u",(unsigned char)c); 1.49 + } 1.50 + } 1.51 + putchar('"'); 1.52 +} 1.53 + 1.54 +static void PrintConstant(const Proto* f, int i) 1.55 +{ 1.56 + const TValue* o=&f->k[i]; 1.57 + switch (ttype(o)) 1.58 + { 1.59 + case LUA_TNIL: 1.60 + printf("nil"); 1.61 + break; 1.62 + case LUA_TBOOLEAN: 1.63 + printf(bvalue(o) ? "true" : "false"); 1.64 + break; 1.65 + case LUA_TNUMBER: 1.66 + printf(LUA_NUMBER_FMT,nvalue(o)); 1.67 + break; 1.68 + case LUA_TSTRING: 1.69 + PrintString(rawtsvalue(o)); 1.70 + break; 1.71 + default: /* cannot happen */ 1.72 + printf("? type=%d",ttype(o)); 1.73 + break; 1.74 + } 1.75 +} 1.76 + 1.77 +static void PrintCode(const Proto* f) 1.78 +{ 1.79 + const Instruction* code=f->code; 1.80 + int pc,n=f->sizecode; 1.81 + for (pc=0; pc<n; pc++) 1.82 + { 1.83 + Instruction i=code[pc]; 1.84 + OpCode o=GET_OPCODE(i); 1.85 + int a=GETARG_A(i); 1.86 + int b=GETARG_B(i); 1.87 + int c=GETARG_C(i); 1.88 + int bx=GETARG_Bx(i); 1.89 + int sbx=GETARG_sBx(i); 1.90 + int line=getline(f,pc); 1.91 + printf("\t%d\t",pc+1); 1.92 + if (line>0) printf("[%d]\t",line); else printf("[-]\t"); 1.93 + printf("%-9s\t",luaP_opnames[o]); 1.94 + switch (getOpMode(o)) 1.95 + { 1.96 + case iABC: 1.97 + printf("%d",a); 1.98 + if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b); 1.99 + if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c); 1.100 + break; 1.101 + case iABx: 1.102 + if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx); 1.103 + break; 1.104 + case iAsBx: 1.105 + if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx); 1.106 + break; 1.107 + } 1.108 + switch (o) 1.109 + { 1.110 + case OP_LOADK: 1.111 + printf("\t; "); PrintConstant(f,bx); 1.112 + break; 1.113 + case OP_GETUPVAL: 1.114 + case OP_SETUPVAL: 1.115 + printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-"); 1.116 + break; 1.117 + case OP_GETGLOBAL: 1.118 + case OP_SETGLOBAL: 1.119 + printf("\t; %s",svalue(&f->k[bx])); 1.120 + break; 1.121 + case OP_GETTABLE: 1.122 + case OP_SELF: 1.123 + if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } 1.124 + break; 1.125 + case OP_SETTABLE: 1.126 + case OP_ADD: 1.127 + case OP_SUB: 1.128 + case OP_MUL: 1.129 + case OP_DIV: 1.130 + case OP_POW: 1.131 + case OP_EQ: 1.132 + case OP_LT: 1.133 + case OP_LE: 1.134 + if (ISK(b) || ISK(c)) 1.135 + { 1.136 + printf("\t; "); 1.137 + if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); 1.138 + printf(" "); 1.139 + if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); 1.140 + } 1.141 + break; 1.142 + case OP_JMP: 1.143 + case OP_FORLOOP: 1.144 + case OP_FORPREP: 1.145 + printf("\t; to %d",sbx+pc+2); 1.146 + break; 1.147 + case OP_CLOSURE: 1.148 + printf("\t; %p",VOID(f->p[bx])); 1.149 + break; 1.150 + case OP_SETLIST: 1.151 + if (c==0) printf("\t; %d",(int)code[++pc]); 1.152 + else printf("\t; %d",c); 1.153 + break; 1.154 + default: 1.155 + break; 1.156 + } 1.157 + printf("\n"); 1.158 + } 1.159 +} 1.160 + 1.161 +#define SS(x) (x==1)?"":"s" 1.162 +#define S(x) x,SS(x) 1.163 + 1.164 +static void PrintHeader(const Proto* f) 1.165 +{ 1.166 + const char* s=getstr(f->source); 1.167 + if (*s=='@' || *s=='=') 1.168 + s++; 1.169 + else if (*s==LUA_SIGNATURE[0]) 1.170 + s="(bstring)"; 1.171 + else 1.172 + s="(string)"; 1.173 + printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n", 1.174 + (f->linedefined==0)?"main":"function",s, 1.175 + f->linedefined,f->lastlinedefined, 1.176 + S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f)); 1.177 + printf("%d%s param%s, %d slot%s, %d upvalue%s, ", 1.178 + f->numparams,f->is_vararg?"+":"",SS(f->numparams), 1.179 + S(f->maxstacksize),S(f->nups)); 1.180 + printf("%d local%s, %d constant%s, %d function%s\n", 1.181 + S(f->sizelocvars),S(f->sizek),S(f->sizep)); 1.182 +} 1.183 + 1.184 +static void PrintConstants(const Proto* f) 1.185 +{ 1.186 + int i,n=f->sizek; 1.187 + printf("constants (%d) for %p:\n",n,VOID(f)); 1.188 + for (i=0; i<n; i++) 1.189 + { 1.190 + printf("\t%d\t",i+1); 1.191 + PrintConstant(f,i); 1.192 + printf("\n"); 1.193 + } 1.194 +} 1.195 + 1.196 +static void PrintLocals(const Proto* f) 1.197 +{ 1.198 + int i,n=f->sizelocvars; 1.199 + printf("locals (%d) for %p:\n",n,VOID(f)); 1.200 + for (i=0; i<n; i++) 1.201 + { 1.202 + printf("\t%d\t%s\t%d\t%d\n", 1.203 + i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); 1.204 + } 1.205 +} 1.206 + 1.207 +static void PrintUpvalues(const Proto* f) 1.208 +{ 1.209 + int i,n=f->sizeupvalues; 1.210 + printf("upvalues (%d) for %p:\n",n,VOID(f)); 1.211 + if (f->upvalues==NULL) return; 1.212 + for (i=0; i<n; i++) 1.213 + { 1.214 + printf("\t%d\t%s\n",i,getstr(f->upvalues[i])); 1.215 + } 1.216 +} 1.217 + 1.218 +void PrintFunction(const Proto* f, int full) 1.219 +{ 1.220 + int i,n=f->sizep; 1.221 + PrintHeader(f); 1.222 + PrintCode(f); 1.223 + if (full) 1.224 + { 1.225 + PrintConstants(f); 1.226 + PrintLocals(f); 1.227 + PrintUpvalues(f); 1.228 + } 1.229 + for (i=0; i<n; i++) PrintFunction(f->p[i],full); 1.230 +}