C Edward Garcia (ewg2115) Naveen Revanna (nr2443) Niket Kandya - - PowerPoint PPT Presentation

c
SMART_READER_LITE
LIVE PREVIEW

C Edward Garcia (ewg2115) Naveen Revanna (nr2443) Niket Kandya - - PowerPoint PPT Presentation

C Edward Garcia (ewg2115) Naveen Revanna (nr2443) Niket Kandya (nk2531) Sean Yeh (smy2112) Introduction ARM V6 Assembly Subset of C Supported Features Control/looping: Types: Functions: Operators: int if malloc + - * / else char


slide-1
SLIDE 1

C

Edward Garcia (ewg2115) Naveen Revanna (nr2443) Niket Kandya (nk2531) Sean Yeh (smy2112)

slide-2
SLIDE 2

Introduction

ARM V6 Assembly Subset of C

slide-3
SLIDE 3

Supported Features

Functions: malloc free printf scanf Types: int char void struct pointer (to anything, unlimited levels) array Control/looping: if else while for return Most of your favorite features from C... Operators: + - * / < <= == > >= && ||

slide-4
SLIDE 4

Unsupported Features

  • double, float types
  • floating point operations
  • short and long integers
  • Unsigned, signedintegers
  • break, continue
  • Enums
  • Sizeof()
  • Increment, decrement operators.
  • do-while and switch statements.
  • auto, register, volatile static and extern.
  • Multi-file compilation and linkage.
  • Preprocessing - no # directives.
  • Function pointers.
  • Function inlining.
  • Static and volatile function.
  • Variable function arguments - Ellipsis (...)
  • Typecasting
slide-5
SLIDE 5

Scoping

  • Global definition of structs
  • Variable Declarations at beginning of functions
  • Variable Scope Limited to function
  • Static Scoping
  • Struct Declarations at beginning of functions
  • Variable, struct and array assignment following

declarations

slide-6
SLIDE 6

Architecture

Scanner Program.cpi Parser AST SAST Bytecode Arm Assembly GCC Program.s Cpi Executable

slide-7
SLIDE 7

Parser / Ast

Structure Declaration

  • Structure name
  • Member Variables

Function Declarations

  • Statement List
  • Variable Declarations
  • Return types
  • Function args
  • Function name

Parser and Scanner Program Ast

slide-8
SLIDE 8

Creating the SAST

Structure Declaration

  • Structure name
  • Member Variables

Function Declarations

  • Function args
  • Variable Declarations
  • Return types
  • Statement List
  • Function name

SAST Local Index

  • variable name
  • variable types

Struct Index

  • struct name
  • member variable

name/type

Function Index

  • Function names
  • Args
  • Return Type

Each Function

slide-9
SLIDE 9

Creating the SAST

k.c < Statement Block + while (i < k.c)

=

a 1 b('4', 27) i Function Index Struct Index Local Index Function Ex()

slide-10
SLIDE 10

Creating the SAST

k.c < Statement Block + while (i < k.c)

=

a 1 b('4', 27) i Function Index Struct Index Local Index Function Ex() variable/function exist? variable/function duplicate?

slide-11
SLIDE 11

Creating the SAST

int < Statement Block + while (i < k.c)

=

int 1 int char Function Index Struct Index Local Index Function Ex() Assign types to leaves

slide-12
SLIDE 12

Creating the SAST

int int Statement Block int while (int)

int

int 1 int char Function Index Struct Index Local Index Function Ex() Assign types to rest of expressions

slide-13
SLIDE 13

Type Checking

  • While conditions
  • If conditions
  • Variable assignments
  • Function arguments
  • Binary Operations
  • Return type checking
  • Pointer Arithmetic
  • Array Index Checking
  • Pointer Assignments
  • Structs Dereferencing
slide-14
SLIDE 14

SP SP & FP int a; char b; char c; char d[2]; int e[2]; char *f; int g[a]; (a=2)

varname type

  • ffset

a [Int] 4 b [Char] 5 c [Char] 6 d

[Arr(2);Char]

8 e

[Arr(2);Int]

16 f

[Ptr;Char]

20 g

[Ptr;Int]

24 a

b c d e f g

SP FP SP

Variable Symbol Table

slide-15
SLIDE 15

int a; char b; char c; char d[2]; int e[2]; char *f;

varname type

  • ffset

a [Int] b [Char] 5 c [Char] d

[Arr(2);Char]

8 e

[Arr(2);Int]

16 f

[Ptr;Char]

20 g

[Ptr;Int]

24 a

b c d e f

Structure Symbol Table

slide-16
SLIDE 16

Bytecode Generation

Bytecode Generation Per Function Indexes AST and Type Information Bytecode List Per Function

  • Stack Offset Information

for variables

  • Label names
  • Values
  • Constants
slide-17
SLIDE 17

Bytecode

slide-18
SLIDE 18

The challenges

  • Array offset calculation

– arr[a+b+2]

  • Pointer arithmetic

– *(p+2) – *(2+a+p)

  • Structure member offsets

– s.a – s.a.c[3] – s->b

  • All reduce to (base + offset) bytecode
slide-19
SLIDE 19

arr[a+b+2]

–BinEval(t1,a,+,b) –BinEval(t2,t1,+,2) –BinEval(t3,t2,*,4) –BinEval(t4,Addr(arr),+,t3) –Pntr(t4)

slide-20
SLIDE 20

*(2+a+p)

  • BinRes(Int);

–BinEval(t1,2,+,a)

  • BinRes(Ptr;Int)

– BinEval(t2,t1,*,4) –BinEval(t3,p,+,t2)

  • BinRes(Int)

–Pntr(t3)

slide-21
SLIDE 21

Arm Assembly Generation

ARM Assembly Bytecode List Per Function

  • Stack Offset Information

for variables

  • Label names
  • Values

Assembly File

  • Variable Addresses
  • Register Allocations
  • Label address
  • Constants addresses
slide-22
SLIDE 22

Cpi -> Bytecode -> Arm

slide-23
SLIDE 23

Testing

161 Tests

  • 64 Type Checking Tests
  • 97 Feature Tests

Test Enviornment

  • SSH and Raspberry Pi Server
  • QEMU Emulation
slide-24
SLIDE 24

Example: Tic-Tac-Toe

  • 2 Player game
  • Features array passing and

printf/scanf

slide-25
SLIDE 25

Example: Linked List

  • Function passing of

structs and pointers

  • Memory allocation

with malloc/free

slide-26
SLIDE 26

Example: Brainfuck Interpreter

Compiling an interpreter?? Yes!

  • runbf.sh is used to pass the source

code of the bf program along with its length to the bf interpreter

  • bf reads the two command line

arguments through scanf

slide-27
SLIDE 27

Project Management

slide-28
SLIDE 28

Project Timeline

Scanner Parser ARM Binops Hello World Bytecode Generation Test Framework while if return SAST feature tests structs Tests Bug Fix Offset Calc Rework Pointers

slide-29
SLIDE 29

Contributions

  • Naveen Revanna - Architecture Czar, Bytecode

Generation

  • Eddy Garcia - Type Checking, Test Case

Generation, External functions

  • Sean Yeh - Test suite, Example programs, bug

fixes

  • Niket Kandya - Scanner/Parser, Scalar Types

and Functions, Design

slide-30
SLIDE 30

Lessons Learned

  • Naveen Revanna - Spend sufficient time in deciding a

scalable architecture at early stages. Don't trust your developer self. Document code sufficiently. A good test infrastructure can save you loads of time.

  • Eddy Garcia - Pattern matching should be a feature

available in all languages. Regression tests are wonderful.

  • Sean Yeh - Next time I will not write test suite script in
  • BASH. Nevertheless, the testing framework turned out

pretty well.

  • Niket Kandya - Time spent on good design is time saved.

Functional Programming is a clean approach. Compilers are fun.