bf uses Label class
This commit is contained in:
parent
613922bda3
commit
1de435ed04
1 changed files with 14 additions and 16 deletions
|
@ -10,12 +10,6 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class Brainfuck : public Xbyak::CodeGenerator {
|
class Brainfuck : public Xbyak::CodeGenerator {
|
||||||
private:
|
|
||||||
enum Direction { B, F };
|
|
||||||
std::string toStr(int labelNo, Direction dir)
|
|
||||||
{
|
|
||||||
return Xbyak::Label::toStr(labelNo) + (dir == B ? 'B' : 'F');
|
|
||||||
}
|
|
||||||
public:
|
public:
|
||||||
int getContinuousChar(std::istream& is, char c)
|
int getContinuousChar(std::istream& is, char c)
|
||||||
{
|
{
|
||||||
|
@ -67,8 +61,7 @@ public:
|
||||||
mov(pGetchar, rsi); // getchar
|
mov(pGetchar, rsi); // getchar
|
||||||
mov(stack, rdx); // stack
|
mov(stack, rdx); // stack
|
||||||
#endif
|
#endif
|
||||||
int labelNo = 0;
|
std::stack<Label> labelF, labelB;
|
||||||
std::stack<int> keepLabelNo;
|
|
||||||
char c;
|
char c;
|
||||||
while (is >> c) {
|
while (is >> c) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
@ -116,17 +109,22 @@ public:
|
||||||
mov(cur, eax);
|
mov(cur, eax);
|
||||||
break;
|
break;
|
||||||
case '[':
|
case '[':
|
||||||
L(toStr(labelNo, B));
|
{
|
||||||
mov(eax, cur);
|
Label B = L();
|
||||||
test(eax, eax);
|
labelB.push(B);
|
||||||
jz(toStr(labelNo, F), T_NEAR);
|
mov(eax, cur);
|
||||||
keepLabelNo.push(labelNo++);
|
test(eax, eax);
|
||||||
|
Label F;
|
||||||
|
jz(F, T_NEAR);
|
||||||
|
labelF.push(F);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ']':
|
case ']':
|
||||||
{
|
{
|
||||||
int no = keepLabelNo.top(); keepLabelNo.pop();
|
Label B = labelB.top(); labelB.pop();
|
||||||
jmp(toStr(no, B));
|
jmp(B);
|
||||||
L(toStr(no, F));
|
Label F = labelF.top(); labelF.pop();
|
||||||
|
L(F);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue