所以,我有这段代码:
/*
* CXenon VM v0.0.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#define IADD 1 // integer add
#define ISUB 2 // integer substract
#define IMUL 3 // integer multiply
#define IDIV 4 // integer divide
#define ILT 5 // integer less than
#define IEQ 6 // integer equals
#define BR 7
#define BRT 8
#define BRF 9
#define ICONST 10 // integer constant
#define LOAD 11 // load from data memory
#define GLOAD 12 // global load from data memory
#define STORE 13 // store in data memory
#define GSTORE 14 // global store in data memory
#define PRINT 15 // print
#define PRINTLN 16 // print with trailing newline
#define POP 17 // pop from stack
#define CALL 18 // call function
#define RET 19
#define FADD 20
#define FSUB 21
#define FMUL 22
#define FDIV 23
#define FCONST 24
#define FLT 25
#define FEQ 26
#define FMT 27
#define FNEQ 28
#define IMT 29
#define INEQ 30
#define BCONST 31
#define BEQ 32
#define BNEQ 33
#define BTRUE 34
#define BFALSE 35
#define HALT 36
char* opcodes[] = {
"",
"IADD",
"ISUB",
"IMULL",
"IDIV",
"ILT",
"IEQ",
"BR",
"BRT",
"BRF",
"ICONST",
"LOAD",
"GLOAD",
"STORE",
"GSTORE",
"PRINT",
"PRINTLN",
"POP",
"CALL",
"RET",
"FADD",
"FSUB",
"FMUL",
"FDIV",
"FCONST",
"FLT",
"FEQ",
"FMT",
"FNEQ",
"IMT",
"INEQ",
"BCONST",
"BEQ",
"BNEQ",
"BTRUE",
"BFALSE",
"HALT"
};
struct stack_base{
char* somechars;
char achar;
int anint;
float afloat;
bool abool;
};
int vm_cpu(struct stack_base* code, bool trace, int datasize){
struct stack_base stack[100];
struct stack_base data[datasize];
int ip = 0; // instructionpointer
int fp; // framepointer
int sp = -1; // stackpointer
int v;
int addr;
int nargs;
int rvalue;
int a, b;
float fv;
float fa, fb;
bool bv;
bool ba, bb;
int opcode = code[ip].anint; // fetch
int size = sizeof(code); // declares size of code
//bool trace = false;
while(ip < size){
if(trace){
printf("%04d: %s\n",ip, opcodes[opcode]); // prints trace
}
ip++;
switch(opcode){
case ICONST:
v = code[ip].anint;
ip++;
sp++;
stack[sp].anint = v;
break;
case ISUB:
b = stack[sp--].anint;
a = stack[sp--].anint;
stack[++sp].anint = a - b;
break;
case IADD:
b = stack[sp--].anint;
a = stack[sp--].anint;
stack[++sp].anint = a + b;
break;
case IMUL:
b = stack[sp--].anint;
a = stack[sp--].anint;
stack[++sp].anint = a * b;
break;
case IDIV:
b = stack[sp--].anint;
a = stack[sp--].anint;
stack[++sp].anint = a/b;
break;
case ILT:
b = stack[sp--].anint;
a = stack[sp--].anint;
if(a > b){
stack[++sp].abool = true;
break;
}
else {
stack[++sp].abool = false;
break;
}
case IMT:
b = stack[sp--].anint;
a = stack[sp--].anint;
if(a < b){
stack[++sp].abool = true;
break;
}
else {
stack[++sp].abool = false;
break;
}
case IEQ:
b = stack[sp--].anint;
a = stack[sp--].anint;
if(a == b){
stack[++sp].abool = true;
break;
}
else {
stack[++sp].abool = false;
break;
}
case INEQ:
b = stack[sp--].anint;
a = stack[sp--].anint;
if(a != b){
stack[++sp].abool = true;
break;
}
else {
stack[++sp].abool = false;
break;
}
case FCONST:
fv = code[ip].afloat;
ip++;
sp++;
stack[sp].afloat = fv;
break;
case FSUB:
fb = stack[sp--].afloat;
fa = stack[sp--].afloat;
stack[++sp].afloat = fa + fb;
break;
case FADD:
fb = stack[sp--].afloat;
fa = stack[sp--].afloat;
stack[++sp].afloat = fa + fb;
break;
case FMUL:
fb = stack[sp--].afloat;
fa = stack[sp--].afloat;
stack[++sp].afloat = fa * fb;
break;
case FDIV:
fb = stack[sp--].afloat;
fa = stack[sp--].afloat;
stack[++sp].afloat = fa/fb;
break;
case FEQ:
fb = stack[sp--].afloat;
fa = stack[sp--].afloat;
if(fa == fb){
stack[++sp].abool = true;
break;
}
else {
stack[++sp].abool = false;
break;
}
case FLT:
fb = stack[sp--].afloat;
fa = stack[sp--].afloat;
if(fa > fb){
stack[++sp].abool = true;
break;
}
else {
stack[++sp].abool = false;
break;
}
case FMT:
fb = stack[sp--].afloat;
fa = stack[sp--].afloat;
if(fa < fb){
stack[++sp].abool = true;
break;
}
else {
stack[++sp].abool = false;
break;
}
case FNEQ:
fb = stack[sp--].afloat;
fa = stack[sp--].afloat;
if(fa != fb){
stack[++sp].abool = true;
break;
}
else {
stack[++sp].abool = false;
break;
}
case BCONST:
bv = code[ip].anint;
ip++;
sp++;
if(bv == BTRUE){
stack[sp].abool = true;
break;
}
else if(bv == BFALSE){
stack[sp].abool = false;
break;
}
case BNEQ:
bb = stack[sp--].abool;
ba = stack[sp--].abool;
if(ba == bb){
stack[++sp].abool = true;
break;
}
else {
stack[++sp].abool = false;
break;
}
case BEQ:
bb = stack[sp--].abool;
ba = stack[sp--].abool;
if(ba != bb){
stack[++sp].abool = true;
break;
}
else {
stack[++sp].abool = false;
break;
}
case PRINT:
v = stack[sp].anint;
sp--;
printf("%i", v);
break;
case PRINTLN:
//v = stack[sp].anint;
//fv = stack[sp].afloat;
bv = stack[sp].abool;
sp--;
printf("%d\n", bv);
break;
case GLOAD:
addr = code[ip].anint;
ip++;
v = data[addr].anint;
sp++;
stack[sp].anint = v;
break;
case GSTORE:
v = stack[sp].anint;
sp--;
addr = code[ip].anint;
ip++;
data[addr].anint = v;
break;
case CALL:
addr = code[ip++].anint;
nargs = code[ip++].anint;
stack[++sp].anint = nargs;
stack[++sp].anint = fp;
stack[++sp].anint = ip;
fp = sp;
ip = addr;
break;
case RET:
rvalue = stack[sp--].anint;
sp = fp;
ip = stack[sp--].anint;
fp = stack[sp--].anint;
nargs = stack[sp--].anint;
sp -= nargs;
stack[sp++].anint = rvalue;
break;
case HALT:
return 0;
break;
}
opcode = code[ip].anint;
}
}
void main(){
struct stack_base hello[] = {
BCONST, BTRUE,
BCONST, BTRUE,
BNEQ,
PRINTLN,
HALT
};
vm_cpu(hello, true, 4);
}
当我编译该代码时,我得到了这些错误:
thewatcher@thewatcher ~/Xenon/CXenon/src $ gcc main.c -o test
main.c: In function ‘main’:
main.c:402:9: warning: initialization makes pointer from integer without a cast [enabled by default]
BCONST, BTRUE,
^
main.c:402:9: warning: (near initialization for ‘hello[0].somechars’) [enabled by default]
main.c:405:9: warning: initialization makes pointer from integer without a cast [enabled by default]
PRINTLN,
^
main.c:405:9: warning: (near initialization for ‘hello[1].somechars’) [enabled by default]
我不明白为什么会这样,我不知道如何解决这个问题。有人能帮助我吗?
答案 0 :(得分:1)
主函数中的结构成员初始化与结构定义不同。 您应该像这样初始化结构成员。
struct stack_base hello[] = {
{
"hey there",'a',22,11,true
},
{
"thank you",'b',11,11,false
},
{
"hello",'c',11,11,true
}
};
以下一行,
int size = sizeof(code);
code
只是指向结构的指针,该结构的大小始终为4(较旧的系统)或8(较新的系统)字节。我猜你试图找到hello[]
结构数组中的成员数。在这种情况下,您必须将此数字作为函数单独传递给vm_cpu
函数,或使用某些逻辑在vm_cpu函数中确定此数字。
如果你不想传递没有。将成员作为参数,在main
fn中初始化结构如下:
struct stack_base hello[] = {
{
"abc",'a',11,11,true
},
{
"abc",'a',11,11,true
},
{
"abc",'a',11,11,true
},NULL
};
在vm_cpu函数中,使用以下逻辑来查找no。结构数组元素。
int size = 0;
struct stack_base *ptr = code;
while(ptr[size].somechars) {
size++;
ptr++;
}
size--;
printf("The no. of elements of structure array: %d\n", size);
答案 1 :(得分:0)
如果正确初始化结构数组hello
,则您面临的错误应该消失。目前初始化它的方式是非常令人困惑,正如编译器告诉你的那样:
警告:( 接近初始化表示'hello [0] .somechars')[默认启用]
意味着初始化结构数组的方式存在错误(see here以完全理解错误中涉及的语法)。你的第二个错误:
警告:初始化使得整数指针不带强制转换
编译器在结构数组的初始化中也很困惑。举例来说,这个初始化:
struct my_struct {
int a;
int b;
}
//Create a structure assigning the value of a to 10 and b to 20:
struct my_struct foo = {10, 20};
这将初始化一个结构,而不是多个。如果你想使用类似的语法创建一个结构数组,你可以这样做:
//Use my_struct from the previous example
struct my_struct foo[] {
{.a = 10, .b = 20},
{.a = 1, .b = 2},
{.a = 5, .b = 5}
};
要进一步了解此语法,请参阅designated initializers。上面的语法为数组中的每个结构创建了一个结构数组,其中包含以下结构:
foo[0].a = 10;
foo[0].b = 20;
foo[1].a = 1;
foo[1].b = 2;
foo[2].a = 5;
foo[2].b = 5;
初始化struct stack_base hello[]
以使每个成员拥有这些值:
//hello[0].somechars will be assigned the string "hello"
hello[0].achar = 'a';
hello[0].anint = 1;
hello[0].afloat = 1.0;
hello[0].abool = 0;
//hello[1].somechars will be assigned the string "bye bye"
hello[1].achar = 'b';
hello[1].anint = 2;
hello[1].afloat = 10.0;
hello[1].abool = 1;
然后,你可以写:
struct stack_base hello[] {
{.achar = 'a', .anint = 1, .afloat = 1.0, .abool = 0},
{.achar = 'b', .anint = 2, .afloat = 10.0, .abool = 1}
};
//Assign strings to achar
strcpy(hello[0].somechars, "hello");
strcpy(hello[1].somechars, "bye bye");
在上面的示例中,不是将精确值分配给结构的每个成员,而是使用常量替换每个成员的赋值是有意义的。例如,您可以将.anint = 1
替换为.anint = BCONST
等。
然而,目前,它只是混淆了初始化结构数组的方式。