[comp.sys.handhelds] What If - someone wrote a HP-48SX C compiler?

daniel@rosevax.Rosemount.COM (The Amazing Daniel Winker) (03/13/90)

I've never taken that Scary Sounding undergrad course where you write
a compiler, so I don't know a lot about the mechanics of it.  But, it
seems to me that it should be possible to write a compiler to run on -
say my PC which would compile C source code into HP-48SX machine code.

Could I get some response?  What are the hurdles to overcome in doing
this?  Does this sound reasonable?  Would HP help out with the Machine
Language codes?  Approximately how long would it take a compulsive
programmer working about 10 hours a week at night to learn about
compilers and write the code?  How about writing the compiler in C in
such a way that it could be compiled into HP-48SX machine code and run
on the calculator?

Just some thoughts.

rustw@yvax.byu.edu (03/14/90)

I have written a C to 28s machine code compiler that works well for very simple
routines.  However I have not had much time to improve upon it.  Most of the
programs compiled so far are much slower than using RPL and they have to be
entered by hand into the 28.  During the summer (when I get my 48) I will
possibly have more time to improve upon this project and get some very fast
results.

Wayne Rust


Relay-Version: Version 1.7 PSU-NETNEWS 5/20/88; site MAINE.BITNET
Posting-Version: Version 1.7 PSU-NETNEWS 5/20/88; site MAINE.BITNET
Path: cunyvm!maine.bitnet!steve
From: Steven A. Nickerson <STEVE@MAINE.BITNET>
Newsgroups: rec.games.frp
Subject: FRP games...
Message-ID: <STEVE.90072112013@MAINE.BITNET>
Date: Tuesday, 13 Mar 1990 11:20:13 EST

I'm sure this has been asked several times..but here it goes
What computer games do all of you think are the "best"?   I'm
looking for a good dungeons and dragons type game, but haven't
found a REAL good one yet.   I have an IBM PC and like Bard's
Tale the best so far.   Anyone have any games that they like in
particular????
Thanks for the suggestions all!!!
SteveN

lishka@uwslh.slh.wisc.edu (Chris Lishka (roommate of Lord Greystoke) ) (03/20/90)

daniel@rosevax.Rosemount.COM (The Amazing Daniel Winker) writes:

>I've never taken that Scary Sounding undergrad course where you write
>a compiler, so I don't know a lot about the mechanics of it.  

I took both a undergraduate and a graduate compiler/language course (I
was the only undergrad in the latter).  The courses are not that hard,
but you do have to work your butt off; the graduate course assignment
was to implement a subset (your choice) of Ada and test it against a
real verification suite!

>But, it
>seems to me that it should be possible to write a compiler to run on -
>say my PC which would compile C source code into HP-48SX machine code.

Yes, this is indeed possible.

>Could I get some response?  What are the hurdles to overcome in doing
>this?  Does this sound reasonable?  

There are many hurdles.  First, I recommend that you do *not* reinvent
the wheel by starting from scratch.  Instead, look around and find a
good public-domain C compiler and modify it.  I believe that a few
of these exist for the Commodore Amiga, although I am not sure about
MS-DOS machines or Macs.

The next hurdle lies in implementing the various libraries that C
requires.  First and foremost is the stdio library (standard
input/output), which contains *very* necessary functions to read and
write to files and streams.  Second, you will need to implement the
strings library; this is more trivial, but you need to decide which
version you want to write (i.e. strings.h as in Berkeley Unix, or
string.h as in System V -- they *are* different).  Third, since you
are writing a compiler for the 48sx, you will want to write a
front-end library to the available math functions on the 48sx.  This
is likely to be difficult, because you will need to find out the entry
addresses for the 48sx math functions as well as match the specific
input parameters/types for each function.  You could use the hp48
Stack to pass parameters, but this might compromise speed.

The final hurdle (that I can think of) is incorporating C "notions"
into the 48sx.  For instance, C works with idea of "streams" and
"files."  However, the 48sx uses objects.  So, what will you use for
files on the 48sx?  Strings are a possibility, but there are
limitations.  Another C notion is the use of an argument vector (argc
and argv) for setting command-line options.  How is this to be used on
the 48sx, where you won't have a command-line, but rather a stack
environment?  These issues need to be well thought out before you
start, because they will affect the end result heavily.

>Would HP help out with the Machine
>Language codes?  

I have no idea.

>Approximately how long would it take a compulsive
>programmer working about 10 hours a week at night to learn about
>compilers and write the code?  

Let me put it this way: compilers written *from*scratch* typically
take experts several *man-years* to implement correctly, and that is
at 40 (or more!) hours per week.  An estimate of two man-years to
write a C compiler from scratch at 40 hours per week for a well-versed
language-type means that it would take you 8 years to complete at ten
hours per week.  There will likely be a new HP "high-end" calculator
(not to mention a new CPU) before then! ;-)

This is why you definitely do not want to do it from scratch.  Nor do
I recommend tackling this alone.  Find an existing, simple,
well-written, public domain C compiler and modify it.  Work in a small
team.  It will save you a lot of time.  Another useful tool would be a
real C verification suite to test how well your compiler matches the
"standard" C (be it ANSI standard or just K&R). 

>How about writing the compiler in C in
>such a way that it could be compiled into HP-48SX machine code and run
>on the calculator?

This doesn't make much sense.  If you write the C compiler to run on
the 48sx, then you would either (a) need to edit C source code on the
48sx using the built in 22 characters/line screen (a dismal thought)
or (b) need to edit the C source on a host computer and download it to
the 48sx.  If you choose (a), it will be miserable trying to write
programs.  If you choose (b), you may as well use the compiler on the
host computer and download the 48sx *binaries*; this is known as
"cross-compilation" and is a technique that is widely used in the
industry (especially for new hardware).

I will give you a hint: write the C compiler in standard, portable C.
This gives you two benefits.  First, the C compiler can be ported to
any run on any host machine (MS-DOS, Amiga, Mac, Joe's home computer,
etc.) that has a C compiler.  Second, if you have a C compiler that
can compile itself, you can COMPILE THE C COMPILER to run on the 48sx,
which is what you wanted to do in the first place!  Portable code is
the way to go in this case.

>Just some thoughts.

I hope this gives you a good idea of what is involved.  If you want to
read some good books on compiler implemtation, I recommend the following:

* The (Green/Red) Dragon Books by Aho, Sethi, and Ullman.
I forget what the real title is, but you can easily recognize them by
the dragons on the front cover.  These are pretty much the definitive
texts on compiler design and implementation, and are fairly good
reading (although they are somewhat technical).

* _Crafting_a_Compiler_ by Charles Fischer (and someone else whose
name I forget...the book is at work).  This is a very good and
thorough book and is well written.  It is not as technical as the
above texts, and has the advantage of discussing important tools like
LEX and YACC.  In addition, it discusses the implementation of Ada to
illustrate problems.  Although you make not like Ada (I am not nuts
about it either), it is a good language to use for this purpose
because it contains nearly every compiled-language construct that one
can think of (except exotic languages, such as Lisp and Smalltalk).

I hope this helps you.  Writing a compiler of any sort is a major and
admirable task.  I think that if you successfully wrote or modified a
C compiler for the 48sx, you could easily find a job in the computer
industry doing this for the rest of your life!

If you have more questions, email to me.  I replied on the net because
I thought I saw more than one article on this topic.  Also, this is
what I came up with off the top of my head, so I may be off on some
points.

					.oO Chris Oo.
-- 
Christopher Lishka 608-262-4485  "Somebody said to me, `But the Beatles were
Wisconsin State Lab. of Hygiene  antimaterialistic.'  That's a huge myth.  John
   lishka@uwslh.slh.wisc.edu     and I literally used to sit down and say `Now,
   uunet!uwvax!uwslh!lishka      let's write a swimming pool'."--Paul McCartney

frankw@hpcvra.CV.HP.COM (Frank Wales) (03/21/90)

[Note: I'm not an HP employee, and don't speak for them, but I do
 have experience pertinent to this topic.]

In some article The Amazing Daniel Winker (daniel@rosevax.Rosemount.COM)
asks about writing an HP-48 C compiler.

Well, I don't want to sound to discouraging, but I think writing a
C compiler for the 48 would be an interesting project, in the sense
of the Chinese curse "may you live in interesting times."

There are certain fundamental difficulties:

	-- the 48 has no file system, so any implementation of
	   open(), close(), read(), write(), etc. would need heavy
	   rework, never mind stdio;
	-- no processes, so no fork(), exec(), etc.;
	-- signal(), setjmp(), and many other commonly used
	   system calls would need work to behave reasonably --
	   some might be impossible to make work;
	-- 99% of the 48's maths functionality has no parallel in C;
	-- fundamental types would be non-standard by most common
	   C implementations -- the 48 CPU has 64-bit registers
	   and 20-bit nibble-based memory addressing, for example, which
	   pushes the usual portability limits;
	-- the 48 operating system is written in a language (RPL) which is
	   like a sort-of polymorphic, sort-of object-oriented, stack-based
	   blend of Forth, LISP and other stuff, and has few things in common
	   with C, certainly at the User level (no pointers, no memory
	   allocation, no reasonable analogues to goto, break or continue).

The last of these is probably the biggest obstacle to a reasonable
implementation of C.  You can either beat the hell out of C to make it
fit the 48, or ignore the OS and take over the hardware.  The former
would involve so many compromises and changes to C that whatever came
out the other end would be seriously unlike any other C you ever saw.
The latter would be nigh on impossible without the internals
documentation, and I would doubt you could get your hands on that.  It
would also defeat much of the obvious object of writing C on the 48,
which is to access its maths capabilities while writing in a popular language.

It would be a genuinely interesting and educational exercise to attempt
to write something which could take C code (ignoring the standard
libraries and providing compatible alternatives which accessed the 48's
resources) and output something which implemented that program on the
48, either by producing RPL code or assembler code.  However, if your
final goal is to bring existing C code to the 48, or to turn the machine
into a C one instead of an RPL one, I feel that you would ultimately be 
frustrated by the obstacles which the design of the HP-48 presents.
--
Frank Wales, Guest of HP Corvallis, [frank@zen.co.uk||frankw@hpcvdq.cv.hp.com]
Zengrange Ltd., Greenfield Road., LEEDS, England, LS9 8DB.   (+1)-503-750-3086

bobp@cbnewsl.ATT.COM (robert.phillips) (03/21/90)

In article <21580037@hpcvra.CV.HP.COM> frankw@hpcvra.CV.HP.COM (Frank Wales) writes:
>There are certain fundamental difficulties:
>
>	-- the 48 has no file system, so any implementation of
>	   open(), close(), read(), write(), etc. would need heavy
>	   rework, never mind stdio;
>	-- no processes, so no fork(), exec(), etc.;
>	-- signal(), setjmp(), and many other commonly used
>	   system calls would need work to behave reasonably --
>	   some might be impossible to make work;
>	-- 99% of the 48's maths functionality has no parallel in C;
>	-- fundamental types would be non-standard by most common
>	   C implementations -- the 48 CPU has 64-bit registers
>	   and 20-bit nibble-based memory addressing, for example, which
>	   pushes the usual portability limits;
>	-- the 48 operating system is written in a language (RPL) which is
>	   like a sort-of polymorphic, sort-of object-oriented, stack-based
>	   blend of Forth, LISP and other stuff, and has few things in common
>	   with C, certainly at the User level (no pointers, no memory
>	   allocation, no reasonable analogues to goto, break or continue).
>
I think the C compiler idea is pretty bogus, but I think you may be missing
the boat about C.  Open(), close(), etc. are not part of the C language--
they are UNIX system calls that implementations of C on other operating 
systems find convenient to provide in some form.  A C language implementation
can work perfectly well without open(),close(),read(),write(),fork(),exec(),
signal(),setjmp(),etc.  The Amazing Dan wants C, not UNIX.  To be strictly
correct, C provides no math capabilities beyond the usual +,-,/,%,*, and 
bitwise operations.  There is a standard math library, but one can always
create a non-standard math library.  

I think your winning objections  may be paraphrased as, "The HP 48sx is 
not a computer (IN THE C LANGUAGE SENSE).  
>  However, if your
>final goal is to bring existing C code to the 48, or to turn the machine
>into a C one instead of an RPL one, I feel that you would ultimately be 
>frustrated by the obstacles which the design of the HP-48 presents.
>--
>Frank Wales, Guest of HP Corvallis, [frank@zen.co.uk||frankw@hpcvdq.cv.hp.com]
>Zengrange Ltd., Greenfield Road., LEEDS, England, LS9 8DB.   (+1)-503-750-3086

I agree.  I think one of the reasons Frank mentions all the UNIX system
calls is to apply the argument of portability.  Indeed, one of the chief
advantages of C is its portability, and this advantage would be sacrificed
by any reasonable implementation on the HP (non-portable custom math library
to call built-in functions, non-standard i/o, etc.).  It WOULD be an
exercise for anyone with too much time on his/her hands, but it is not as
if compilers have never before been ported to oddball architectures.  
HP usually does a pretty good job on their "language" design.  In all 
probability, the compiler you write would not approach the speed and 
efficiency of HP's language.  

Rob Phillips
AT&BL
190 River Rd.
Summit, NJ 07901
201-522-6359
attunix!bobp

frankw@hpcvra.CV.HP.COM (Frank Wales) (03/23/90)

In article <21580037@hpcvra.CV.HP.COM> I wrote:
>There are certain fundamental difficulties:
> 
>[pile of stuff relating to system calls, libraries and other things]

In article <4686@cbnewsl.ATT.COM> bobp@cbnewsl.ATT.COM (robert.phillips) writes:
}I think the C compiler idea is pretty bogus, but I think you may be missing
}the boat about C.  A C language implementation can work perfectly well
}without open(),close(),read(),write(),fork(),exec(), signal(),setjmp(),etc.

I know; I've used some -- I didn't think it appropriate to bring up
discussions about different types of C implementation beyond my comment:

  It would be a genuinely interesting and educational exercise to attempt
  to write something which could take C code (ignoring the standard
  libraries and providing compatible alternatives which accessed the 48's
  resources) and output something which implemented that program on the
  48, either by producing RPL code or assembler code. 

Essentially, you end up writing all your own libraries and system calls,
and have 48C as the language name, or whatever.  Building such a thing
would not be easy, but could be rewarding.

}I think one of the reasons Frank mentions all the UNIX system
}calls is to apply the argument of portability.  Indeed, one of the chief
}advantages of C is its portability, and this advantage would be sacrificed
}by any reasonable implementation on the HP (non-portable custom math library
}to call built-in functions, non-standard i/o, etc.).

Exactly.  And to be pragmatic about it, the generated code would need to
coexist with the RPL operating system, which limits the kinds of
routines that would reasonably be written to special-purpose,
the-only-alternative-is-assembly routines.  This is still
a significant use, but there is a fairly limited number of people
who would benefit from such a compiler being available, because
for most programming uses the 48 is anticipated for, RPL is good
enough.  (And the 48 is first-and-foremost a calculator, not a
personal computer.)

But don't let us put you off.  ;-)
--
Frank Wales, Guest of HP Corvallis, [frank@zen.co.uk||frankw@hpcvdq.cv.hp.com]
Zengrange Ltd., Greenfield Road., LEEDS, England, LS9 8DB.   (+1)-503-750-3086