9d94e3b81b
- Initialize Rust project configuration (Cargo) and .gitignore - Implement core Intermediate Representation (IR), printer, and builder utilities - Add IR validation module for type checking and constraint verification - Introduce optimization passes: Mem2Reg, Constant Folding, Copy Propagation, Dead Code Elimination, and SSA Destruction - Implement x86_64 backend for assembly code generation - Add a C test harness and main entry point to generate, compile, and test a GCD assembly function
47 lines
1.5 KiB
C
47 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <inttypes.h>
|
|
|
|
// Declare the external assembly function generated by the Rust backend
|
|
extern uint64_t gcd(uint64_t a, uint64_t b);
|
|
|
|
int main() {
|
|
// Define an array of test cases: {a, b, expected_result}
|
|
uint64_t test_cases[][3] = {
|
|
{48, 18, 6},
|
|
{54, 24, 6},
|
|
{7, 13, 1}, // Prime numbers
|
|
{100, 10, 10}, // One is a multiple of the other
|
|
{2740, 1760, 20}, // Larger random numbers
|
|
{1234567890, 90, 90}, // Testing 64-bit bounds
|
|
{5, 0, 5} // Edge case: b is 0 (Your IR handles this perfectly!)
|
|
};
|
|
|
|
int num_cases = sizeof(test_cases) / sizeof(test_cases[0]);
|
|
int passed = 0;
|
|
|
|
printf("Running GCD Assembly Tests...\n");
|
|
printf("-----------------------------\n");
|
|
|
|
for (int i = 0; i < num_cases; i++) {
|
|
uint64_t a = test_cases[i][0];
|
|
uint64_t b = test_cases[i][1];
|
|
uint64_t expected = test_cases[i][2];
|
|
|
|
// Call out into your compiled assembly!
|
|
uint64_t result = gcd(a, b);
|
|
|
|
if (result == expected) {
|
|
printf("[PASS] gcd(%" PRIu64 ", %" PRIu64 ") = %" PRIu64 "\n", a, b, result);
|
|
passed++;
|
|
} else {
|
|
printf("[FAIL] gcd(%" PRIu64 ", %" PRIu64 ") = %" PRIu64 " (Expected: %" PRIu64 ")\n", a, b, result, expected);
|
|
}
|
|
}
|
|
|
|
printf("-----------------------------\n");
|
|
printf("Results: %d/%d passed.\n", passed, num_cases);
|
|
|
|
return (passed == num_cases) ? 0 : 1;
|
|
}
|