コード例 #1
0
ファイル: transtmt.c プロジェクト: zhanglp92/splos_parser
/**
 * 翻译for 语句
 *
 * for (expr1; expr2; expr3) stmt is translated into
 *     expr1
 *     goto testBB
 * loopBB:
 *     stmt
 * contBB:
 *     expr3
 * testBB:
 *     if expr2 goto loopBB (goto loopBB if expr2 is NULL)
 * nextBB:
 *     ...
 * Please pay attention to the difference between for and while
 * The continue point and loop test point is same for a while statemnt,
 * but different for a for statment.
 */
static void TranslateForStatement(AstStatement stmt)
{
	AstForStatement forStmt = AsFor(stmt);

	forStmt->loopBB = CreateBBlock ();
	forStmt->contBB = CreateBBlock ();
	forStmt->testBB = CreateBBlock ();
	forStmt->nextBB = CreateBBlock ();

    /* for 的初始条件 */
	if (forStmt->initExpr) {

		TranslateExpression (forStmt->initExpr);
	}
    /* 跳转到条件判断 */
	GenerateJump (forStmt->testBB);

    /* 循环体 */
	StartBBlock (forStmt->loopBB);
	TranslateStatement (forStmt->stmt);

    /* 步长 */
	StartBBlock (forStmt->contBB);
	if (forStmt->incrExpr) {

		TranslateExpression (forStmt->incrExpr);
	}

    /* 测试条件,是否跳转 */
	StartBBlock (forStmt->testBB);
	if (forStmt->expr) {

		TranslateBranch (forStmt->expr, forStmt->loopBB, forStmt->nextBB);
	} else {

		GenerateJump (forStmt->loopBB);
	}

	StartBBlock (forStmt->nextBB);
}
コード例 #2
0
ファイル: dumpast.c プロジェクト: descent/ucc-code
static void DumpStatement(AstStatement stmt, int pos)
{
	switch (stmt->kind)
	{
	case NK_ExpressionStatement:

		DumpExpression(AsExpr(stmt)->expr, pos);
		break;

	case NK_LabelStatement:

		fprintf(ASTFile, "(label %s:\n", AsLabel(stmt)->id);
		LeftAlign(ASTFile, pos + 2);
		DumpStatement(AsLabel(stmt)->stmt, pos + 2);
		LeftAlign(ASTFile, pos);
		fprintf(ASTFile, "end-label)");
		break;

	case NK_CaseStatement:

		fprintf(ASTFile, "(case  ");
		DumpExpression(AsCase(stmt)->expr, pos + 7);
		LeftAlign(ASTFile, pos + 2);
		DumpStatement(AsCase(stmt)->stmt, pos + 2);
		LeftAlign(ASTFile, pos);
		fprintf(ASTFile, "end-case)");
		break;

	case NK_DefaultStatement:

		fprintf(ASTFile, "(default  ");
		DumpStatement(AsDef(stmt)->stmt, pos + 10);
		LeftAlign(ASTFile, pos);
		fprintf(ASTFile, "end-default)");
		break;

	case NK_IfStatement:

		fprintf(ASTFile, "(if  ");
		DumpExpression(AsIf(stmt)->expr, pos + 5);
		LeftAlign(ASTFile, pos + 2);
		fprintf(ASTFile, "(then  ");
		LeftAlign(ASTFile, pos + 4);
		DumpStatement(AsIf(stmt)->thenStmt, pos + 4);
		LeftAlign(ASTFile, pos + 2);
		fprintf(ASTFile, "end-then)");
		if (AsIf(stmt)->elseStmt != NULL)
		{
			LeftAlign(ASTFile, pos + 2);
			fprintf(ASTFile, "(else  ");
			LeftAlign(ASTFile, pos + 4);
			DumpStatement(AsIf(stmt)->elseStmt, pos + 4);
			LeftAlign(ASTFile, pos + 2);
			fprintf(ASTFile, "end-else)");
		}
		LeftAlign(ASTFile, pos);
		fprintf(ASTFile, "end-if)");
		break;

	case NK_SwitchStatement:

		fprintf(ASTFile, "(switch ");
		DumpExpression(AsSwitch(stmt)->expr, pos + 9);
		LeftAlign(ASTFile, pos + 2);
		DumpStatement(AsSwitch(stmt)->stmt, pos + 2);
		LeftAlign(ASTFile, pos);
		fprintf(ASTFile, "end-switch)");
		break;

	case NK_WhileStatement:

		fprintf(ASTFile, "(while  ");
		DumpExpression(AsLoop(stmt)->expr, pos + 8);
		LeftAlign(ASTFile, pos + 2);
		DumpStatement(AsLoop(stmt)->stmt, pos + 2);
		LeftAlign(ASTFile, pos);
		fprintf(ASTFile, "end-while)");
		break;

	case NK_DoStatement:

		fprintf(ASTFile, "(do  ");
		DumpExpression(AsLoop(stmt)->expr, pos + 5);
		LeftAlign(ASTFile, pos + 2);
		DumpStatement(AsLoop(stmt)->stmt, pos + 2);
		LeftAlign(ASTFile, pos);
		fprintf(ASTFile, ")");
		break;

	case NK_ForStatement:

		fprintf(ASTFile, "(for  ");
		DumpExpression(AsFor(stmt)->initExpr, pos + 6);
		LeftAlign(ASTFile, pos + 6);
		DumpExpression(AsFor(stmt)->expr, pos + 6);
		LeftAlign(ASTFile, pos + 6);
		DumpExpression(AsFor(stmt)->incrExpr, pos + 6);
		LeftAlign(ASTFile, pos + 2);
		DumpStatement(AsFor(stmt)->stmt, pos + 2);
		LeftAlign(ASTFile, pos);
		fprintf(ASTFile, "end-for)");
		break;

	case NK_GotoStatement:

		fprintf(ASTFile, "(goto %s)", AsGoto(stmt)->id);
		break;

	case NK_ContinueStatement:

		fprintf(ASTFile, "(continue)");
		break;

	case NK_BreakStatement:

		fprintf(ASTFile, "(break)");
		break;

	case NK_ReturnStatement:

		fprintf(ASTFile, "(ret ");
		DumpExpression(AsRet(stmt)->expr, pos + 5);
		fprintf(ASTFile, ")");
		break;

	case NK_CompoundStatement:
		{
			AstNode p = ((AstCompoundStatement)stmt)->stmts;

			fprintf(ASTFile, "{");
			while (p != NULL)
			{
				LeftAlign(ASTFile, pos + 2);
				DumpStatement((AstStatement)p, pos + 2);
				if (p->next != NULL)
					fprintf(ASTFile, "\n");
				p = p->next;
			}
			LeftAlign(ASTFile, pos);
			fprintf(ASTFile, "}");
			break;
		}

	default:
		assert(0);
	}
}