|
|
@@ -12,7 +12,7 @@
|
|
|
VM before the main function is called.
|
|
|
|
|
|
{v int glob = 1;
|
|
|
-export int main() \{
|
|
|
+void foo() \{
|
|
|
int foo = 0;
|
|
|
int bar = foo;
|
|
|
\} v}
|
|
|
@@ -21,7 +21,7 @@ export int main() \{
|
|
|
glob = 1;
|
|
|
\}
|
|
|
int glob;
|
|
|
-export int main() \{
|
|
|
+void foo() \{
|
|
|
int foo;
|
|
|
int bar;
|
|
|
foo = 0;
|
|
|
@@ -36,28 +36,32 @@ export int main() \{
|
|
|
generated for each array index. For scalar values and aray constants that
|
|
|
have a lower nesting level than the number of array dimensions, for-loops
|
|
|
are generated. The following example shows all situation:
|
|
|
-{v int[3] a = [1, 2, 3];
|
|
|
-int[3] b = 4;
|
|
|
-int[2, 2] c = [[3, 4], 5]; v}
|
|
|
+{v void foo() \{
|
|
|
+ int[3] a = [1, 2, 3];
|
|
|
+ int[3] b = 4;
|
|
|
+ int[2, 2] c = [[3, 4], 5];
|
|
|
+\} v}
|
|
|
|
|
|
The snippet above is transformed to code equivalent to the following:
|
|
|
|
|
|
-{v int[] a;
|
|
|
-int[] b;
|
|
|
-int[] c;
|
|
|
-a = __allocate(3);
|
|
|
-a[0] = 1;
|
|
|
-a[1] = 2;
|
|
|
-a[2] = 3;
|
|
|
-b = __allocate(3);
|
|
|
-for (int i = 0, 3) \{
|
|
|
- b[i] = 4;
|
|
|
-\}
|
|
|
-c = __allocate(4);
|
|
|
-c[0] = 3;
|
|
|
-c[1] = 4;
|
|
|
-for (int i = 0, 2) \{
|
|
|
- c[1, i] = 5;
|
|
|
+{v void foo() \{
|
|
|
+ int[] a;
|
|
|
+ int[] b;
|
|
|
+ int[] c;
|
|
|
+ a = __allocate(3);
|
|
|
+ a[0] = 1;
|
|
|
+ a[1] = 2;
|
|
|
+ a[2] = 3;
|
|
|
+ b = __allocate(3);
|
|
|
+ for (int i = 0, 3) \{
|
|
|
+ b[i] = 4;
|
|
|
+ \}
|
|
|
+ c = __allocate(4);
|
|
|
+ c[0] = 3;
|
|
|
+ c[1] = 4;
|
|
|
+ for (int i = 0, 2) \{
|
|
|
+ c[1, i] = 5;
|
|
|
+ \}
|
|
|
\} v}
|
|
|
|
|
|
{2 Prevent incorrect double evaluation}
|
|
|
@@ -92,10 +96,13 @@ void foo() \{
|
|
|
and constant propagation):
|
|
|
{v ...
|
|
|
void foo() \{
|
|
|
- int a$dim$$2 = two();
|
|
|
- int const$$1 = two();
|
|
|
- int const$$2 = two();
|
|
|
+ int a$dim$$2;
|
|
|
+ int const$$1;
|
|
|
+ int const$$2;
|
|
|
int[2, a$dim$$2] a;
|
|
|
+ a$dim$$2 = two();
|
|
|
+ const$$1 = two();
|
|
|
+ const$$2 = two();
|
|
|
a = __allocate(2 * a$dim$$2);
|
|
|
a[0] = 1;
|
|
|
a[1] = const$$1;
|
|
|
@@ -108,7 +115,7 @@ void foo() \{
|
|
|
|
|
|
{2 Transforming for-loops to while-loops}
|
|
|
|
|
|
-{v for(int i = <start>,<stop>,<step>) \{
|
|
|
+{v for (int i = <start>, <stop>, <step>) \{
|
|
|
<body>
|
|
|
\} v}
|
|
|
|
|
|
@@ -117,16 +124,17 @@ void foo() \{
|
|
|
{v i$1 = <start>;
|
|
|
step$2 = <step>;
|
|
|
stop$3 = <stop>;
|
|
|
-while((step$2 > 0) ? (i$1 < stop$3) : (i$1 > stop$3)) \{
|
|
|
+while ((step$2 > 0) ? (i$1 < stop$3) : (i$1 > stop$3)) \{
|
|
|
<body>
|
|
|
i$1 = i$1 + step$2;
|
|
|
\} v}
|
|
|
|
|
|
- Where [i$1], [step$2] and [stop$3] are fresh variables. Definitions of these
|
|
|
+ Here, [i$1], [step$2] and [stop$3] are fresh variables. Definitions of these
|
|
|
new variables are added to the scope of the current function. Every
|
|
|
occurrence of [i] in [<body>] is replaced with the fresh variable [i$1],
|
|
|
this prevents problems with nested for-loops that use the same induction
|
|
|
- variable .*)
|
|
|
+ variable.
|
|
|
+ *)
|
|
|
|
|
|
(** Main phase function, called by {!Main}. *)
|
|
|
val phase : Main.phase_func
|