310 likes | 331 Views
Cross-Platform Low-Level Language. CPL 3 - Language Overview Brian Westphal. Design Overview. Designed to be like assembly, except with structure Designed to be compatible as an intermediate language between many types of high level languages and Assembly
E N D
Cross-Platform Low-Level Language CPL3 - Language Overview Brian Westphal
Design Overview • Designed to be like assembly, except with structure • Designed to be compatible as an intermediate language between many types of high level languages and Assembly • Designed to be cross-platform compatible
Design Overview • Supports: • Nested functions • Variable scoping - global, local (per function level) • Very strict typing • One-dimensional arrays • Function pointers • All whitespace can be removed
Extended Design Overview • Supports • Dynamic memory allocation/deallocation • No deallocation is available with SPIM • I/O with stdin and stdout
Grammar Overview • Given in EBNF format, compatible with my parser generator (available at http://www.fokno.org/parser/) • Written in Java
Grammar Overview • In the grammar file, instead of Rule : ‘a’ B ; B : ‘b’ B | ; • There would be Rule ::= ‘a’ B* B ::= ‘b’
Grammar Overview • Regular expressions are in single-quotes • There is no need for a separate scanner • Rules are not in single-quotes • Rules can be followed by modifiers: *, +, ? • Other EBNF options are available but not used in this grammar (there are no epsilons or binary subtractions)
Grammar Overview • Code can only exist in blocks immediately following a rule: //Supports types for casting and declarations. Type ::= '/(' (IntType | FloatType | CharType | AddressType) '/)' { RETree syntaxTree = (RETree) args; RETree type = (RETree) syntaxTree.get (1); return type.collapse (); }
Grammar Overview • The first two sections (sections are denoted by <%>) are for import statements, and additional functions respectively. The third section is for productions. • Other than those things, if you cannot read part of the grammar file, ask me - westphal@cs.unr.edu (and give me 12 to 24 hours to reply) • If you have suggestions for making CPL3 better, (i.e. more complete) please send me email as well.
Language Overview -by example: variables vars:; .func_main { return:(i); params:; vars:(i32)$x,(i32)$y; MOV $x (i32)100; MOV $y (i32)0; ADD $y $y $x; RET (i)0; }
Language Overview -by example: functions vars:; #This is a comment .func_main { return:(i); params:; vars:(i32)$fact; func_types:((i32):i32); MOV $1 (i32)5; CALL .func_factorial; MOV $fact $return; }
Language Overview -by example: functions .func_factorial { return:(i32); params:(i32)$value; vars:(i32)$t1; if $value == (i32)0 { RET (i32)1; }
Language Overview -by example: functions else { SUB $t1 $value (i32)1; func_types:((i32):i32); MOV $1 $t1; CALL .func_factorial; MOV $t1 $return; MULT $t1 $t1 $value; RET $t1; } }
Language Overview -by example: dynamic memory vars:; .func_main { return:(i); params:; vars:(f32*)$floatArray,(i32)$index; ALLOC $floatArray (i32)100 sizeof(f32); MOV $index (i32)0;
Language Overview -by example: dynamic memory wloop $index < (i32)100 { MOV $floatArray[$index] (f32)0.0; ADD $index $index (i32)1; } DEALLOC $floatArray; RET (i)0; }
Language Overview -by example: nested functions vars:; .func_main { return:(i); params:; vars:(i)$x; .localfunc_increment { return:void; params:(i)$y; vars:;
Language Overview -by example: nested functions ADD $x $x $y; } func_types:((i):i); MOV $1 (i)10; CALL .localfunc_increment; RET (i)0; }
Language Overview -by example: function pointers vars:; .func_squareNum { return:(i); params:(i)$x; vars:(i)$y; MULT $y $x $x; RET $y; }
Language Overview -by example: function pointers .func_main { return:(i); params:; vars:(i)$x,(void*)$funcPointer; MOV $x (i)10; MOV $funcPointer .func_squareNum;
Language Overview -by example: function pointers func_types:((i):i); MOV $1 $x; CALL $funcPointer; MOV $x $return; RET (i)0; }
//Signed int types. typedef int i; typedef char i8; typedef short i16; typedef int i32; typedef long long i64; typedef i si; typedef i8 si8; typedef i16 si16; typedef i32 si32; typedef i64 si64; //Unsigned int types. typedef unsigned int ui; typedef unsigned char ui8; typedef unsigned short ui16; typedef unsigned int ui32; typedef unsigned long long ui64; //Signed float types. typedef float f; typedef float f32; typedef double f64; typedef f sf; typedef f32 sf32; typedef f64 sf64; //Unsigned float types (no unsigned float in PowerPC or Intel). typedef float uf; typedef float uf32; typedef double uf64; //Char types. typedef char c; typedef char c8; typedef short c16; Language Overview - types
Language Overview -variable and type examples (i32)$x (i32)10 (i32)-10E6 (f32)$pi (f32)3.1415927 (f32)1.0E-6 (c)$letter (c)’a’ (c16)0020 (i*)$pointer (c8*)a0000:B800 (c8)$string[1024]
Language Overview - strings • Supported with the MOV statement only MOV $string “Hello World!\n\n”
Language Overview - conditions • Conditions do not use parentheses • No complex conditions are allowed (i.e. no &&) • The following operations are allowed: <, <=, ==, >=, >, !=
Math: ADD, SUB, MULT, DIV, REM, NEG Logical: EQ, GT, GE, LT, LE, NE, AND, NOT, NOR, OR, XOR Bitwise: SHL, SHR, BAND, BOR, BXOR Functions: RET, CALL Branching: JMP, JEZ Memory: ALLOC, DEALLOC Other: MOV, HALT Language Overview -built-in functions
Language Extensions - I/O • The following I/O functions are available via the blackbox programs PrintInt ReadInt PrintFloat ReadFloat PrintDouble ReadDouble PrintString ReadString
func_types:(void:i32); MOV $1 (i32)100; CALL .PrintInt; MOV $storage “Hello, World!\n\n” func_types:(void:c8*); MOV $1 $storage; CALL .PrintString; func_types:((i32):void); CALL .ReadInt; MOV $storage $return; func_types:(void:c8*,i32); MOV $1 $storage; MOV $1 (i32)1024; CALL .ReadString; Language Extensions -by example: I/O
Blackbox Programs • Use the blackbox programs (available at http://www.fokno.org/cpl3/) to convert your CPL3 code into an executable or into SPIM assembly. • Will only say - error or no errors, no specific error messages • Read the documentation; some things that the compiler allows for are not allowed in the language.
Language References • Please see the language reference available at http://www.fokno.org/cpl3/ for additional information and examples. This presentation and example files for this presentation will also be posted on that site. • Also, be sure to see the rest of the links from http://www.fokno.org/ for information on my Java parser and regular expression packages.
Comments and Questions • Please feel free to ask any additional questions and make any comments at this time