Oppolzer - Informatik / Stanford Pascal Compiler


Home       Lebenslauf       Schwerpunkte       Kenntnisse       Seminare       Kunden       Projekte       Produkte       Blog       Stanford Pascal       Kontakt

The Stanford Pascal Compiler / Evolution Steps

Back to Compiler main page

Pointer arithmetic - new functions ADDR, PTRADD, PTRDIFF, SIZEOF, PTR2INT

When I started to code the LE inspired storage allocation routines, I soon discovered that some more compiler features are needed to make this task more fun. In fact, I borrowed some features from the C language and added them to Stanford Pascal in a sensible way (I hope), so that the overall spirit of the language will not be compromised.

The features added in this sprint include

Here is an example program that shows the new features:


program TESTADDR ( OUTPUT ) ; (********) (*$A+ *) (********) type CHAR10 = array [ 1 .. 10 ] of CHAR ; SHOWPTR = record case BOOLEAN of TRUE : ( X1 : -> CHAR ) ; FALSE : ( X2 : INTEGER ) end ; var X : INTEGER ; Y : -> INTEGER ; FELD : array [ 1 .. 10 ] of CHAR ; CHP : -> CHAR ; CHP2 : -> CHAR ; Z : SHOWPTR ; begin (* HAUPTPROGRAMM *) X := 25 ; Y := ADDR ( X ) ; WRITELN ( 'vergleich: ' , X , Y -> ) ; FELD := 'OPPOLZER' ; CHP := ADDR ( FELD ) ; while CHP -> <> ' ' do begin WRITELN ( 'chp -> = ' , CHP -> ) ; CHP := PTRADD ( CHP , 1 ) ; end (* while *) ; CHP2 := ADDR ( FELD ) ; X := PTR2INT ( CHP ) ; WRITELN ( 'ptr2int (chp) = ' , X ) ; Z . X1 := CHP ; WRITELN ( 'chp via showptr = ' , Z . X2 ) ; X := PTR2INT ( CHP2 ) ; WRITELN ( 'ptr2int (chp2) = ' , X ) ; X := PTR2INT ( ADDR ( FELD ) ) ; WRITELN ( 'ptr2int (addr(feld)) = ' , X ) ; WRITELN ( 'ptr2int (addr(feld)) = ' , PTR2INT ( ADDR ( FELD ) ) ) ; X := PTRDIFF ( CHP , CHP2 ) ; WRITELN ( 'ptrdiff = ' , X ) ; X := PTRDIFF ( CHP , ADDR ( FELD ) ) ; WRITELN ( 'ptrdiff = ' , X ) ; X := PTRDIFF ( ADDR ( FELD ) , CHP ) ; WRITELN ( 'ptrdiff = ' , X ) ; WRITELN ( 'sizeof (feld) = ' , SIZEOF ( FELD ) ) ; WRITELN ( 'sizeof (integer) = ' , SIZEOF ( INTEGER ) ) ; WRITELN ( 'sizeof (char10) = ' , SIZEOF ( CHAR10 ) ) ; end (* HAUPTPROGRAMM *) .

Note: the type SHOWPTR shows how you could do pointer arithmetic before my extensions (it is indeed done this way in SNAPSHOT and other system related routines). I used it to test the results.

The A+ switch tells the P-Code translator to output ASSEMBLER code in addition to the binary 370 code to file ASMOUT, which proved to be very useful when doing these changes and implementing the new P-Code instructions.

When adding the new functions, I also improved the compiler (PASCAL1), so that future additions of new library functions are still easier (improved table layout of the internal compiler tables).

Back to Compiler main page