Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!uwm.edu!uwvax!uwslh!lishka From: lishka@uwslh.slh.wisc.edu (Chris Lishka (roommate of Lord Greystoke) ) Newsgroups: comp.sys.handhelds Subject: Re: What If - someone wrote a HP-48SX C compiler? Message-ID: <1990Mar19.172401.23226@uwslh.slh.wisc.edu> Date: 19 Mar 90 17:24:01 GMT References: <8697@rosevax.Rosemount.COM> Organization: Wisconsin State Laboratory of Hygiene Lines: 133 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