summaryrefslogtreecommitdiff
path: root/drivers/staging/ktap/userspace/dump.c
blob: 088b08d0fa8e480aa48814d73f04d7ab5679b937 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * dump.c - save precompiled ktap chunks
 *
 * This file is part of ktap by Jovi Zhangwei.
 *
 * Copyright (C) 2012-2013 Jovi Zhangwei <jovi.zhangwei@gmail.com>.
 *
 * Copyright (C) 1994-2013 Lua.org, PUC-Rio.
 *  - The part of code in this file is copied from lua initially.
 *  - lua's MIT license is compatible with GPL.
 *
 * ktap is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * ktap is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../include/ktap_types.h"
#include "../include/ktap_opcodes.h"
#include "ktapc.h"


typedef struct {
	ktap_writer writer;
	void *data;
	int strip;
	int status;
} DumpState;

#define DumpMem(b, n, size, D)	DumpBlock(b, (n)*(size), D)
#define DumpVar(x, D)		DumpMem(&x, 1, sizeof(x), D)

static void DumpBlock(const void *b, size_t size, DumpState *D)
{
	if (D->status == 0)
		D->status = ((D->writer))(b, size, D->data);
}

static void DumpChar(int y, DumpState *D)
{
	char x = (char)y;
	DumpVar(x, D);
}

static void DumpInt(int x, DumpState *D)
{
	DumpVar(x, D);
}

static void DumpNumber(ktap_number x, DumpState *D)
{
	DumpVar(x,D);
}

static void DumpVector(const void *b, int n, size_t size, DumpState *D)
{
	DumpInt(n, D);
	DumpMem(b, n, size, D);
}

static void DumpString(const ktap_string *s, DumpState *D)
{
	if (s == NULL) {
		int size = 0;
		DumpVar(size, D);
	} else {
		int size = s->tsv.len + 1;		/* include trailing '\0' */
		DumpVar(size, D);
		DumpBlock(getstr(s), size * sizeof(char), D);
	}
}

#define DumpCode(f, D)	 DumpVector(f->code, f->sizecode, sizeof(ktap_instruction), D)

static void DumpFunction(const ktap_proto *f, DumpState *D);

static void DumpConstants(const ktap_proto *f, DumpState *D)
{
	int i, n = f->sizek;

	DumpInt(n, D);
	for (i = 0; i < n; i++) {
		const ktap_value* o=&f->k[i];
		DumpChar(ttypenv(o), D);
		switch (ttypenv(o)) {
		case KTAP_TNIL:
			break;
		case KTAP_TBOOLEAN:
			DumpChar(bvalue(o), D);
			break;
		case KTAP_TNUMBER:
			DumpNumber(nvalue(o), D);
			break;
		case KTAP_TSTRING:
			DumpString(rawtsvalue(o), D);
			break;
		default:
			printf("ktap: DumpConstants with unknown vaule type %d\n", ttypenv(o));
			ktap_assert(0);
		}
	}
	n = f->sizep;
	DumpInt(n, D);
	for (i = 0; i < n; i++)
		DumpFunction(f->p[i], D);
}

static void DumpUpvalues(const ktap_proto *f, DumpState *D)
{
	int i, n = f->sizeupvalues;

	DumpInt(n, D);
	for (i = 0; i < n; i++) {
		DumpChar(f->upvalues[i].instack, D);
		DumpChar(f->upvalues[i].idx, D);
	}
}

static void DumpDebug(const ktap_proto *f, DumpState *D)
{
	int i,n;

	DumpString((D->strip) ? NULL : f->source, D);
	n= (D->strip) ? 0 : f->sizelineinfo;
	DumpVector(f->lineinfo, n, sizeof(int), D);
	n = (D->strip) ? 0 : f->sizelocvars;
	DumpInt(n, D);

	for (i = 0; i < n; i++) {
		DumpString(f->locvars[i].varname, D);
		DumpInt(f->locvars[i].startpc, D);
		DumpInt(f->locvars[i].endpc, D);
	}
	n = (D->strip) ? 0 : f->sizeupvalues;
	DumpInt(n, D);
	for (i = 0; i < n; i++)
		DumpString(f->upvalues[i].name, D);
}

static void DumpFunction(const ktap_proto *f, DumpState *D)
{
	DumpInt(f->linedefined, D);
	DumpInt(f->lastlinedefined, D);
	DumpChar(f->numparams, D);
	DumpChar(f->is_vararg, D);
	DumpChar(f->maxstacksize, D);
	DumpCode(f, D);
	DumpConstants(f, D);
	DumpUpvalues(f, D);
	DumpDebug(f, D);
}

static void DumpHeader(DumpState *D)
{
	u8 h[KTAPC_HEADERSIZE];

	kp_header(h);
	DumpBlock(h, KTAPC_HEADERSIZE, D);
}

/*
 * dump ktap function as precompiled chunk
 */
int ktapc_dump(const ktap_proto *f, ktap_writer w, void *data, int strip)
{
	DumpState D;

	D.writer = w;
	D.data = data;
	D.strip = strip;
	D.status = 0;
	DumpHeader(&D);
	DumpFunction(f, &D);
	return D.status;
}