summaryrefslogtreecommitdiff
path: root/src/luac/dump.c
blob: 479ce5d416e16d341bee5abd73e641e4acc6ddb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
** $Id: dump.c,v 1.20 1999/07/02 19:34:26 lhf Exp $
** save bytecodes to file
** See Copyright Notice in lua.h
*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "luac.h"

#ifdef OLD_ANSI
#define strerror(e)     "(no error message provided by operating system)"
#endif

#define DumpBlock(b,size,D)	fwrite(b,size,1,D)
#define	DumpInt			DumpLong

static void DumpWord(int i, FILE* D)
{
 int hi= 0x0000FF & (i>>8);
 int lo= 0x0000FF &  i;
 fputc(hi,D);
 fputc(lo,D);
}

static void DumpLong(long i, FILE* D)
{
 int hi= 0x00FFFF & (i>>16);
 int lo= 0x00FFFF & i;
 DumpWord(hi,D);
 DumpWord(lo,D);
}

static void DumpNumber(real x, FILE* D, int native, TProtoFunc* tf)
{
 if (native)
  DumpBlock(&x,sizeof(x),D);
 else
 {
  char b[256];
  int n;
  sprintf(b,NUMBER_FMT"%n",x,&n);
  luaU_str2d(b,tf->source->str);	/* help lundump not to fail */
  fputc(n,D);
  DumpBlock(b,n,D);
 }
}

static void DumpCode(TProtoFunc* tf, FILE* D)
{
 int size=luaU_codesize(tf);
 DumpLong(size,D);
 DumpBlock(tf->code,size,D);
}

static void DumpString(char* s, int size, FILE* D)
{
 if (s==NULL)
  DumpLong(0,D);
 else
 {
  DumpLong(size,D);
  DumpBlock(s,size,D);
 }
}

static void DumpTString(TaggedString* s, FILE* D)
{
 if (s==NULL)
  DumpString(NULL,0,D);
 else
  DumpString(s->str,s->u.s.len+1,D);
}

static void DumpLocals(TProtoFunc* tf, FILE* D)
{
 if (tf->locvars==NULL)
  DumpInt(0,D);
 else
 {
  LocVar* v;
  int n=0;
  for (v=tf->locvars; v->line>=0; v++)
   ++n;
  DumpInt(n,D);
  for (v=tf->locvars; v->line>=0; v++)
  {
   DumpInt(v->line,D);
   DumpTString(v->varname,D);
  }
 }
}

static void DumpFunction(TProtoFunc* tf, FILE* D, int native);

static void DumpConstants(TProtoFunc* tf, FILE* D, int native)
{
 int i,n=tf->nconsts;
 DumpInt(n,D);
 for (i=0; i<n; i++)
 {
  TObject* o=tf->consts+i;
  fputc(-ttype(o),D);			/* ttype(o) is negative - ORDER LUA_T */
  switch (ttype(o))
  {
   case LUA_T_NUMBER:
	DumpNumber(nvalue(o),D,native,tf);
	break;
   case LUA_T_STRING:
	DumpTString(tsvalue(o),D);
	break;
   case LUA_T_PROTO:
	DumpFunction(tfvalue(o),D,native);
	break;
   case LUA_T_NIL:
	break;
   default:				/* cannot happen */
	luaU_badconstant("dump",i,o,tf);
	break;
  }
 }
}

static void DumpFunction(TProtoFunc* tf, FILE* D, int native)
{
 DumpInt(tf->lineDefined,D);
 DumpTString(tf->source,D);
 DumpCode(tf,D);
 DumpLocals(tf,D);
 DumpConstants(tf,D,native);
 if (ferror(D))
  luaL_verror("write error" IN ": %s (errno=%d)",INLOC,strerror(errno),errno);
}

static void DumpHeader(TProtoFunc* Main, FILE* D, int native)
{
 fputc(ID_CHUNK,D);
 fputs(SIGNATURE,D);
 fputc(VERSION,D);
 if (native)
 {
  fputc(sizeof(real),D);
  DumpNumber(TEST_NUMBER,D,native,Main);
 }
 else
  fputc(0,D);
}

void luaU_dumpchunk(TProtoFunc* Main, FILE* D, int native)
{
 DumpHeader(Main,D,native);
 DumpFunction(Main,D,native);
}