Skip to content

Commit 44512c5

Browse files
committed
support label, jmp and so on
1 parent cf597e9 commit 44512c5

File tree

2 files changed

+196
-13
lines changed

2 files changed

+196
-13
lines changed

rxbyak/rxbyak.cpp

Lines changed: 194 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,27 @@
1010
#define RB_FUNC(f) reinterpret_cast<VALUE (*)(...)>(f)
1111

1212
#define RXBYAK_GENERATOR(s, x) RXbyakGenerator* x;Data_Get_Struct(s, RXbyakGenerator, x)
13-
13+
#define RXBYAK_CALL1(s, m, a1) RXBYAK_GENERATOR(s,rx);rx->m(a1);return Qnil
14+
15+
#define RXBYAK_JUMP(s, m, argc, argv) \
16+
RXBYAK_GENERATOR(s, rx); \
17+
if (argc==1) { \
18+
rx->_##m(argv[0], Qnil); \
19+
} else if (argc==2) { \
20+
rx->_##m(argv[0], argv[1]); \
21+
} else { \
22+
rb_raise(rb_eArgError, "wrong number of arguments"); \
23+
} \
24+
return Qnil;
25+
26+
#define RXBYAK_JUMP_IMPL(m, dest, jump_type) \
27+
Check_Type(dest, T_SYMBOL); \
28+
const char* label = rb_id2name(rb_to_id(dest)); \
29+
if (jump_type != Qnil) { \
30+
m(label); /* TODO */ \
31+
} else { \
32+
m(label); \
33+
} \
1434

1535
/**
1636
* @brief RXbyak implementation
@@ -212,6 +232,107 @@ class RXbyakGenerator : public Xbyak::CodeGenerator {
212232
const Xbyak::Reg& dest = id2reg(op1);
213233
not(dest);
214234
}
235+
void set_label(const VALUE& x) {
236+
Check_Type(x, T_SYMBOL);
237+
const char* label = rb_id2name(rb_to_id(x));
238+
L(label);
239+
}
240+
241+
242+
void _jmp(const VALUE& dest, const VALUE& jump_type) {
243+
Check_Type(dest, T_SYMBOL);
244+
const char* label = rb_id2name(rb_to_id(dest));
245+
if (jump_type != Qnil) {
246+
jmp(label); /* TODO */
247+
} else {
248+
jmp(label);
249+
} \
250+
}
251+
void _jo(const VALUE& dest, const VALUE& jump_type) {
252+
RXBYAK_JUMP_IMPL(jo, dest, jump_type);
253+
}
254+
void _jno(const VALUE& dest, const VALUE& jump_type) {
255+
RXBYAK_JUMP_IMPL(jno, dest, jump_type);
256+
}
257+
void _jb(const VALUE& dest, const VALUE& jump_type) {
258+
RXBYAK_JUMP_IMPL(jb, dest, jump_type);
259+
}
260+
void _jnae(const VALUE& dest, const VALUE& jump_type) {
261+
RXBYAK_JUMP_IMPL(jnae, dest, jump_type);
262+
}
263+
void _jnb(const VALUE& dest, const VALUE& jump_type) {
264+
RXBYAK_JUMP_IMPL(jnb, dest, jump_type);
265+
}
266+
void _jae(const VALUE& dest, const VALUE& jump_type) {
267+
RXBYAK_JUMP_IMPL(jae, dest, jump_type);
268+
}
269+
void _je(const VALUE& dest, const VALUE& jump_type) {
270+
RXBYAK_JUMP_IMPL(je, dest, jump_type);
271+
}
272+
void _jz(const VALUE& dest, const VALUE& jump_type) {
273+
RXBYAK_JUMP_IMPL(jz, dest, jump_type);
274+
}
275+
void _jne(const VALUE& dest, const VALUE& jump_type) {
276+
RXBYAK_JUMP_IMPL(jne, dest, jump_type);
277+
}
278+
void _jnz(const VALUE& dest, const VALUE& jump_type) {
279+
RXBYAK_JUMP_IMPL(jnz, dest, jump_type);
280+
}
281+
void _jbe(const VALUE& dest, const VALUE& jump_type) {
282+
RXBYAK_JUMP_IMPL(jbe, dest, jump_type);
283+
}
284+
void _jna(const VALUE& dest, const VALUE& jump_type) {
285+
RXBYAK_JUMP_IMPL(jna, dest, jump_type);
286+
}
287+
void _jnbe(const VALUE& dest, const VALUE& jump_type) {
288+
RXBYAK_JUMP_IMPL(jnbe, dest, jump_type);
289+
}
290+
void _ja(const VALUE& dest, const VALUE& jump_type) {
291+
RXBYAK_JUMP_IMPL(ja, dest, jump_type);
292+
}
293+
void _js(const VALUE& dest, const VALUE& jump_type) {
294+
RXBYAK_JUMP_IMPL(js, dest, jump_type);
295+
}
296+
void _jns(const VALUE& dest, const VALUE& jump_type) {
297+
RXBYAK_JUMP_IMPL(jns, dest, jump_type);
298+
}
299+
void _jp(const VALUE& dest, const VALUE& jump_type) {
300+
RXBYAK_JUMP_IMPL(jp, dest, jump_type);
301+
}
302+
void _jpe(const VALUE& dest, const VALUE& jump_type) {
303+
RXBYAK_JUMP_IMPL(jpe, dest, jump_type);
304+
}
305+
void _jnp(const VALUE& dest, const VALUE& jump_type) {
306+
RXBYAK_JUMP_IMPL(jnp, dest, jump_type);
307+
}
308+
void _jpo(const VALUE& dest, const VALUE& jump_type) {
309+
RXBYAK_JUMP_IMPL(jpo, dest, jump_type);
310+
}
311+
void _jl(const VALUE& dest, const VALUE& jump_type) {
312+
RXBYAK_JUMP_IMPL(jl, dest, jump_type);
313+
}
314+
void _jnge(const VALUE& dest, const VALUE& jump_type) {
315+
RXBYAK_JUMP_IMPL(jnge, dest, jump_type);
316+
}
317+
void _jnl(const VALUE& dest, const VALUE& jump_type) {
318+
RXBYAK_JUMP_IMPL(jnl, dest, jump_type);
319+
}
320+
void _jge(const VALUE& dest, const VALUE& jump_type) {
321+
RXBYAK_JUMP_IMPL(jge, dest, jump_type);
322+
}
323+
void _jle(const VALUE& dest, const VALUE& jump_type) {
324+
RXBYAK_JUMP_IMPL(jle, dest, jump_type);
325+
}
326+
void _jng(const VALUE& dest, const VALUE& jump_type) {
327+
RXBYAK_JUMP_IMPL(jng, dest, jump_type);
328+
}
329+
void _jnle(const VALUE& dest, const VALUE& jump_type) {
330+
RXBYAK_JUMP_IMPL(jnle, dest, jump_type);
331+
}
332+
void _jg(const VALUE& dest, const VALUE& jump_type) {
333+
RXBYAK_JUMP_IMPL(jg, dest, jump_type);
334+
}
335+
215336

216337
void _ret(int imm = 0) { ret(imm); }
217338
void _aaa() { aaa(); }
@@ -269,8 +390,7 @@ class RXbyakGenerator : public Xbyak::CodeGenerator {
269390

270391
extern "C"
271392
VALUE RXbyak_mov(VALUE self, VALUE a1, VALUE a2) {
272-
RXbyakGenerator* rx;
273-
Data_Get_Struct(self, RXbyakGenerator, rx);
393+
RXBYAK_GENERATOR(self, rx);
274394

275395
switch (TYPE(a1)) {
276396
case T_ARRAY:
@@ -310,8 +430,7 @@ VALUE RXbyak_mov(VALUE self, VALUE a1, VALUE a2) {
310430

311431
extern "C"
312432
VALUE RXbyak_movq(VALUE self, const VALUE a1, const VALUE a2) {
313-
RXbyakGenerator* rx;
314-
Data_Get_Struct(self, RXbyakGenerator, rx);
433+
RXBYAK_GENERATOR(self, rx);
315434

316435
switch (TYPE(a1)) {
317436
case T_ARRAY:
@@ -653,6 +772,43 @@ extern "C" VALUE RXbyak_not(VALUE self, VALUE op1) {
653772
return Qnil;
654773
}
655774

775+
extern "C" VALUE RXbyak_label(VALUE self, VALUE label) {
776+
RXBYAK_CALL1(self, set_label, label);
777+
}
778+
779+
780+
extern "C" VALUE RXbyak_jmp(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jmp, argc, argv); }
781+
extern "C" VALUE RXbyak_jo(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jo, argc, argv); }
782+
extern "C" VALUE RXbyak_jno(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jno, argc, argv); }
783+
extern "C" VALUE RXbyak_jb(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jb, argc, argv); }
784+
extern "C" VALUE RXbyak_jnae(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jnae, argc, argv); }
785+
extern "C" VALUE RXbyak_jnb(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jnb, argc, argv); }
786+
extern "C" VALUE RXbyak_jae(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jae, argc, argv); }
787+
extern "C" VALUE RXbyak_je(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, je, argc, argv); }
788+
extern "C" VALUE RXbyak_jz(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jz, argc, argv); }
789+
extern "C" VALUE RXbyak_jne(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jne, argc, argv); }
790+
extern "C" VALUE RXbyak_jnz(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jnz, argc, argv); }
791+
extern "C" VALUE RXbyak_jbe(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jbe, argc, argv); }
792+
extern "C" VALUE RXbyak_jna(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jna, argc, argv); }
793+
extern "C" VALUE RXbyak_jnbe(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jnbe, argc, argv); }
794+
extern "C" VALUE RXbyak_ja(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, ja, argc, argv); }
795+
extern "C" VALUE RXbyak_js(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, js, argc, argv); }
796+
extern "C" VALUE RXbyak_jns(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jns, argc, argv); }
797+
extern "C" VALUE RXbyak_jp(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jp, argc, argv); }
798+
extern "C" VALUE RXbyak_jpe(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jpe, argc, argv); }
799+
extern "C" VALUE RXbyak_jnp(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jnp, argc, argv); }
800+
extern "C" VALUE RXbyak_jpo(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jpo, argc, argv); }
801+
extern "C" VALUE RXbyak_jl(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jl, argc, argv); }
802+
extern "C" VALUE RXbyak_jnge(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jnge, argc, argv); }
803+
extern "C" VALUE RXbyak_jnl(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jnl, argc, argv); }
804+
extern "C" VALUE RXbyak_jge(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jge, argc, argv); }
805+
extern "C" VALUE RXbyak_jle(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jle, argc, argv); }
806+
extern "C" VALUE RXbyak_jng(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jng, argc, argv); }
807+
extern "C" VALUE RXbyak_jnle(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jnle, argc, argv); }
808+
extern "C" VALUE RXbyak_jg(int argc, const VALUE* argv, VALUE self) { RXBYAK_JUMP(self, jg, argc, argv); }
809+
810+
811+
656812

657813
extern "C"
658814
VALUE RXbyak_method_missing(int argc, const VALUE* argv, VALUE self) {
@@ -665,12 +821,7 @@ VALUE RXbyak_method_missing(int argc, const VALUE* argv, VALUE self) {
665821

666822
extern "C"
667823
VALUE RXbyak_exec(int argc, const VALUE* argv, VALUE self) {
668-
RXbyakGenerator* rx;
669-
Data_Get_Struct(self, RXbyakGenerator, rx);
670-
671-
//int (*proc)() = (int (*)())rx->getCode();
672-
//VALUE r = INT2FIX(proc());
673-
824+
RXBYAK_GENERATOR(self, rx);
674825
if (argc<2) rb_raise(rb_eTypeError, "too few parameters");
675826
Check_Type(argv[0], T_FLOAT);
676827
Check_Type(argv[1], T_FLOAT);
@@ -771,6 +922,38 @@ void Init_RXbyak(void) {
771922
rb_define_method(rb_cRXbyak, "neg", RB_FUNC(RXbyak_neg), 1);
772923
rb_define_method(rb_cRXbyak, "not", RB_FUNC(RXbyak_not), 1);
773924

925+
rb_define_method(rb_cRXbyak, "L", RB_FUNC(RXbyak_label), 1);
926+
927+
rb_define_method(rb_cRXbyak, "jmp", RB_FUNC(RXbyak_jmp), -1);
928+
rb_define_method(rb_cRXbyak, "jo", RB_FUNC(RXbyak_jo), -1);
929+
rb_define_method(rb_cRXbyak, "jno", RB_FUNC(RXbyak_jno), -1);
930+
rb_define_method(rb_cRXbyak, "jb", RB_FUNC(RXbyak_jb), -1);
931+
rb_define_method(rb_cRXbyak, "jnae", RB_FUNC(RXbyak_jnae), -1);
932+
rb_define_method(rb_cRXbyak, "jnb", RB_FUNC(RXbyak_jnb), -1);
933+
rb_define_method(rb_cRXbyak, "jae", RB_FUNC(RXbyak_jae), -1);
934+
rb_define_method(rb_cRXbyak, "je", RB_FUNC(RXbyak_je), -1);
935+
rb_define_method(rb_cRXbyak, "jz", RB_FUNC(RXbyak_jz), -1);
936+
rb_define_method(rb_cRXbyak, "jne", RB_FUNC(RXbyak_jne), -1);
937+
rb_define_method(rb_cRXbyak, "jnz", RB_FUNC(RXbyak_jnz), -1);
938+
rb_define_method(rb_cRXbyak, "jbe", RB_FUNC(RXbyak_jbe), -1);
939+
rb_define_method(rb_cRXbyak, "jna", RB_FUNC(RXbyak_jna), -1);
940+
rb_define_method(rb_cRXbyak, "jnbe", RB_FUNC(RXbyak_jnbe), -1);
941+
rb_define_method(rb_cRXbyak, "ja", RB_FUNC(RXbyak_ja), -1);
942+
rb_define_method(rb_cRXbyak, "js", RB_FUNC(RXbyak_js), -1);
943+
rb_define_method(rb_cRXbyak, "jns", RB_FUNC(RXbyak_jns), -1);
944+
rb_define_method(rb_cRXbyak, "jp", RB_FUNC(RXbyak_jp), -1);
945+
rb_define_method(rb_cRXbyak, "jpe", RB_FUNC(RXbyak_jpe), -1);
946+
rb_define_method(rb_cRXbyak, "jnp", RB_FUNC(RXbyak_jnp), -1);
947+
rb_define_method(rb_cRXbyak, "jpo", RB_FUNC(RXbyak_jpo), -1);
948+
rb_define_method(rb_cRXbyak, "jl", RB_FUNC(RXbyak_jl), -1);
949+
rb_define_method(rb_cRXbyak, "jnge", RB_FUNC(RXbyak_jnge), -1);
950+
rb_define_method(rb_cRXbyak, "jnl", RB_FUNC(RXbyak_jnl), -1);
951+
rb_define_method(rb_cRXbyak, "jge", RB_FUNC(RXbyak_jge), -1);
952+
rb_define_method(rb_cRXbyak, "jle", RB_FUNC(RXbyak_jle), -1);
953+
rb_define_method(rb_cRXbyak, "jng", RB_FUNC(RXbyak_jng), -1);
954+
rb_define_method(rb_cRXbyak, "jnle", RB_FUNC(RXbyak_jnle), -1);
955+
rb_define_method(rb_cRXbyak, "jg", RB_FUNC(RXbyak_jg), -1);
956+
774957
rb_define_method(rb_cRXbyak, "method_missing", RB_FUNC(RXbyak_method_missing), -1);
775958

776959
rb_define_method(rb_cRXbyak, "exec", RB_FUNC(RXbyak_exec), -1);

rxbyak/test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
require 'RXbyak'
2+
23
rx = RXbyak.new
34
def rx.code
45
mov eax, [esp, 8]
56
movq xmm0, [eax]
67
mov eax, [esp, 12]
78
movq xmm1, [eax]
9+
L:loop
810
divsd xmm0, xmm1
911
mov eax, [esp, 4]
1012
movq [eax], xmm0
1113
ret
1214
end
1315
rx.code
1416

15-
p rx.eax
16-
1717
puts rx.exec(256.0, 256.0)
1818
puts rx.exec(123.45, 678.9)

0 commit comments

Comments
 (0)