Skip to content

Commit 727f4b6

Browse files
author
razziana
committed
Tree merge.
1 parent d641749 commit 727f4b6

File tree

9 files changed

+224
-22
lines changed

9 files changed

+224
-22
lines changed

visscript-hackground/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CC = gcc
22
CFLAGS = -O6 -ggdb3 -Wall
33
LIBS = `pkg-config --cflags --libs libvisual`
44

5-
OBJS = avs_lexer.o avs_parser.o avs_test.o avs_compiler.o avs_il_tree.o avs_il_assembler.o avs_runnable.o avs_functions.o avs_stack.o avs_blob.o avs_blob_pool.o avs_x86_opcode.o avs_il_register.o avs_il_instruction.o avs_ix_compiler.o avs_ix_machine.o avs_il_core.o
5+
OBJS = avs_lexer.o avs_parser.o avs_test.o avs_compiler.o avs_il_tree.o avs_il_assembler.o avs_runnable.o avs_functions.o avs_stack.o avs_blob.o avs_blob_pool.o avs_x86_opcode.o avs_il_register.o avs_il_instruction.o avs_ix_compiler.o avs_ix_machine.o avs_il_core.o avs_x86_compiler.o
66

77
#avs_x86_opcode.o avs_x86_assembler.o
88

visscript-hackground/avs_il_core.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@
88

99

1010
extern ILCore il_core_ix;
11+
extern ILCore il_core_x86;
12+
13+
static ILCore * get_core(void)
14+
{
15+
ILCore *cores[] =
16+
{
17+
&il_core_ix,
18+
&il_core_x86,
19+
NULL,
20+
};
21+
22+
return cores[1];
23+
}
24+
1125

1226

1327
static void context_ctor(ILCoreContext *ctx, ILCore *core)
@@ -23,17 +37,6 @@ static ILCoreContext * create_context(ILCore *core)
2337
return ctx;
2438
}
2539

26-
static ILCore * get_core(void)
27-
{
28-
ILCore *cores[] =
29-
{
30-
&il_core_ix,
31-
NULL,
32-
};
33-
34-
return cores[0];
35-
}
36-
3740
int avs_il_core_compile(ILCoreContext *ctx, AvsILTreeContext *tree, AvsRunnable *obj)
3841
{
3942
return ctx->core->compile(ctx, tree, obj);

visscript-hackground/avs_il_register.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ static ILRegister * load_identifier(AvsILAssemblerContext *ctx, AvsRunnable *rob
3636
var = avs_runnable_variable_create(robj, arg->value.identifier, 0);
3737
}
3838

39+
avs_debug(print("ILR: Variable name: %s", var->name));
40+
3941
reg->type = ILRegisterTypeVariable;
4042
reg->value.variable = var;
4143

@@ -49,6 +51,7 @@ static ILRegister * load_worker(AvsILAssemblerContext *ctx, AvsRunnableVariable
4951
ILRegister *reg = avs_il_register_create();
5052

5153
reg->type = ILRegisterTypeConstant;
54+
convert_argument(arg, reg);
5255
return reg;
5356
}
5457

visscript-hackground/avs_ix.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ typedef struct _AvsIXOpcode IXOpcode;
1212
typedef enum _AvsIXOpcodeType {
1313
IXOpcodeNop,
1414
IXOpcodeAssign,
15+
IXOpcodeAdd,
16+
IXOpcodeSub,
1517
IXOpcodeMul,
18+
IXOpcodeDiv,
19+
IXOpcodeMod,
20+
IXOpcodeAnd,
21+
IXOpcodeOr,
1622
IXOpcodeInvalid
1723
} IXOpcodeType;
1824

visscript-hackground/avs_ix_compiler.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,30 @@ static int compile_opcode(IXRunnableData *rd, ILInstruction *insn)
5454

5555
switch (insn->type) {
5656
case ILInstructionAssign:
57-
op = opcode_add(rd, IXOpcodeAssign);
57+
case ILInstructionAdd:
58+
case ILInstructionSub:
59+
case ILInstructionMul:
60+
case ILInstructionDiv:
61+
case ILInstructionMod:
62+
case ILInstructionAnd:
63+
case ILInstructionOr: {
64+
#define _L(x) ILInstruction##x
65+
#define _R(x) IXOpcode##x
66+
static unsigned char insn2insn[ILInstructionCount] = {
67+
[_L(Assign)] = _R(Assign), [_L(Add)] = _R(Add),
68+
[_L(Sub)] = _R(Sub), [_L(Mul)] = _R(Mul),
69+
[_L(Div)] = _R(Div), [_L(Mod)] = _R(Mod),
70+
[_L(And)] = _R(And), [_L(Or)] = _R(Or),
71+
};
72+
#undef _L
73+
#undef _R
74+
75+
op = opcode_add(rd, insn2insn[insn->type]);
5876
op->reg[0] = get_operand(insn->reg[0]);
5977
op->reg[1] = get_operand(insn->reg[1]);
78+
op->reg[2] = get_operand(insn->reg[2]);
6079
break;
61-
62-
case ILInstructionMul:
63-
op = opcode_add(rd, IXOpcodeMul);
64-
op->reg[0] = get_operand(insn->reg[0]); /* Dest */
65-
op->reg[1] = get_operand(insn->reg[1]); /* Source0 */
66-
op->reg[2] = get_operand(insn->reg[2]); /* Source1 */
67-
break;
80+
}
6881

6982
default:
7083
break;

visscript-hackground/avs_ix_machine.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,55 @@ IX_INSTRUCTION(ix_nop)
1414

1515
IX_INSTRUCTION(ix_assign)
1616
{
17-
*op->reg[0] = *op->reg[1];
17+
*op->reg[0] = *op->reg[1] = *op->reg[2];
18+
}
19+
20+
IX_INSTRUCTION(ix_add)
21+
{
22+
*op->reg[0] = *op->reg[1] + *op->reg[2];
23+
}
24+
25+
IX_INSTRUCTION(ix_sub)
26+
{
27+
*op->reg[0] = *op->reg[1] - *op->reg[2];
1828
}
1929

2030
IX_INSTRUCTION(ix_mul)
2131
{
2232
*op->reg[0] = *op->reg[1] * *op->reg[2];
2333
}
2434

35+
IX_INSTRUCTION(ix_div)
36+
{
37+
*op->reg[0] = *op->reg[1] / *op->reg[2];
38+
}
39+
40+
IX_INSTRUCTION(ix_mod)
41+
{
42+
*op->reg[0] = (unsigned int)*op->reg[1] % (unsigned int)*op->reg[2];
43+
}
44+
45+
IX_INSTRUCTION(ix_and)
46+
{
47+
*op->reg[0] = (unsigned int)*op->reg[1] & (unsigned int)*op->reg[2];
48+
}
49+
50+
IX_INSTRUCTION(ix_or)
51+
{
52+
*op->reg[0] = (unsigned int)*op->reg[1] | (unsigned int)*op->reg[2];
53+
}
54+
2555
static IXOpcodeHandler opcode_handler[] =
2656
{
2757
ix_nop,
2858
ix_assign,
59+
ix_add,
60+
ix_sub,
2961
ix_mul,
62+
ix_div,
63+
ix_mod,
64+
ix_and,
65+
ix_or,
3066
};
3167

3268
int avs_ix_machine_run(AvsRunnable *obj)

visscript-hackground/avs_x86.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
#define _AVS_X86_H 1
33

44
#include "avs_x86_opcode.h"
5-
#include "avs_x86_assembler.h"
5+
#include "avs_x86_compiler.h"
6+
7+
typedef struct _X86GlobalData {
8+
X86Context ctx;
9+
} X86GlobalData;
10+
11+
#define X86_GLOBALDATA(ictx) \
12+
((X86GlobalData *) (ictx)->ctx)
613

714
#endif /* !_AVS_X86_H */
815

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <string.h>
5+
#include <errno.h>
6+
7+
#include "avs.h"
8+
#include "avs_x86.h"
9+
10+
/* XXX: Duplicate / Reference */
11+
static AvsNumber * get_operand(ILRegister *reg)
12+
{
13+
switch (reg->type) {
14+
case ILRegisterTypeConstant:
15+
return &reg->value.constant;
16+
17+
case ILRegisterTypeVariable:
18+
return reg->value.variable->value;
19+
}
20+
21+
return NULL;
22+
}
23+
24+
static int compile_opcode(X86GlobalData *gd, ILInstruction *insn)
25+
{
26+
switch (insn->type) {
27+
case ILInstructionAssign:
28+
x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[2])));
29+
x86_emit1(&gd->ctx, fsts, offset(get_operand(insn->reg[1])));
30+
x86_emit1(&gd->ctx, fstps, offset(get_operand(insn->reg[0])));
31+
break;
32+
33+
case ILInstructionAdd:
34+
x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[1])));
35+
x86_emit1(&gd->ctx, fadds, offset(get_operand(insn->reg[2])));
36+
x86_emit1(&gd->ctx, fstps, offset(get_operand(insn->reg[0])));
37+
break;
38+
39+
case ILInstructionSub:
40+
x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[1])));
41+
x86_emit1(&gd->ctx, fsubs, offset(get_operand(insn->reg[2])));
42+
x86_emit1(&gd->ctx, fstps, offset(get_operand(insn->reg[0])));
43+
break;
44+
45+
case ILInstructionMul:
46+
x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[1])));
47+
x86_emit1(&gd->ctx, fmuls, offset(get_operand(insn->reg[2])));
48+
x86_emit1(&gd->ctx, fstps, offset(get_operand(insn->reg[0])));
49+
break;
50+
51+
case ILInstructionDiv:
52+
x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[1])));
53+
x86_emit1(&gd->ctx, fdivs, offset(get_operand(insn->reg[2])));
54+
x86_emit1(&gd->ctx, fstps, offset(get_operand(insn->reg[0])));
55+
break;
56+
57+
case ILInstructionAnd:
58+
#if 0
59+
x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[1])));
60+
x86_emit1(&gd->ctx, fistp, eax);
61+
x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[2])));
62+
x86_emit1(&gd->ctx, fistp, ebx);
63+
x86_emit2(&gd->ctx, and, eax, ebx);
64+
x86_emit1(&gd->ctx, filds, eax);
65+
x86_emit1(&gd->ctx, fstps, offset(get_operand(insn->reg[0])));
66+
#endif
67+
break;
68+
69+
default:
70+
break;
71+
}
72+
73+
return 0;
74+
}
75+
76+
IL_CORE_COMPILE(avs_x86_compiler_compile)
77+
{
78+
X86GlobalData *gd = X86_GLOBALDATA(ctx);
79+
ILInstruction *insn;
80+
81+
avs_debug(print("X86: Compiling started..."));
82+
x86_context_reset(&gd->ctx);
83+
84+
/* Compile function entrance, setup stack frame*/
85+
x86_emit1(&gd->ctx, pushl, ebp);
86+
x86_emit2(&gd->ctx, movl, esp, ebp);
87+
88+
for (insn=tree->base; insn != NULL; insn = insn->next) {
89+
avs_debug(print("X86: Compiling instruction: %p", insn));
90+
compile_opcode(gd, insn);
91+
}
92+
93+
/* Cleanup stack frame */
94+
x86_emit0(&gd->ctx, emms);
95+
x86_emit0(&gd->ctx, leave);
96+
x86_emit0(&gd->ctx, ret);
97+
98+
/* Link machine */
99+
obj->run = (AvsRunnableExecuteCall) gd->ctx.buf;
100+
avs_debug(print("X86: Compiling finished..."));
101+
return 0;
102+
}
103+
104+
IL_CORE_INIT(avs_x86_compiler_init)
105+
{
106+
X86GlobalData *gd = visual_mem_malloc0(sizeof(X86GlobalData));
107+
memset(gd, 0, sizeof(X86GlobalData));
108+
ctx->ctx = gd;
109+
110+
/* Initialize X86 Assembler opcode context */
111+
x86_context_init(&gd->ctx, 4096, 1024*1024);
112+
113+
return 0;
114+
}
115+
116+
ILCore il_core_x86 =
117+
{
118+
.name = "X86 Bytecode",
119+
.init = avs_x86_compiler_init,
120+
.compile = avs_x86_compiler_compile,
121+
};
122+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef _AVS_IX_COMPILER_H
2+
#define _AVS_IX_COMPILER_H 1
3+
4+
#include "avs.h"
5+
6+
typedef struct _AvsIXCompilerContext {
7+
AvsILTreeContext *tree;
8+
} IXCompilerContext;
9+
10+
/* prototypes */
11+
12+
#endif /* !_AVS_IX_COMPILER_H */

0 commit comments

Comments
 (0)