Introduction to FOAM (the intermediate language for Aldor)
Laurentiu DraganUWO/ORCCA Programming Languages Group
Overview
● What is FOAM● Short example ● FOAM instructions – Grammar● Calling Protocols● Built-in operations● Semantics of FOAM● Runtime support● Optimizations
What is FOAM
● First Order Abstract Machine (FOAM)● Intermediate languages
– LIR, MIR, HIR
● FOAM – HIR● Developed at IBM by Dr. Stephen Watt
FOAM Goals
● Platform independence, well-defined semantics– Optimizations done at FOAM level are platform
independent
● Efficient mapping to ANSI C and Common Lisp● Has a structure that allows easy manipulation
– FOAM-to-FOAM transformations to optimize the code at FOAM level.
Structure
● FOAM unit – compilation unit (source file)● Each unit:
– List of declarations– List of definitions
● Declarations– Special slots: globals, constants
● Definitions: programs, initialization
Simple Example
● simple2.as#include "aldor"#include "aldorio"
import from Integer;
1;
● simple2.fm● simple3.c
High-level overview of FOAM
FOAM.as .fm
Built-ins
CLISP
FORTRANFOAM
Domains(Run-time)
PCall
BCall
Import/export
FOAM Grammar
Unit
● [Unit <DFmt> <DDef>]
● <DEnv> ::= [DEnv <e0: Int> ... <en-1: Int>]
● <DFluid> ::= [DFluid <i0: Int> ... <fi-1: Int>]
Declarations
● <DFmt> ::= [DFmt <f0: DDecl> ... <fn-1: DDecl>]
● <DDecl> ::= [DDecl <u: Byte> <d0: Decl> ... <dn-1>: Decl>]
● <Decl> ::= [Decl <t: Type> <s: String> <p: Byte> <r0: Byte> <r1: Byte>]
● <GDecl> ::= [GDecl <t: Type> <s: String> <r0: Byte> <r1: Byte> <dir: Byte> <p: Byte>]
Definitions
● <DDef> ::= [DDef <v0: Def> ... <vn-1: Def>]● <Def> ::= [Def <r: Reference> <v: Value>]
Commands● <Cmd> ::= <Def> | <Expr> | one of
● [Seq <c0: Cmd> <c1: Cmd> ... <cn: Cmd>]● [Goto <l: Label>]● [If <e: Expr> <l: Label>]
● [Select <e: Expr> <l0: Label> ... <ln-1: Label>]
● [Return <e: Expr>]
Commands (Contd)
● [Set <r: Reference> <e: Expr>]● [Lose <r: Reference>]● [PushEnv <f: Int> <e: Reference>]● [PopEnv ]● [Protect <e: Expr> <p:Reference>]● [Throw <to: Expr> <e: Expr>]● [Halt ]
Expressions
● <Expr> ::= <Value> | one of● [BVal <n: Int>]● [Label <n: Int>]● [Cast <t: Type> <e: Expr>]● [ANew <t: Type> <e: Expr>]● [RNew <f: Int>]● [TRNew <f: Int>]● [RCopy <f: Int> <e: Expr>]
Expressions (Contd)
● [BCall <o: Int> <e0: Expr> ... <en-1: Expr>]
● [CCall <t: Type> <c: Expr> <e0: Expr> ... <en-1: Expr>]
● [OCall <t: Type> <f: Expr> <e: Reference> <e0: Expr> ... <en-1: Expr>]
● [PCall <p: Int> <t: Type> <f: Expr> <e: Reference> <e0: Expr> ... <en-1: Expr>]
● [MFmt <f: Int> <c: Expr>]
● [Values <e0: Expr> ... <en: Expr>]● [Catch <t: Name> <e: Expr>]
Values
● <Value> ::= <Reference> | <Data>
References
● <Reference> ::= one of● [AElt <t: Type> <n: Expr> <a: Expr>]● [RElt <f: Int> <r: Expr> <n: Int>]● [IRElt <f: Int> <r: Expr> <n: Int>]
● [TRElt <f0: Int> <f1: Int> <r: Expr> <i: Expr> <n: Int>]
● [EElt <l: Int> <n: Int> <f: Int> <e: Reference>]
● [Const <n: Int>]● [Glo <n: Int>]
References (Contd)
● [Fluid <n: Int>]● [Par <n: Int>]● [Loc <n: Int>]● [Lex <l: Int> <n: Int>]● [Env <l: Int>]● [EEnv <l: Int> <e: Reference>]● [CEnv <c: Expr>]● [CProg <c: Expr>]● [EInfo <e: Expr>]● [PRef <r: Int> <p: Expr>]
Data and Types● <Data> ::=
Nil | Char | Bool | Byte | HInt | SInt | HInt | SInt | BInt | SFlo | DFlo | Arr | Rec | Prog | Clos | Ptr
● <Type> ::= Nil | Char | Bool | Byte | HInt | SInt | BInt | SFlo | DFlo | Arr | Rec | Env | Prog | Clos | Ptr | Word | Arb | NOp
FOAM Types
● Primitive Types– Boolean: Bool– Numeric
● Floating Point: SFlo, DFlo● Integral: Char, Byte, SInt, HInt
● Reference Types– Ptr, Clos, Env, BInt– Arr, Rec, Prog, NOp
Programs
● [Prog <n: Int> <m: Int> <t: Type> <f: Int> <b: Int> <size: Int> <time: Int> <par: DDecl> <loc: DDecl> <lex: DEnv> <fluid: DFluid> <c0: Cmd> <c1: Cmd> ... <cn-1: Cmd>]
● t – the return type
● f – the format of the return type, if t is NOp
● b – contains different flags: generator, side-effects, optimization info
● size – the size of prog including the programs called
● time – estimated running time
● loc - local varibles
● Lex – an array of formats of lexical levels
●C0
... cn – commands performed in sequence
Protocols
● Describe the interface used for accessing objects
● FOAM_Proto_Foam – natural mapping for FOAM objects
● FOAM_Proto_Other – natural mapping for objects in the hosting system
● FOAM_Proto_Init – initializer of a unit
● FOAM_Proto_C – objects are coming from C
● FOAM_Proto_Lisp – objects are coming from Lisp
● FOAM_Proto_Fortran – objects are coming from FORTRAN
Built-in Operations
● Operations on Bool – contains only values true and false– And, Or evaluate both arguments– Examples: true, false, Not, And, Or, EQ, NE
● Operations on Char– can be mapped to native set (EBCDIC, ASCII)– Space, newline, isDigit, EQ, lower, ord, num, ...
Built-in Operations (Contd)
● Operations on SFlo, DFlo● Operations on Byte, HInt, SInt, BInt● Operations on Ptr● Text Operations: FormatXXX , ScanXXX● Conversion Operations
Semantics of FOAM Programs
● A FOAM program is a set of Units
● One unit is considered to be the starting unit
● The execution starts with the first program of the starting unit
● Only Globals are shared among units
● The order of evaluation of arguments of a call is undefined
● For PCall the order is defined according to the protocol used
Runtime Support
● Written in Aldor● Most of it offers support for domains and
categories● Support for debugging
Optimizations
● Aldor compiler uses aggressive optimization to obtain performace close to C code
● Optimizations like: dead variable elimination, procedure integration, constant folding, copy propagation, peephole optimizations, common subexpression elimination.
● As a result of these optimizations the resulted code can be 10s or 100s times faster than unoptimized code
Top Related