Skip to content

Commit 7f6beeb

Browse files
committed
add: 添加luf的函数头定义,添加luf最终大小的估算, 比luac大13%左右
1 parent 74600d8 commit 7f6beeb

File tree

5 files changed

+408
-61
lines changed

5 files changed

+408
-61
lines changed

include/luat_luac_report.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define LUAT_LUAC_REPORT_T
33

44
typedef struct luac_data {
5-
size_t i; // 文件索引
5+
size_t id; // 文件索引
66
union
77
{
88
char* data;
@@ -28,6 +28,7 @@ typedef struct luac_data_group {
2828

2929
// 实现一个与Lua原始function平替的结构体, 用于存储函数信息
3030
typedef struct luf_func {
31+
size_t id;
3132
lu_byte numparams; /* number of fixed parameters */
3233
lu_byte is_vararg;
3334
lu_byte maxstacksize; /* number of registers needed by this function */
@@ -74,7 +75,7 @@ typedef struct luac_file {
7475
}luac_file_t;
7576

7677
typedef struct TIO {
77-
const char* ptr;
78+
char* ptr;
7879
const char* begin;
7980
}tio_t;
8081

include/luat_luf.h

+41
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,47 @@
44
#include "luat_base.h"
55
#include "luat_luac_report.h"
66

7+
// 定义一下luf的存储结构
8+
9+
typedef struct luf_file {
10+
uint8_t magic[4];
11+
uint32_t version;
12+
uint32_t size;
13+
uint32_t crc;
14+
}luf_file_t;
15+
16+
typedef struct luf_func_head {
17+
lu_byte magic;
18+
lu_byte numparams; /* number of fixed parameters */
19+
lu_byte is_vararg;
20+
lu_byte maxstacksize; /* number of registers needed by this function */
21+
uint32_t size; // 总大小,包含头部
22+
uint32_t offset; // 偏移,相对于luf_file_t的起始位置
23+
uint32_t sizecode; // 代码指令
24+
uint32_t sizep; /* size of 'p' 子函数数量 */
25+
uint32_t sizek; /* size of 'k' 常量表 */
26+
uint32_t sizeupvalues; /* size of 'upvalues' */
27+
uint32_t sizeupvalues2; /* size of 'upvalues'的名称信息 */
28+
uint32_t sizelineinfo; // 行号数据
29+
uint32_t sizelocvars; // 局部变量信息和名称信息
30+
uint32_t linedefined; /* debug information */
31+
uint32_t lastlinedefined; /* debug information */
32+
33+
// 后续存放的内容:
34+
// 1. code指令, 实际长度为 sizecode * 4 字节
35+
// 2. 子函数数量id索引 sizep * 4 字节
36+
// 3. k 常量表 sizek * 4 字节
37+
// 4. upvalues sizeupvalues * 2 字节
38+
// 5. upvalues的名称信息 sizeupvalues2 * 4 字节
39+
// 6. 行号数据 sizelineinfo * 4 字节
40+
// 7. 局部变量信息 sizelocvars * 8 字节
41+
// 8. 局部变量的名称信息 sizelocvars * 4 字节
42+
43+
// 所以总长度等于
44+
// sizecode * 4 + sizep * 4 + sizek * 4 + sizeupvalues * 2 + sizeupvalues2 * 4 + sizelineinfo * 4 + sizelocvars * 8 + sizelocvars * 4
45+
}luf_func_head_t;
46+
747
void luat_luf_toluac(luac_file_t *cf);
48+
void luac_to_luf(luac_file_t *cfs, size_t count, luac_report_t *rpt);
849

950
#endif

port/luat_luac_report.c

+23-55
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
static luf_func_t * LoadFunction(luac_file_t *cf, tio_t* tio, size_t id);
1515

1616
static void LoadBlock (tio_t* t, void *b, size_t size) {
17-
memcpy(b, t->ptr, size);
17+
memcpy((char*)b, t->ptr, size);
1818
t->ptr += size;
1919
}
2020

@@ -127,7 +127,7 @@ static void LoadConstants (tio_t *tio, luac_file_t* cf, luf_func_t *f) {
127127
case LUA_TLNGSTR:
128128
rpt->strs.data[rpt->strs.count].data = LoadString(tio, &len);
129129
rpt->strs.data[rpt->strs.count].len = len;
130-
rpt->strs.data[rpt->strs.count].type = LUA_TLNGSTR;
130+
rpt->strs.data[rpt->strs.count].type = t;
131131
f->k[i] = &rpt->strs.data[rpt->strs.count];
132132
rpt->strs.count ++;
133133
break;
@@ -150,20 +150,12 @@ static void LoadUpvalues (tio_t *tio, luac_file_t* cf, luf_func_t *f) {
150150
}
151151
f->upvalues = luat_heap_malloc(n * sizeof(lu_byte) * 2);
152152
LoadBlock(tio, f->upvalues, n * sizeof(lu_byte) * 2);
153-
// LLOGD("upvalue数量 %d", n);
154-
// for (i = 0; i < n; i++) {
155-
// // f->upvalues[i].instack = LoadByte(S);
156-
// // f->upvalues[i].idx = LoadByte(S);
157-
// f->upvalues[i*2] = LoadByte(tio);
158-
// f->upvalues[i*2 + 1] = LoadByte(tio);
159-
// }
160153
}
161154

162155
static void LoadProtos (tio_t *tio, luac_file_t *cf, luf_func_t *f) {
163156
int i;
164157
int n = LoadInt(tio);
165158
f->sizep = n;
166-
// LLOGD("子函数数量 %d", n);
167159
for (i = 0; i < n; i++) {
168160
f->p[i] = LoadFunction(cf, tio, cf->report->func_count);
169161
}
@@ -201,6 +193,7 @@ static void LoadDebug (tio_t *tio, luac_file_t *cf, luf_func_t *f) {
201193
// 变量名称
202194
rpt->strs.data[rpt->strs.count].data = LoadString(tio, &len);
203195
rpt->strs.data[rpt->strs.count].len = len;
196+
rpt->strs.data[rpt->strs.count].type = len <= 41 ? LUA_TSTRING : LUA_TLNGSTR;
204197
f->locvars2[i] = &rpt->strs.data[rpt->strs.count];
205198
rpt->strs.count ++;
206199
f->locvars[i*2] = LoadInt(tio);
@@ -215,6 +208,7 @@ static void LoadDebug (tio_t *tio, luac_file_t *cf, luf_func_t *f) {
215208
for (i = 0; i < n; i++) {
216209
rpt->strs.data[rpt->strs.count].data = LoadString(tio, &len);
217210
rpt->strs.data[rpt->strs.count].len = len;
211+
rpt->strs.data[rpt->strs.count].type = len <= 41 ? LUA_TSTRING : LUA_TLNGSTR;
218212
f->upvalues2[i] = &rpt->strs.data[rpt->strs.count];
219213
rpt->strs.count ++;
220214
// LLOGD("debug信息: upvalue变量名长度 %d", len);
@@ -234,7 +228,7 @@ static luf_func_t * LoadFunction(luac_file_t *cf, tio_t* tio, size_t id) {
234228
// char* tmp = NULL;
235229
rpt->strs.data[rpt->strs.count].data = LoadString(tio, &len);
236230
rpt->strs.data[rpt->strs.count].len = len;
237-
rpt->strs.data[rpt->strs.count].type = LUA_TSTRING;
231+
rpt->strs.data[rpt->strs.count].type = len <= 41 ? LUA_TSTRING : LUA_TLNGSTR;
238232
f->source = &rpt->strs.data[rpt->strs.count];
239233
rpt->strs.count++;
240234
// f->source = LoadString(S, f);
@@ -249,8 +243,8 @@ static luf_func_t * LoadFunction(luac_file_t *cf, tio_t* tio, size_t id) {
249243
rpt->codes.data[rpt->codes.count].data = LoadCode(tio, &len);
250244
rpt->codes.data[rpt->codes.count].len = len;
251245
f->code = &rpt->codes.data[rpt->codes.count];
252-
f->sizecode = len;
253-
// LLOGD("函数[%d]代码大小 %d", id, len / 4);
246+
f->sizecode = len / 4;
247+
// LLOGD("函数[%d]代码大小 %d", id, len);
254248
rpt->codes.count ++;
255249
// cf->g_report->func_count ++;
256250

@@ -260,29 +254,12 @@ static luf_func_t * LoadFunction(luac_file_t *cf, tio_t* tio, size_t id) {
260254
// LLOGD("啥情况");
261255
LoadDebug(tio, cf, f);
262256
// LLOGD("啥情况2");
263-
264-
if (f->sizep) {
265-
// LLOGD("存在子函数 %d", f->sizep);
266-
// f->p = luat_heap_malloc(sizeof(void*) * f->sizep);
267-
// memset(f->p, 0, sizeof(void*) * f->sizep);
268-
// LLOGD("函数信息 id=%d %p", id, &cf->report->luf_funcs[id]);
269-
// LLOGD("函数信息 id=%d %p", id+1, &cf->report->luf_funcs[id+1]);
270-
for (size_t i = 0; i < f->sizep; i++)
271-
{
272-
// LLOGD("设置函数引用 %d %p %p %p %p", i, f, f->p[i], &cf->report->luf_funcs[id], &cf->report->luf_funcs[id + i]);
273-
// f->p[i] = &cf->report->luf_funcs[id + i + 1];
274-
// LLOGD("子函数关联 %-2d -> %-2d", id + i + 1, id);
275-
}
276-
}
277-
else {
278-
// LLOGD("没有子函数了");
279-
}
280257
// LLOGD("===================<<<< %d", id);
281258
return f;
282259
}
283260

284261
int luadb_do_report_file(luac_file_t *cf, const char* data) {
285-
LLOGD("分析文件%s %p", cf->source_file, data);
262+
// LLOGD("分析文件%s %p", cf->source_file, data);
286263
// char tmpbuff[8] = {0};
287264
const char* ptr = data;
288265
// 首先, 分析头部
@@ -329,7 +306,7 @@ int luadb_do_report_file(luac_file_t *cf, const char* data) {
329306
ptr ++;
330307

331308

332-
tio_t tio = {.ptr = ptr};
309+
tio_t tio = {.ptr = ptr, .begin = ptr};
333310
LoadFunction(cf, &tio, 0);
334311

335312
// LLOGD("分析完成");
@@ -448,15 +425,6 @@ int luadb_do_report(luat_luadb2_ctx_t *ctx) {
448425
cfs[i].report = &rpts[i];
449426
cfs[i].ptr = ctx->fs->files[i].ptr;
450427
luadb_do_report_file(&cfs[i], ctx->fs->files[i].ptr);
451-
452-
// 函数数据检查
453-
for (size_t i = 0; i < 1024; i++)
454-
{
455-
// if (cfs[i].report->luf_funcs[i].sizek){
456-
// LLOGD("函数数据 %d 存在, 指针位置: 0x%p", i, &cfs[i].report->luf_funcs[i] );
457-
// }
458-
}
459-
460428
}
461429
else {
462430
LLOGD("跳过非lua文件 %s", ctx->fs->files[i].name);
@@ -486,16 +454,16 @@ int luadb_do_report(luat_luadb2_ctx_t *ctx) {
486454
for (size_t i = 0; i < ctx->fs->filecount; i++)
487455
{
488456
if (cfs[i].report) {
489-
luat_luf_toluac(&cfs[i]);
457+
// luat_luf_toluac(&cfs[i]);
490458
}
491459
}
492-
460+
luac_to_luf(&cfs[0], ctx->fs->filecount, report);
493461

494462
// LLOGD("==============================================================");
495463
// LLOGD(" 分类汇总报告");
496464
// LLOGD("==============================================================\r\n");
497465
// 分类报告
498-
466+
#if 0
499467
// 文件信息表格
500468
size_t file_max_len = 0;
501469
size_t file_total_len = 0;
@@ -523,22 +491,22 @@ int luadb_do_report(luat_luadb2_ctx_t *ctx) {
523491
// LLOGD("| %-30s | %6d | %s |", "==avg==", file_total_len / (ctx->fs->filecount > 0 ? ctx->fs->filecount : 1), "平均");
524492
LLOGD("==============================================================\r\n");
525493

526-
// luac_data_report_print_group(cfs, ctx->fs->filecount, report, 0, "字符串/二进制数据统计");
527-
// luac_data_report_print_group(cfs, ctx->fs->filecount, report, 1, "函数代码统计");
494+
luac_data_report_print_group(cfs, ctx->fs->filecount, report, 0, "字符串/二进制数据统计");
495+
luac_data_report_print_group(cfs, ctx->fs->filecount, report, 1, "函数代码统计");
528496
// luac_data_report_print_group(cfs, ctx->fs->filecount, report, 2, "常量数值统计");
529497
// luac_data_report_print_group(cfs, ctx->fs->filecount, report, 3, "upvalue统计");
530498
// luac_data_report_print_group(cfs, ctx->fs->filecount, report, 4, "代码行数映射信息统计");
531499

532500
// 打印一下函数对象
533-
size_t func_count = 0;
534-
for (size_t i = 0; i < 1024; i++)
535-
{
536-
if (report->luf_funcs[i].sizecode > 0) {
537-
func_count ++;
538-
}
539-
}
501+
// size_t func_count = 0;
502+
// for (size_t i = 0; i < 1024; i++)
503+
// {
504+
// if (report->luf_funcs[i].sizecode > 0) {
505+
// func_count ++;
506+
// }
507+
// }
540508
// LLOGD("函数实例的总数 %d %d", func_count, report->func_count);
541-
542-
509+
#endif
510+
LLOGD("ALL Done");
543511
return 0;
544512
}

port/luat_luf_main.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
#include "luat_log.h"
1212

1313
static void DumpByte(tio_t *io, lu_byte c) {
14-
memcpy(io->ptr, &c, 1);
14+
memcpy(io->ptr, (char*)&c, 1);
1515
io->ptr += 1;
1616
}
1717

1818
static void DumpBlock(tio_t *io, void *b, size_t size) {
19-
memcpy(io->ptr, b, size);
19+
memcpy(io->ptr, (char*)b, size);
2020
io->ptr += size;
2121
}
2222

@@ -232,7 +232,7 @@ void luat_luf_toluac(luac_file_t *cf) {
232232
}
233233
}
234234

235-
LLOGD("当前差距[%s] %08X %08X %08X", cf->source_file, diff, (int)(tio.ptr - buff), cf->fileSize);
235+
// LLOGD("当前差距[%s] %08X %08X %08X", cf->source_file, diff, (int)(tio.ptr - buff), cf->fileSize);
236236
if (diff > 0)
237237
{
238238
LLOGD("luac/luf不一致");
@@ -259,7 +259,7 @@ void luat_luf_toluac(luac_file_t *cf) {
259259
}
260260
}
261261
else {
262-
LLOGD("luac/luf一致");
262+
// LLOGD("luac/luf一致");
263263
}
264264

265265
// 完成输出

0 commit comments

Comments
 (0)