change opcode print format

This commit is contained in:
ValKmjolnir 2022-03-27 01:20:17 +08:00
parent cf722fd98c
commit 86f6296268
5 changed files with 152 additions and 7 deletions

View File

@ -30,8 +30,10 @@ test:nasal
@ ./nasal -op -t -d test/pi.nas
@ ./nasal -op -t -d test/prime.nas
@ ./nasal -op -t -d test/props_sim.nas
@ ./nasal -op -e test/qrcode.nas
@ ./nasal -op -t -d test/quick_sort.nas
@ ./nasal -op -e test/scalar.nas
-@ ./nasal -op -t test/snake.nas
@ ./nasal -op -e test/trait.nas
-@ ./nasal -op -t test/tetris.nas
@ ./nasal -op -t -d test/turingmachine.nas

View File

@ -166,7 +166,7 @@ struct
struct opcode
{
uint16_t op; // opcode
uint8_t op; // opcode
uint16_t fidx;// source code file index
uint32_t num; // imm num
uint32_t line;// line of source code
@ -200,6 +200,11 @@ private:
std::unordered_map<std::string,int> global;
// local : max 32768 upvalues 65536 values
std::list<std::unordered_map<std::string,int>> local;
// func end stack, reserved for print
std::stack<uint32_t> fbstk;
std::stack<uint32_t> festk;
void die(std::string,const uint32_t);
void regist_number(const double);
@ -613,7 +618,7 @@ void nasal_codegen::multi_def(const nasal_ast& ast)
{
auto& ids=ast[0].child();
int size=ids.size();
if(ast[1].type()==ast_multi_scalar)
if(ast[1].type()==ast_multi_scalar) // (var a,b,c)=(c,b,a);
{
auto& vals=ast[1].child();
for(int i=0;i<size;++i)
@ -625,7 +630,7 @@ void nasal_codegen::multi_def(const nasal_ast& ast)
gen(op_loadl,local_find(str),ids[i].line());
}
}
else
else // (var a,b,c)=[0,1,2];
{
calc_gen(ast[1]);
for(int i=0;i<size;++i)
@ -1138,7 +1143,32 @@ void nasal_codegen::print_op(uint32_t index)
{
// print opcode index,opcode name,opcode immediate number
const opcode& c=code[index];
printf("0x%.8x: %s ",index,code_table[c.op].name);
if(!festk.empty() && index==festk.top())
{
printf("<0x%x>;\n\n",fbstk.top());
fbstk.pop();
festk.pop();
}
if(c.op==op_newf)
{
printf("\nfunc <0x%x>:\n",index);
for(uint32_t i=index;i<code.size();++i)
if(code[i].op==op_jmp)
{
fbstk.push(index);
festk.push(code[i].num);
break;
}
}
printf(" 0x%.8x: %.2x %.2x %.2x %.2x %.2x %s ",
index,
c.op,
uint8_t((c.num&0xff000000)>>24),
uint8_t((c.num&0x00ff0000)>>16),
uint8_t((c.num&0x0000ff00)>>8),
uint8_t(c.num&0x000000ff),
code_table[c.op].name
);
// print detail info
switch(c.op)
{
@ -1170,9 +1200,10 @@ void nasal_codegen::print_op(uint32_t index)
void nasal_codegen::print()
{
for(auto& num:num_res)
std::cout<<".number "<<num<<'\n';
std::cout<<" .number "<<num<<'\n';
for(auto& str:str_res)
std::cout<<".symbol \""<<rawstr(str)<<"\"\n";
std::cout<<" .symbol \""<<rawstr(str)<<"\"\n";
std::cout<<"\n";
for(uint32_t i=0;i<code.size();++i)
print_op(i);
}

View File

@ -509,7 +509,7 @@ inline void nasal_vm::opr_lnkeqc()
inline void nasal_vm::opr_meq()
{
mem_addr[0]=(--gc.top)[0];
mem_addr[0]=(--gc.top)[0]; // pop old mem_addr[0] and replace it
mem_addr=nullptr;
}
inline void nasal_vm::opr_eq()

View File

@ -49,14 +49,17 @@ var testfile=[
"test/loop.nas ",
"test/mandel.nas ",
"test/mandelbrot.nas ",
"test/md5.nas ",
"test/module_test.nas ",
"test/nasal_test.nas ",
"test/pi.nas ",
"test/prime.nas ",
"test/props_sim.nas ",
"test/props.nas ",
"test/qrcode.nas ",
"test/quick_sort.nas ",
"test/scalar.nas ",
"test/snake.nas ",
"test/tetris.nas ",
"test/trait.nas ",
"test/turingmachine.nas",

109
test/md5.nas Normal file
View File

@ -0,0 +1,109 @@
import("lib.nas");
var md5=func(s){
var K=[
0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,0x698098d8,
0x8b44f7af,0xffff5bb1,0x895cd7be,0x6b901122,0xfd987193,
0xa679438e,0x49b40821,0xf61e2562,0xc040b340,0x265e5a51,
0xe9b6c7aa,0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,
0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,0xa9e3e905,
0xfcefa3f8,0x676f02d9,0x8d2a4c8a,0xfffa3942,0x8771f681,
0x6d9d6122,0xfde5380c,0xa4beea44,0x4bdecfa9,0xf6bb4b60,
0xbebfbc70,0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,
0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,0xf4292244,
0x432aff97,0xab9423a7,0xfc93a039,0x655b59c3,0x8f0ccc92,
0xffeff47d,0x85845dd1,0x6fa87e4f,0xfe2ce6e0,0xa3014314,
0x4e0811a1,0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391
];
var S=[
7,12,17,22,
7,12,17,22,
7,12,17,22,
7,12,17,22,
5,9,14,20,
5,9,14,20,
5,9,14,20,
5,9,14,20,
4,11,16,23,
4,11,16,23,
4,11,16,23,
4,11,16,23,
6,10,15,21,
6,10,15,21,
6,10,15,21,
6,10,15,21
]
var l=func(num,cx){
for(var i=0;i<cx;i+=1)
num=bits.bitand(0xffffffff,num*2);
return num;
}
var r=func(num,cx){
for(var i=0;i<cx;i+=1)
num=int(num/2);
return num;
}
var rol=func(num,cx){
if(cx>32)
cx=cx-int(cx/32)*32;
return bits.bitor(l(num,cx),r(num,32-cx));
}
var F=func(x,y,z){
return bits.bitor(
bits.bitand(x,y),
bits.bitand(bits.bitnot(x),z)
);
}
var G=func(x,y,z){
return bits.bitor(
bits.bitand(x,z),
bits.bitand(y,bits.bitnot(z))
);
}
var H=func(x,y,z){
return bits.bitxor(bits.bitxor(x,y),z);
}
var I=func(x,y,z){
return bits.bitor(
bits.bitxor(x,y),
bits.bitnot(z)
);
}
var len=size(s);
var res=[];
var v=[256,128,64,32,16,8,4,2,1];
for(var i=0;i<size(s);i+=1){
var c=s[i];
foreach(var j;v){
append(res,bits.bitand(c,j)?1:0);
}
}
var mod=size(res)-int(size(res)/512)*512;
if(mod==0){
append(res,1);
mod+=1;
for(;mod<448;mod+=1)
append(res,0);
}elsif(mod<448){
append(res,1);
mod+=1;
for(;mod<448;mod+=1)
append(res,0);
}elsif(mod>448){
append(res,1);
mod+=1;
for(;mod<512+448;mod+=1)
append(res,0);
}
var tmp=[];
var A=0x01234567;
var B=0x89abcdef;
var C=0xfedcba98;
var D=0x76543210;
println(res,' ',size(res));
}
md5("helloworld");