Пример #1
0
int main(int argc, char *argv[]) {
//****************************************************
// Хочется получить трассу запросов для:
	int *a, *b; // a == 1000, b == 2000

	for (int i = 0; i < 10000; i++) {
		a[i] = a[i] + b[i];
		print_read((void *)(1000 + i * sizeof(int)));
		print_read((void *)(2000 + i * sizeof(int)));
		print_write((void *)(1000 + i * sizeof(int)));
	}

	return 0;
}
/* 
    Print statments in proc
*/
void print_stmt(Stmt stmt, char* proc_id) {

    // Print based on statement kind
    switch (stmt->kind) {
        case STMT_ASSIGN:
            print_assign(stmt->info.assign, proc_id);
            break;      
        case STMT_ASSIGN_ARRAY:
            print_assign_array(stmt->info.assign, 
                stmt->info.assign.exprs, proc_id);
            break;
        case STMT_COND:{
            printf("#if\n");

            // Form label
            char ifCountStr[15];
            char str[80];
            if (stmt->info.cond.else_branch != NULL) {
                sprintf(ifCountStr, "%d", ifcount);
                strcpy (str,"else_");
                strcat (str,ifCountStr);
            }
            else {
                sprintf(ifCountStr, "%d", ifcount);
                strcpy (str,"end_if_");
                strcat (str,ifCountStr);
            }

            int currentIfCount = ifcount;
            ifcount++;
            
            // print conditions of if statment
            print_cond(stmt->info.cond.cond, proc_id, 0, 0, str, "");

            // Print then statements
            print_stmts(stmt->info.cond.then_branch, proc_id);

            // Print labels
            char endIfLabel[80];
            char currCountStr[15];
            sprintf(currCountStr, "%d", currentIfCount);
            strcpy (endIfLabel,"end_if_");
            strcat (endIfLabel,currCountStr);

            // Print else statements
            if (stmt->info.cond.else_branch != NULL) {
                // Branch unconditional
                printf("branch_uncond %s\n", endIfLabel);

                // Do False
                printf("%s:\n", str);

                print_stmts(stmt->info.cond.else_branch, proc_id);
            }
        
            // print label
            printf("%s:\n", endIfLabel);

            printf("\n");
            break;
        }
        case STMT_WHILE:{

            // Print start of while label
            printf("#while\n");
            char whileStr[15];
            sprintf(whileStr, "%d", loopcount);
            char top[80];
            strcpy (top,"top_of_loop_");
            strcat (top,whileStr);

            char loopbreak[80];
            strcpy (loopbreak,"loop_is_false_");
            strcat (loopbreak,whileStr);

            int currentLoopCount = loopcount;
            loopcount++;

            // print while condition
            print_cond(stmt->info.loop.cond, proc_id, 0, 1, top, loopbreak);

            // Print statements
            print_stmts(stmt->info.loop.body, proc_id);

            // Print unconditional branch
            printf("branch_uncond %s\n", top);

            // Print end loop label
            printf("%s:\n", loopbreak);
            printf("\n");

            break;
        }
        case STMT_READ:
            print_read(stmt, proc_id);
            break;      
        case STMT_READ_ARRAY:
            print_read_array(stmt, proc_id);
            break;
        case STMT_WRITE:
            print_write(stmt, proc_id);
            break;
        case STMT_FNCALL:
            print_fncall(stmt, proc_id);
            break;
    }
}