Skip to content

Commit

Permalink
bf uses Label class
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Sep 4, 2018
1 parent 613922b commit 1de435e
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions sample/bf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
#endif

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:
int getContinuousChar(std::istream& is, char c)
{
Expand Down Expand Up @@ -67,8 +61,7 @@ class Brainfuck : public Xbyak::CodeGenerator {
mov(pGetchar, rsi); // getchar
mov(stack, rdx); // stack
#endif
int labelNo = 0;
std::stack<int> keepLabelNo;
std::stack<Label> labelF, labelB;
char c;
while (is >> c) {
switch (c) {
Expand Down Expand Up @@ -116,17 +109,22 @@ class Brainfuck : public Xbyak::CodeGenerator {
mov(cur, eax);
break;
case '[':
L(toStr(labelNo, B));
mov(eax, cur);
test(eax, eax);
jz(toStr(labelNo, F), T_NEAR);
keepLabelNo.push(labelNo++);
{
Label B = L();
labelB.push(B);
mov(eax, cur);
test(eax, eax);
Label F;
jz(F, T_NEAR);
labelF.push(F);
}
break;
case ']':
{
int no = keepLabelNo.top(); keepLabelNo.pop();
jmp(toStr(no, B));
L(toStr(no, F));
Label B = labelB.top(); labelB.pop();
jmp(B);
Label F = labelF.top(); labelF.pop();
L(F);
}
break;
default:
Expand Down

0 comments on commit 1de435e

Please sign in to comment.