Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Factorial Jako example with IMCC

4 views
Skip to first unread message

gre...@focusresearch.com

unread,
Dec 4, 2002, 11:17:01 AM12/4/02
to perl6-i...@perl.org
All --

Before I go changing the Jako compiler, I'd like some feedback. There are
three files
below. First is fact.jako, the factorial algorithm expressed as a Jako
subroutine with
some mainline code that calls it. Then comes fact.imc, which I produced by
hand-
editing the fact.pasm file that jakoc currently produces for fact.jako.
Finally, the
fact.pasm file that results from running imcc on fact.imc.

You can see the workaround I'm using to allow the programmer to
intersperse chunks
of code to be executed right now with chunks of code that are subroutine
defininitions.


#
# fact.jako
#
# Some simple code to print some factorials
#
# Based on fact.pasm originally be Leon Brocard <ac...@astray.com>
2001-09-14.
#
# Copyright (C) 2001 Gregor N. Purdy. All rights reserved.
# This program is free software. It is subject to the same
# license as Perl itself.
#
# $Id: fact.jako,v 1.5 2001/10/19 12:36:16 gregor Exp $
#

const int N = 15;


#
# fact()
#

sub int fact(int n) {
var int i = 0;
var int f = 1;

while(i <= n) {
i++;
f *= i;
}

return f;
}


#
# MAIN PROGRAM:
#

var int f;

print("Algorithm F1 (The factorial function)\n");
print(" Calculating fact($N) = ...\n");

f= fact(N);

print(" ... = $f\n");

------------------------- snip: fact.imc follows
-----------------------------------

###############################################################################
# This Parrot assembler file was produced by the Jako compiler. #
# Initial comments from the source code are reproduced below. #
###############################################################################

#
# fact.jako
#
# Some simple code to print some factorials
#
# Based on fact.pasm originally be Leon Brocard <ac...@astray.com>
2001-09-14.
#
# Copyright (C) 2001 Gregor N. Purdy. All rights reserved.
# This program is free software. It is subject to the same
# license as Perl itself.
#
# $Id: fact.jako,v 1.5 2001/10/19 12:36:16 gregor Exp $
#

#.sub __main
# call _main
# end
#.end
# const int N = 15;
..sub __CODE__0
fact_BEFORE: goto fact_AFTER # sub int fact (int
n) {
..end
..sub _fact
fact_ENTER: saveall
.param int n
.local int i
.local int f

i = 0
f = 1

_W2_WHILE:
_W2_NEXT: gt i, n, _W2_LAST # _W2: while (i <=
n) {
_W2_REDO: inc i # i++;
mul f, f, i # f = f * i;
_W2_CONT: branch _W2_NEXT # }
_W2_LAST:

.return f # return f
branch fact_LEAVE
fact_LEAVE: # }

restoreall
ret
fact_AFTER: goto __CODE__1
..end

..sub __CODE__1
# var int f;
print "Algorithm F1 (The factorial function)\n" #
print(...);
print " Calculating fact(" # print(...);
print 15
print ") = ...\n"

.arg 15
call _fact
.result I1

print " ... = " # print(...);
print I1
print "\n"
end
..end


------------------------- snip: fact.pasm follows
-----------------------------------


__CODE__0:
fact_BEFORE:
branch fact_AFTER
ret
_fact:
fact_ENTER:
saveall
restore I2
set I0, 0
set I1, 1
_W2_WHILE:
_W2_NEXT:
gt I0, I2, _W2_LAST
_W2_REDO:
inc I0
mul I1, I1, I0
_W2_CONT:
branch _W2_NEXT
_W2_LAST:
save I1
branch fact_LEAVE
fact_LEAVE:
restoreall
ret
fact_AFTER:
branch __CODE__1
ret
__CODE__1:
print "Algorithm F1 (The factorial function)\n"
print " Calculating fact("
print 15
print ") = ...\n"
save 15
bsr _fact
restore I1
print " ... = "
print I1
print "\n"
end
ret


Leopold Toetsch

unread,
Dec 4, 2002, 12:41:28 PM12/4/02
to gre...@focusresearch.com, perl6-i...@perl.org
gre...@focusresearch.com wrote:

> All --
>
> Before I go changing the Jako compiler, I'd like some feedback.


.sub dummy
goto main

_fact:
...
ret
main:
..
call _fact
...
.end

"goto"s to other subs are not subject of address fixup (though this
could be changed).

leo

0 new messages