2 * dump.c - save precompiled ktap chunks
4 * This file is part of ktap by Jovi Zhangwei.
6 * Copyright (C) 2012-2013 Jovi Zhangwei <jovi.zhangwei@gmail.com>.
8 * Copyright (C) 1994-2013 Lua.org, PUC-Rio.
9 * - The part of code in this file is copied from lua initially.
10 * - lua's MIT license is compatible with GPL.
12 * ktap is free software; you can redistribute it and/or modify it
13 * under the terms and conditions of the GNU General Public License,
14 * version 2, as published by the Free Software Foundation.
16 * ktap is distributed in the hope it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 * You should have received a copy of the GNU General Public License along with
22 * this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "../include/ktap_types.h"
31 #include "../include/ktap_opcodes.h"
42 #define DumpMem(b, n, size, D) DumpBlock(b, (n)*(size), D)
43 #define DumpVar(x, D) DumpMem(&x, 1, sizeof(x), D)
45 static void DumpBlock(const void *b, size_t size, DumpState *D)
48 D->status = ((D->writer))(b, size, D->data);
51 static void DumpChar(int y, DumpState *D)
57 static void DumpInt(int x, DumpState *D)
62 static void DumpNumber(ktap_number x, DumpState *D)
67 static void DumpVector(const void *b, int n, size_t size, DumpState *D)
70 DumpMem(b, n, size, D);
73 static void DumpString(const ktap_string *s, DumpState *D)
79 int size = s->tsv.len + 1; /* include trailing '\0' */
81 DumpBlock(getstr(s), size * sizeof(char), D);
85 #define DumpCode(f, D) DumpVector(f->code, f->sizecode, sizeof(ktap_instruction), D)
87 static void DumpFunction(const ktap_proto *f, DumpState *D);
89 static void DumpConstants(const ktap_proto *f, DumpState *D)
94 for (i = 0; i < n; i++) {
95 const ktap_value* o=&f->k[i];
96 DumpChar(ttypenv(o), D);
101 DumpChar(bvalue(o), D);
104 DumpNumber(nvalue(o), D);
107 DumpString(rawtsvalue(o), D);
110 printf("ktap: DumpConstants with unknown vaule type %d\n", ttypenv(o));
116 for (i = 0; i < n; i++)
117 DumpFunction(f->p[i], D);
120 static void DumpUpvalues(const ktap_proto *f, DumpState *D)
122 int i, n = f->sizeupvalues;
125 for (i = 0; i < n; i++) {
126 DumpChar(f->upvalues[i].instack, D);
127 DumpChar(f->upvalues[i].idx, D);
131 static void DumpDebug(const ktap_proto *f, DumpState *D)
135 DumpString((D->strip) ? NULL : f->source, D);
136 n= (D->strip) ? 0 : f->sizelineinfo;
137 DumpVector(f->lineinfo, n, sizeof(int), D);
138 n = (D->strip) ? 0 : f->sizelocvars;
141 for (i = 0; i < n; i++) {
142 DumpString(f->locvars[i].varname, D);
143 DumpInt(f->locvars[i].startpc, D);
144 DumpInt(f->locvars[i].endpc, D);
146 n = (D->strip) ? 0 : f->sizeupvalues;
148 for (i = 0; i < n; i++)
149 DumpString(f->upvalues[i].name, D);
152 static void DumpFunction(const ktap_proto *f, DumpState *D)
154 DumpInt(f->linedefined, D);
155 DumpInt(f->lastlinedefined, D);
156 DumpChar(f->numparams, D);
157 DumpChar(f->is_vararg, D);
158 DumpChar(f->maxstacksize, D);
165 static void DumpHeader(DumpState *D)
167 u8 h[KTAPC_HEADERSIZE];
170 DumpBlock(h, KTAPC_HEADERSIZE, D);
174 * dump ktap function as precompiled chunk
176 int ktapc_dump(const ktap_proto *f, ktap_writer w, void *data, int strip)