cross-platform low-level language cpl 3 - language overview brian westphal

30
Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Upload: marcia-ellis

Post on 29-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Cross-Platform Low-Level LanguageCPL3 - Language Overview

Brian Westphal

Page 2: 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

• Designed to be cross-platform compatible

Page 3: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

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

Page 4: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Extended Design Overview

• Supports– Dynamic memory allocation/deallocation

• No deallocation is available with SPIM

– I/O with stdin and stdout

Page 5: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Grammar Overview

• Given in EBNF format, compatible with my parser generator (available at http://www.fokno.org/parser/)

• Written in Java

Page 6: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Grammar Overview

• In the grammar file, instead ofRule : ‘a’ B ;B : ‘b’ B | ;

• There would beRule ::= ‘a’ B*B ::= ‘b’

Page 7: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

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)

Page 8: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

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 ();}

Page 9: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

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 - [email protected] (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.

Page 10: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

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;}

Page 11: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

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;}

Page 12: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Language Overview -by example: functions

.func_factorial

{

return:(i32);

params:(i32)$value;

vars:(i32)$t1;

if $value == (i32)0

{

RET (i32)1;

}

Page 13: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

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; }}

Page 14: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

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;

Page 15: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

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;

}

Page 16: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Language Overview -by example: nested functions

vars:;

.func_main{ return:(i); params:; vars:(i)$x;

.localfunc_increment { return:void; params:(i)$y; vars:;

Page 17: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Language Overview -by example: nested functions

ADD $x $x $y;

}

func_types:((i):i);

MOV $1 (i)10;

CALL .localfunc_increment;

RET (i)0;

}

Page 18: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Language Overview -by example: function pointers

vars:;

.func_squareNum

{

return:(i);

params:(i)$x;

vars:(i)$y;

MULT $y $x $x;

RET $y;

}

Page 19: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Language Overview -by example: function pointers

.func_main

{

return:(i);

params:;

vars:(i)$x,(void*)$funcPointer;

MOV $x (i)10;

MOV $funcPointer .func_squareNum;

Page 20: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Language Overview -by example: function pointers

func_types:((i):i);

MOV $1 $x;

CALL $funcPointer;

MOV $x $return;

RET (i)0;

}

Page 21: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Language Overview - types//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;

Page 22: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

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]

Page 23: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Language Overview - strings

• Supported with the MOV statement onlyMOV $string “Hello World!\n\n”

Page 24: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Language Overview - conditions

• Conditions do not use parentheses– No complex conditions are allowed (i.e. no

&&)

• The following operations are allowed:<, <=, ==, >=, >, !=

Page 25: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Language Overview -built-in functions

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

Page 26: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Language Extensions - I/O

• The following I/O functions are available via the blackbox programs

PrintInt ReadInt

PrintFloat ReadFloat

PrintDouble ReadDouble

PrintString ReadString

Page 27: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Language Extensions -by example: I/O

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;

Page 28: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

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.

Page 29: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

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.

Page 30: Cross-Platform Low-Level Language CPL 3 - Language Overview Brian Westphal

Comments and Questions

• Please feel free to ask any additional questions and make any comments at this time