aboutsummaryrefslogtreecommitdiff
Refinement of the CMiLan compiler

Refinement of the CMiLan compiler
Adding function support

Introduction

Milan is an educational procedural programming language. The standard distribution of the CMilan compiler supports the definition of only one procedure, which is the entry point of the program. In this laboratory work, the compiler is extended to implement declarations, definitions, and function calls.

1. Refinement description

All functions must be declared before the entry point is defined, and defined after it. In the function declarations, you need to specify the number of arguments and indicate whether it returns a single value or does not return anything.

Several functions can have the same name if they have different numbers of arguments (function-overloading). You can declare local variables in functions, which shadow global variables. The compiler eliminates tail calls.

Obviously, some changes had to be made to the virtual machine as well, i.e. stack framing.

2. Documentation

The exported report can be found at this link.

3. Showcase

3.1. Doubling a number

Source code:

/* double.mil --- read double write                            -*- MiLan -*- */
/* function declarations */
FUNC double (1) RETURNS;

/* program */
BEGIN
        LOCALS n;
        n := READ;
        n := double (n);
        WRITE (n)
END

/* function definitions */
FUNC double (n) RETURNS
BEGIN
        RETURN (n + n)
END
/* double.mil ends here */

Assembly:

0:      SHIFT   1
1:      INPUT
2:      STOREB  0
3:      LOADB   0
4:      CALL    9
5:      STOREB  0
6:      LOADB   0
7:      PRINT
8:      STOP
9:      LOADB   -3
10:     LOADB   -3
11:     ADD
12:     STOREB  -3
13:     RETURN  0

Usage:

> 10
20

3.2. GCD

Source code:

/* gcd.mil -- compute greatest common divisor                  -*- MiLan -*- */
FUNC gcd (0);

BEGIN
        a := READ;
        b := READ;
        gcd ();
        WRITE (c)
END

FUNC gcd ()
BEGIN
        WHILE a != b DO
              IF a < b THEN b := b - a
                       ELSE a := a - b FI OD;
        c := a
END
/* gcd.mil ends here */

Assembly:

0:      INPUT
1:      STORE   0
2:      INPUT
3:      STORE   1
4:      CALL    8
5:      LOAD    2
6:      PRINT
7:      STOP
8:      LOAD    0
9:      LOAD    1
10:     COMPARE 1
11:     JUMP_NO 26
12:     LOAD    0
13:     LOAD    1
14:     COMPARE 2
15:     JUMP_NO 21
16:     LOAD    1
17:     LOAD    0
18:     SUB
19:     STORE   1
20:     JUMP    25
21:     LOAD    0
22:     LOAD    1
23:     SUB
24:     STORE   0
25:     JUMP    8
26:     LOAD    0
27:     STORE   2
28:     RETURN  0

Usage:

> 9
> 6
3

3.3. Factorial (w/o tail-call elimination)

Source code:

/* fac.mil --- compute factorial of a number                   -*- MiLan -*- */
FUNC fac (1) RETURNS;

BEGIN
        WRITE (fac (READ))
END

FUNC fac (n) RETURNS
BEGIN
        IF n = 0 THEN RETURN 1 FI;
        RETURN (n*fac (n - 1))
END
/* fac.mil end here */

Assembly:

0:      INPUT
1:      CALL    4
2:      PRINT
3:      STOP
4:      LOADB   -3
5:      PUSH    0
6:      COMPARE 0
7:      JUMP_NO 11
8:      PUSH    1
9:      STOREB  -3
10:     RETURN  0
11:     LOADB   -3
12:     LOADB   -3
13:     PUSH    1
14:     SUB
15:     CALL    4
16:     MULT
17:     STOREB  -3
18:     RETURN  0

Usage:

> 31
738197504

3.4. Factorial (w/ tail-call elimination)

Source code:

/* fac-tail.mil --- compute factorial of a number using tail recursion -*- MiLan -*- */
FUNC fac (1) RETURNS;
FUNC fac (2) RETURNS;

BEGIN
        WRITE (fac (READ))
END

FUNC fac (n) RETURNS
BEGIN
        RETURN fac (n, 1)
END

FUNC fac (n acc) RETURNS
BEGIN
        IF n = 0 THEN RETURN acc FI;
        RETURN fac (n - 1, n*acc)
END
/* fac-tail.mil end here */

Assembly:

0:      INPUT
1:      CALL    4
2:      PRINT
3:      STOP
4:      LOADB   -3
5:      PUSH    1
6:      CALL    9
7:      STOREB  -3
8:      RETURN  0
9:      LOADB   -4
10:     PUSH    0
11:     COMPARE 0
12:     JUMP_NO 16
13:     LOADB   -3
14:     STOREB  -4
15:     RETURN  1
16:     LOADB   -4
17:     PUSH    1
18:     SUB
19:     LOADB   -4
20:     LOADB   -3
21:     MULT
22:     STOREB  -3
23:     STOREB  -4
24:     JUMP    9

Usage:

> 31
738197504

3.5. Fibonacci (w/o tail-call elimination)

Source code:

/* fib.mil --- compute fibonacci number                        -*- MiLan -*- */
FUNC fib (1) RETURNS;

BEGIN
        WRITE (fib (READ))
END

FUNC fib (n) RETURNS
BEGIN
        IF n < 2 THEN RETURN (n) FI;
        RETURN (fib (n - 1) + fib (n - 2))
END
/* fib.mil ends here */

Assembly:

0:      INPUT
1:      CALL    4
2:      PRINT
3:      STOP
4:      LOADB   -3
5:      PUSH    2
6:      COMPARE 2
7:      JUMP_NO 11
8:      LOADB   -3
9:      STOREB  -3
10:     RETURN  0
11:     LOADB   -3
12:     PUSH    1
13:     SUB
14:     CALL    4
15:     LOADB   -3
16:     PUSH    2
17:     SUB
18:     CALL    4
19:     ADD
20:     STOREB  -3
21:     RETURN  0

Usage:

> 20000
Error: stack overflow
Code:

        14      CALL            4
VM error

3.6. Fibonacci (w/ tail-call elimination)

Source code:

/* fib-tail.mil --- compute fibonacci number using tail recursion -*- MiLan -*- */
FUNC fib (1) RETURNS;
FUNC fib (3) RETURNS;

BEGIN
        WRITE (fib (READ))
END

FUNC fib (n) RETURNS
BEGIN
        RETURN fib (0, 1, n)
END

FUNC fib (a b n) RETURNS
BEGIN
        IF n = 0 THEN RETURN a FI;
        IF n = 1 THEN RETURN b FI;
        RETURN fib (b, a + b, n - 1)
END
/* fib-tail.mil end here*/

Assembly:

0:      INPUT
1:      CALL    4
2:      PRINT
3:      STOP
4:      PUSH    0
5:      PUSH    1
6:      LOADB   -3
7:      CALL    10
8:      STOREB  -3
9:      RETURN  0
10:     LOADB   -3
11:     PUSH    0
12:     COMPARE 0
13:     JUMP_NO 17
14:     LOADB   -5
15:     STOREB  -5
16:     RETURN  2
17:     LOADB   -3
18:     PUSH    1
19:     COMPARE 0
20:     JUMP_NO 24
21:     LOADB   -4
22:     STOREB  -5
23:     RETURN  2
24:     LOADB   -4
25:     LOADB   -5
26:     LOADB   -4
27:     ADD
28:     LOADB   -3
29:     PUSH    1
30:     SUB
31:     STOREB  -3
32:     STOREB  -4
33:     STOREB  -5
34:     JUMP    10

Usage:

> 1000000
1884755131

4. Unlicense

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to https://unlicense.org/

Date: 2025H1

Author: Martin Michalec

Created: 2026-11-08 Sun 10:01