void DoDo(void)
{
        char code[MAXMSG];
        char l1[MAXLBL];
        char l2[MAXLBL];

        Match('d');
        NewLabel();
        strncpy(l1, label, MAXLBL);
        NewLabel();
        strncpy(l2, label, MAXLBL); // exit point
	printf("# DO\n");

	Expression(); // expr1 = repeat count
	EmitLn("pushl %eax\t\t# repeat count");

        PostLabel(l1);
	Block(l2);

	EmitLn("popl %ecx");
	EmitLn("dec %ecx");
	EmitLn("pushl %ecx");

	// test
        snprintf(code, MAXMSG, "jnz .%s", l1);
        EmitLn(code);
	
	Match('e'); // ENDWHILE
	printf("#ENDDO\n");
	PostLabel(l2);
}
void DoIf(char *exit_label)
{
        char code[MAXMSG];
	char l1[MAXLBL];
	char l2[MAXLBL];

	Match('i');
	printf("# IF\n");
	NewLabel();
	strncpy(l1, label, MAXLBL);
	strncpy(l2, label, MAXLBL);
	BoolExpression();

        snprintf(code, MAXMSG, "je .%s", l1);
	EmitLn(code);
	printf("# TRUE\n");
	// here, tutorial 05 only matches 'e' once, after the if statement
	// this doesn't seem to work unless i add Look=='l' to the test in
	// Blocks().

	Block(exit_label);
	if (Look=='l') {
		Match('l');
		printf("#ELSE\n");
		NewLabel();
		strncpy(l2, label, MAXLBL);
		snprintf(code, MAXMSG, "jmp .%s", l2); 
		EmitLn(code);
		PostLabel(l1);
		Block(exit_label);
	}
	Match('e'); // ENDIF
	printf("#ENDIF\n");
	PostLabel(l2);
}
void DoIf()
{
    Condition();
    char L1[MAX_BUF];
    char L2[MAX_BUF];
    strcpy(L1, NewLabel());
    strcpy(L2, L1);

    sprintf(tmp, "jz %s", L1);
    EmitLn(tmp);

    Block();

    if (Token == 'l') {
        /* match *else* statement */
        strcpy(L2, NewLabel());

        sprintf(tmp, "jmp %s", L2);
        EmitLn(tmp);

        PostLabel(L1);

        Block();
    }

    PostLabel(L2);
    MatchString("ENDIF");
}
void DoIf(char *L)
{
    char L1[MAX_BUF];
    char L2[MAX_BUF];
    strcpy(L1, NewLabel());
    strcpy(L2, L1);

    Match('i');
    BoolExpression();

    sprintf(tmp, "jz %s", L1);
    EmitLn(tmp);

    Block(L);
    dprint("DoIf: Got Look = %c\n", Look);

    if (Look == 'l') {
        /* match *else* statement */
        Match('l');
        strcpy(L2, NewLabel());

        sprintf(tmp, "jmp %s", L2);
        EmitLn(tmp);

        PostLabel(L1);

        Block(L);
    }

    Match('e');
    PostLabel(L2);
}
void DoWhile(void)
{
        char code[MAXMSG];
        char l1[MAXLBL];
        char l2[MAXLBL];

        Match('w');
        NewLabel();
        strncpy(l1, label, MAXLBL);
	NewLabel();
        strncpy(l2, label, MAXLBL);

        PostLabel(l1);
	BoolExpression();

        snprintf(code, MAXMSG, "je .%s", l2);
        EmitLn(code);
	Block(l2);

	Match('e'); // ENDWHILE
	printf("#ENDWHILE\n");
	snprintf(code, MAXMSG, "jmp .%s", l1);
	EmitLn(code);

	PostLabel(l2);
}
/* I haven't test the actual generated x86 code here, so you're free to
 * inform me if there are bugs. :) */
void DoFor()
{
    char L1[MAX_BUF];
    char L2[MAX_BUF];

    Match('f');
    strcpy(L1, NewLabel());
    strcpy(L2, NewLabel());
    char name = GetName();
    Match('=');
    Expression();
    EmitLn("subl %eax, $1");  /* SUBQ #1, D0*/
    sprintf(tmp, "lea %c, %%edx", name);
    EmitLn(tmp);
    EmitLn("movl %eax, (%edx)");
    Expression();
    EmitLn("push %eax"); /* save the execution of expression */
    PostLabel(L1);
    sprintf(tmp, "lea %c, %%edx", name);
    EmitLn(tmp);
    EmitLn("movl (%edx), %eax");
    EmitLn("addl %eax, 1");
    EmitLn("movl %eax, (%edx)");
    EmitLn("cmp (%esp), %eax");
    sprintf(tmp, "jg %s", L2);
    EmitLn(tmp);
    Block(L2);
    Match('e');
    sprintf(tmp, "jmp %s", L1);
    EmitLn(tmp);
    PostLabel(L2);
    EmitLn("pop %eax");
}
void DoFor() {
  char L1[100];
  char L2[100];

  Match('f');
  strcpy(L1, NewLabel());
  strcpy(L2, NewLabel());
  char Name = GetName();

  Match('=');
  Expression();
  EmitLn("dec rax");
  sprintf(tmp, "mov qword [%c], rax", Name);
  EmitLn(tmp);
  Expression();
  EmitLn("push rax");
  PostLabel(L1);
  sprintf(tmp, "mov rax, qword [%c]", Name);
  EmitLn(tmp);
  EmitLn("inc rax");
  sprintf(tmp, "mov qword [%c], rax", Name);
  EmitLn(tmp);
  EmitLn("cmp rax, qword [rsp]");
  sprintf(tmp, "jg %s", L2);
  EmitLn(tmp);
  Block();
  Match('e'); 
  sprintf(tmp, "jmp %s", L1);
  EmitLn(tmp);
  PostLabel(L2);
  EmitLn("add rsp, 8");

}
void DoFor(void)
{
	char name;
        char code[MAXMSG];
        char l1[MAXLBL];
        char l2[MAXLBL];

        Match('f');
	printf("# FOR\n");
        NewLabel();
        strncpy(l1, label, MAXLBL);
	NewLabel();
        strncpy(l2, label, MAXLBL);

	// could call Assignment here, but I need to keep the loop counter handle
	name = GetName();
	Match('=');
	Expression(); // expr1 = initial value
	snprintf(code, MAXMSG, "movl $%c,%%edx", name);
	EmitLn(code);
	EmitLn("movl %eax,(%edx)\t\t# assignment");

	Expression(); // expr2 = target value
	EmitLn("pushl %eax    \t\t # put target count on stack");

        // Loop: 
	PostLabel(l1);
	EmitLn("popl %eax  \t\t# recover tgt");
	EmitLn("pushl %eax");
        snprintf(code, MAXMSG, "movl $%c,%%edx", name);
        EmitLn(code);
	EmitLn("movl (%edx), %ecx  \t\t # Grab loop counter");

	// test
	EmitLn("cmpl %eax, %ecx\t\t# for test");
        snprintf(code, MAXMSG, "jg .%s", l2);
        EmitLn(code);
	
	Block(l2);
	//EmitLn("popl %ecx  \t\t# don't need return val");

	// loopback
        snprintf(code, MAXMSG, "movl $%c,%%edx", name); 
	EmitLn(code);
	EmitLn("movl (%edx), %ecx    \t\t # grab loop counter");
	EmitLn("inc %ecx  \t\t # i++");
	EmitLn("movl %ecx, (%edx)");
	snprintf(code, MAXMSG, "jmp .%s", l1);
        EmitLn(code);
	PostLabel(l2);

	Match('e'); // ENDWHILE 
	EmitLn("popl %eax");

	printf("#ENDFOR\n");

}
void DoLoop()
{
    char L1[MAX_BUF];
    char L2[MAX_BUF];
    Match('p');
    strcpy(L1, NewLabel());
    strcpy(L2, NewLabel());
    PostLabel(L1);
    Block(L2);
    Match('e');
    sprintf(tmp, "jmp %s", L1);
    EmitLn(tmp);
    PostLabel(L2);
}
void DoWhile() {
  char L1[100];
  char L2[100];

  strcpy(L1, NewLabel());
  strcpy(L2, NewLabel());
  PostLabel(L1);
  BoolExpression();
  BranchFalse(L2);
  Block();
  MatchString("ENDWHILE");
  Branch(L1);
  PostLabel(L2);
}
void DoWhile()
{
    Next();
    char L1[MAX_BUF];
    char L2[MAX_BUF];
    sprintf(L1, NewLabel());
    sprintf(L2, NewLabel());
    PostLabel(L1);
    BoolExpression();
    BranchFalse(L2);
    Block();
    MatchString("ENDWHILE");
    Branch(L1);
    PostLabel(L2);
}
void DoRepeat()
{
    char L1[MAX_BUF];
    char L2[MAX_BUF];
    Match('r');
    strcpy(L1, NewLabel());
    strcpy(L2, NewLabel());
    PostLabel(L1);
    Block(L2);
    Match('u');
    BoolExpression();

    sprintf(tmp, "jz %s", L1);
    EmitLn(tmp);
    PostLabel(L2);
}
void DoBlock(char Name) {
  Declarations();
  sprintf(tmp, "%c", Name);
  PostLabel("main");
  EmitLn("push rbp");
  Statements();
}
void DoWhile() {
  char L1[100];
  char L2[100];

  Match('w');
  strcpy(L1, NewLabel());
  strcpy(L2, NewLabel());
  PostLabel(L1);
  BoolExpression();
  sprintf(tmp, "jz %s", L2);
  EmitLn(tmp);
  Block();
  Match('e');
  sprintf(tmp, "jmp %s", L1);
  EmitLn(tmp);
  PostLabel(L2);
}
void DoIf() {
  char L1[100];
  char L2[100];
  BoolExpression();
  strcpy(L1,  NewLabel());
  strcpy(L2,  L1);
  BranchFalse(L1);
  Block();
  if(Token == 'l') {
    Match('l');
    strcpy(L2,  NewLabel());
    Branch(L2);
    PostLabel(L1);
    Block();
  }
  PostLabel(L2);
  MatchString("ENDIF");
}
void DoLoop() {
  char L[100];
  Match('p');
  strcpy(L, NewLabel());
  PostLabel(L);
  Block();
  Match('e');
  sprintf(tmp, "jmp %s", L);
  EmitLn(tmp);
}
void DoDo()
{
    Match('d');
    char L1[MAX_BUF];
    char L2[MAX_BUF];
    strcpy(L1, NewLabel());
    strcpy(L2, NewLabel());
    Expression();
    EmitLn("subl %eax, $1");
    EmitLn("movl %eax, %ecx");
    PostLabel(L1);
    EmitLn("pushl %ecx");
    Block(L2);
    EmitLn("popl %ecx");
    sprintf(tmp, "loop %s", L1);
    EmitLn(tmp);
    EmitLn("pushl %ecx");
    PostLabel(L2);
    EmitLn("popl %ecx");
}
/* Recognize and Translate an IF construct */
void DoIf()
{
    Next();
    char L1[MAX_BUF];
    char L2[MAX_BUF];
    sprintf(L1, NewLabel());
    sprintf(L2, L1);
    BoolExpression();
    BranchFalse(L1);
    Block();
    if (Token == 'l') {
        Next();
        sprintf(L2, NewLabel());
        Branch(L2);
        PostLabel(L1);
        Block();
    }
    PostLabel(L2);
    MatchString("ENDIF");
}
void DoRepeat() {
  char L[100];
  Match('r');
  strcpy(L, NewLabel());
  PostLabel(L);
  Block();
  Match('u');
  BoolExpression();
  sprintf(tmp, "jmp %s", L);
  EmitLn(tmp);
}
void DoLoop(void)
{
        char code[MAXMSG];
        char l1[MAXLBL];
        char l2[MAXLBL];

        Match('p');
        NewLabel();
        strncpy(l1, label, MAXLBL);
	NewLabel();
	strncpy(l2, label, MAXLBL); // exit point

	PostLabel(l1);
	Block(l2);
	Match('e');
        snprintf(code, MAXMSG, "jmp .%s", l1);
        EmitLn(code);
	printf("#ENDLOOP\n");	
        PostLabel(l2);
}
void DoIf() {
  char L1[100];
  char L2[100];
  Match('i');
  BoolExpression();
  strcpy(L1,  NewLabel());
  strcpy(L2,  L1);
  sprintf(tmp, "jz %s", L1);
  EmitLn(tmp);
  Block();
  if(Look == 'l') {
    Match('l');
    strcpy(L2,  NewLabel());
    sprintf(tmp, "jmp %s", L2);
    EmitLn(tmp);
    PostLabel(L1);
    Block();
  }
  Match('e');
  PostLabel(L2);
}
void DoRepeat(void)
{
        char code[MAXMSG];
        char l1[MAXLBL];
        char l2[MAXLBL];

        Match('r');
	printf("# REPEAT\n");
        NewLabel();
        strncpy(l1, label, MAXLBL);
        NewLabel();
        strncpy(l2, label, MAXLBL);
        PostLabel(l1);
	Block(l2);
	Match('u');
	printf("# UNTIL\n");
	BoolExpression();
        snprintf(code, MAXMSG, "je .%s\t\t#repeat loop", l1);
        EmitLn(code);

	PostLabel(l2); // exit point
}
Exemple #23
0
void DoBlock(char name){
    Declarations();
    PostLabel(name);
    Statements();
}
void Prolog() {
  EmitLn("section .text");
  EmitLn("global  main");
  PostLabel("main");
  EmitLn("push rbp");
}