annotate src/lua/src/print.c @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $
rlm@1 3 ** print bytecodes
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7 #include <ctype.h>
rlm@1 8 #include <stdio.h>
rlm@1 9
rlm@1 10 #define luac_c
rlm@1 11 #define LUA_CORE
rlm@1 12
rlm@1 13 #include "ldebug.h"
rlm@1 14 #include "lobject.h"
rlm@1 15 #include "lopcodes.h"
rlm@1 16 #include "lundump.h"
rlm@1 17
rlm@1 18 #define PrintFunction luaU_print
rlm@1 19
rlm@1 20 #define Sizeof(x) ((int)sizeof(x))
rlm@1 21 #define VOID(p) ((const void*)(p))
rlm@1 22
rlm@1 23 static void PrintString(const TString* ts)
rlm@1 24 {
rlm@1 25 const char* s=getstr(ts);
rlm@1 26 size_t i,n=ts->tsv.len;
rlm@1 27 putchar('"');
rlm@1 28 for (i=0; i<n; i++)
rlm@1 29 {
rlm@1 30 int c=s[i];
rlm@1 31 switch (c)
rlm@1 32 {
rlm@1 33 case '"': printf("\\\""); break;
rlm@1 34 case '\\': printf("\\\\"); break;
rlm@1 35 case '\a': printf("\\a"); break;
rlm@1 36 case '\b': printf("\\b"); break;
rlm@1 37 case '\f': printf("\\f"); break;
rlm@1 38 case '\n': printf("\\n"); break;
rlm@1 39 case '\r': printf("\\r"); break;
rlm@1 40 case '\t': printf("\\t"); break;
rlm@1 41 case '\v': printf("\\v"); break;
rlm@1 42 default: if (isprint((unsigned char)c))
rlm@1 43 putchar(c);
rlm@1 44 else
rlm@1 45 printf("\\%03u",(unsigned char)c);
rlm@1 46 }
rlm@1 47 }
rlm@1 48 putchar('"');
rlm@1 49 }
rlm@1 50
rlm@1 51 static void PrintConstant(const Proto* f, int i)
rlm@1 52 {
rlm@1 53 const TValue* o=&f->k[i];
rlm@1 54 switch (ttype(o))
rlm@1 55 {
rlm@1 56 case LUA_TNIL:
rlm@1 57 printf("nil");
rlm@1 58 break;
rlm@1 59 case LUA_TBOOLEAN:
rlm@1 60 printf(bvalue(o) ? "true" : "false");
rlm@1 61 break;
rlm@1 62 case LUA_TNUMBER:
rlm@1 63 printf(LUA_NUMBER_FMT,nvalue(o));
rlm@1 64 break;
rlm@1 65 case LUA_TSTRING:
rlm@1 66 PrintString(rawtsvalue(o));
rlm@1 67 break;
rlm@1 68 default: /* cannot happen */
rlm@1 69 printf("? type=%d",ttype(o));
rlm@1 70 break;
rlm@1 71 }
rlm@1 72 }
rlm@1 73
rlm@1 74 static void PrintCode(const Proto* f)
rlm@1 75 {
rlm@1 76 const Instruction* code=f->code;
rlm@1 77 int pc,n=f->sizecode;
rlm@1 78 for (pc=0; pc<n; pc++)
rlm@1 79 {
rlm@1 80 Instruction i=code[pc];
rlm@1 81 OpCode o=GET_OPCODE(i);
rlm@1 82 int a=GETARG_A(i);
rlm@1 83 int b=GETARG_B(i);
rlm@1 84 int c=GETARG_C(i);
rlm@1 85 int bx=GETARG_Bx(i);
rlm@1 86 int sbx=GETARG_sBx(i);
rlm@1 87 int line=getline(f,pc);
rlm@1 88 printf("\t%d\t",pc+1);
rlm@1 89 if (line>0) printf("[%d]\t",line); else printf("[-]\t");
rlm@1 90 printf("%-9s\t",luaP_opnames[o]);
rlm@1 91 switch (getOpMode(o))
rlm@1 92 {
rlm@1 93 case iABC:
rlm@1 94 printf("%d",a);
rlm@1 95 if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b);
rlm@1 96 if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c);
rlm@1 97 break;
rlm@1 98 case iABx:
rlm@1 99 if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx);
rlm@1 100 break;
rlm@1 101 case iAsBx:
rlm@1 102 if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx);
rlm@1 103 break;
rlm@1 104 }
rlm@1 105 switch (o)
rlm@1 106 {
rlm@1 107 case OP_LOADK:
rlm@1 108 printf("\t; "); PrintConstant(f,bx);
rlm@1 109 break;
rlm@1 110 case OP_GETUPVAL:
rlm@1 111 case OP_SETUPVAL:
rlm@1 112 printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-");
rlm@1 113 break;
rlm@1 114 case OP_GETGLOBAL:
rlm@1 115 case OP_SETGLOBAL:
rlm@1 116 printf("\t; %s",svalue(&f->k[bx]));
rlm@1 117 break;
rlm@1 118 case OP_GETTABLE:
rlm@1 119 case OP_SELF:
rlm@1 120 if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
rlm@1 121 break;
rlm@1 122 case OP_SETTABLE:
rlm@1 123 case OP_ADD:
rlm@1 124 case OP_SUB:
rlm@1 125 case OP_MUL:
rlm@1 126 case OP_DIV:
rlm@1 127 case OP_POW:
rlm@1 128 case OP_EQ:
rlm@1 129 case OP_LT:
rlm@1 130 case OP_LE:
rlm@1 131 if (ISK(b) || ISK(c))
rlm@1 132 {
rlm@1 133 printf("\t; ");
rlm@1 134 if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
rlm@1 135 printf(" ");
rlm@1 136 if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
rlm@1 137 }
rlm@1 138 break;
rlm@1 139 case OP_JMP:
rlm@1 140 case OP_FORLOOP:
rlm@1 141 case OP_FORPREP:
rlm@1 142 printf("\t; to %d",sbx+pc+2);
rlm@1 143 break;
rlm@1 144 case OP_CLOSURE:
rlm@1 145 printf("\t; %p",VOID(f->p[bx]));
rlm@1 146 break;
rlm@1 147 case OP_SETLIST:
rlm@1 148 if (c==0) printf("\t; %d",(int)code[++pc]);
rlm@1 149 else printf("\t; %d",c);
rlm@1 150 break;
rlm@1 151 default:
rlm@1 152 break;
rlm@1 153 }
rlm@1 154 printf("\n");
rlm@1 155 }
rlm@1 156 }
rlm@1 157
rlm@1 158 #define SS(x) (x==1)?"":"s"
rlm@1 159 #define S(x) x,SS(x)
rlm@1 160
rlm@1 161 static void PrintHeader(const Proto* f)
rlm@1 162 {
rlm@1 163 const char* s=getstr(f->source);
rlm@1 164 if (*s=='@' || *s=='=')
rlm@1 165 s++;
rlm@1 166 else if (*s==LUA_SIGNATURE[0])
rlm@1 167 s="(bstring)";
rlm@1 168 else
rlm@1 169 s="(string)";
rlm@1 170 printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
rlm@1 171 (f->linedefined==0)?"main":"function",s,
rlm@1 172 f->linedefined,f->lastlinedefined,
rlm@1 173 S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f));
rlm@1 174 printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
rlm@1 175 f->numparams,f->is_vararg?"+":"",SS(f->numparams),
rlm@1 176 S(f->maxstacksize),S(f->nups));
rlm@1 177 printf("%d local%s, %d constant%s, %d function%s\n",
rlm@1 178 S(f->sizelocvars),S(f->sizek),S(f->sizep));
rlm@1 179 }
rlm@1 180
rlm@1 181 static void PrintConstants(const Proto* f)
rlm@1 182 {
rlm@1 183 int i,n=f->sizek;
rlm@1 184 printf("constants (%d) for %p:\n",n,VOID(f));
rlm@1 185 for (i=0; i<n; i++)
rlm@1 186 {
rlm@1 187 printf("\t%d\t",i+1);
rlm@1 188 PrintConstant(f,i);
rlm@1 189 printf("\n");
rlm@1 190 }
rlm@1 191 }
rlm@1 192
rlm@1 193 static void PrintLocals(const Proto* f)
rlm@1 194 {
rlm@1 195 int i,n=f->sizelocvars;
rlm@1 196 printf("locals (%d) for %p:\n",n,VOID(f));
rlm@1 197 for (i=0; i<n; i++)
rlm@1 198 {
rlm@1 199 printf("\t%d\t%s\t%d\t%d\n",
rlm@1 200 i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
rlm@1 201 }
rlm@1 202 }
rlm@1 203
rlm@1 204 static void PrintUpvalues(const Proto* f)
rlm@1 205 {
rlm@1 206 int i,n=f->sizeupvalues;
rlm@1 207 printf("upvalues (%d) for %p:\n",n,VOID(f));
rlm@1 208 if (f->upvalues==NULL) return;
rlm@1 209 for (i=0; i<n; i++)
rlm@1 210 {
rlm@1 211 printf("\t%d\t%s\n",i,getstr(f->upvalues[i]));
rlm@1 212 }
rlm@1 213 }
rlm@1 214
rlm@1 215 void PrintFunction(const Proto* f, int full)
rlm@1 216 {
rlm@1 217 int i,n=f->sizep;
rlm@1 218 PrintHeader(f);
rlm@1 219 PrintCode(f);
rlm@1 220 if (full)
rlm@1 221 {
rlm@1 222 PrintConstants(f);
rlm@1 223 PrintLocals(f);
rlm@1 224 PrintUpvalues(f);
rlm@1 225 }
rlm@1 226 for (i=0; i<n; i++) PrintFunction(f->p[i],full);
rlm@1 227 }