1 Star 2 Fork 1

万里/jsvm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
disasm.c 2.57 KB
一键复制 编辑 原始数据 按行查看 历史
万里 提交于 2021-11-01 00:23 . add source files
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "image.h"
#include "vm.h"
#include "gc.h"
char* read_until_eof(FILE* f, uint32_t* len)
{
size_t cap = 4096;
size_t idx = 0;
char* buff = js_alloc(cap);
while(!feof(stdin)) {
idx += fread(buff + idx, 1, 4096, f);
if(idx >= cap) {
cap *= 2;
buff = js_realloc(buff, cap);
}
}
*len = idx;
return buff;
}
int main()
{
uint32_t dummy;
uint32_t len;
char* buff;
js_image_t* image;
uint32_t i, j, op;
double number;
js_gc_init(&dummy);
buff = read_until_eof(stdin, &len);
image = js_image_parse(buff, len);
printf("read %d sections\n", image->section_count);
for(i = 0; i < image->section_count; i++) {
printf("\nsection %d (flags %d, var count: %d):\n", i, image->sections[i].flags, image->sections[i].var_count);
for(j = 0; j < image->sections[i].instruction_count; j++) {
op = image->sections[i].instructions[j];
printf(" %04d %-12s", j, js_instruction(op)->name);
switch(js_instruction(op)->operand) {
case OPERAND_NONE:
printf("\n");
break;
case OPERAND_NUMBER:
number = *(double*)&image->sections[i].instructions[++j];
printf("%lf\n", number);
j++;
break;
case OPERAND_UINT32:
op = image->sections[i].instructions[++j];
printf("%u\n", op);
break;
case OPERAND_UINT32_UINT32:
op = image->sections[i].instructions[++j];
printf("%u, ", op);
op = image->sections[i].instructions[++j];
printf("%u\n", op);
break;
case OPERAND_STRING:
op = image->sections[i].instructions[++j];
printf("\"%s\" (%d)\n", image->strings[op]->buff, op);
break;
case OPERAND_UINT32_STRING:
op = image->sections[i].instructions[++j];
printf("%u, ", op);
op = image->sections[i].instructions[++j];
printf("\"%s\" (%d)\n", image->strings[op]->buff, op);
break;
}
}
}
printf("\nstrings:\n");
for(i = 0; i < image->string_count; i++) {
printf(" %04d \"%s\"\n", i, image->strings[i]->buff);
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/wanliofficial/jsvm.git
git@gitee.com:wanliofficial/jsvm.git
wanliofficial
jsvm
jsvm
master

搜索帮助