forked from valderman/ccwf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97830f7
commit e821a6c
Showing
28 changed files
with
2,185 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
-- programs ------------------------------------------------ | ||
|
||
entrypoints Prog ; | ||
|
||
Program. Prog ::= [TopDef] ; | ||
|
||
FnDef. TopDef ::= Type Ident "(" [Arg] ")" Blk ; | ||
|
||
separator nonempty TopDef "" ; | ||
|
||
Argument. Arg ::= Type Ident; | ||
|
||
separator Arg "," ; | ||
|
||
-- statements ---------------------------------------------- | ||
|
||
Block. Blk ::= "{" [Stmt] "}" ; | ||
|
||
separator Stmt "" ; | ||
|
||
Empty. Stmt ::= ";" ; | ||
|
||
BStmt. Stmt ::= Blk ; | ||
|
||
Decl. Stmt ::= Type [Item] ";" ; | ||
|
||
NoInit. Item ::= Ident ; | ||
|
||
Init. Item ::= Ident "=" Expr ; | ||
|
||
separator nonempty Item "," ; | ||
|
||
Ass. Stmt ::= Ident "=" Expr ";" ; | ||
|
||
Incr. Stmt ::= Ident "++" ";" ; | ||
|
||
Decr. Stmt ::= Ident "--" ";" ; | ||
|
||
Ret. Stmt ::= "return" Expr ";" ; | ||
|
||
VRet. Stmt ::= "return" ";" ; | ||
|
||
Cond. Stmt ::= "if" "(" Expr ")" Stmt ; | ||
|
||
CondElse. Stmt ::= "if" "(" Expr ")" Stmt "else" Stmt ; | ||
|
||
While. Stmt ::= "while" "(" Expr ")" Stmt ; | ||
|
||
SExp. Stmt ::= Expr ";" ; | ||
|
||
-- Types --------------------------------------------------- | ||
|
||
Int. Type ::= "int" ; | ||
|
||
Doub. Type ::= "double" ; | ||
|
||
Bool. Type ::= "boolean" ; | ||
|
||
Void. Type ::= "void" ; | ||
|
||
internal Fun. Type ::= Type "(" [Type] ")" ; | ||
|
||
separator Type "," ; | ||
|
||
-- Expressions --------------------------------------------- | ||
|
||
EVar. Expr6 ::= Ident ; | ||
|
||
ELitInt. Expr6 ::= Integer ; | ||
|
||
ELitDoub. Expr6 ::= Double; | ||
|
||
ELitTrue. Expr6 ::= "true" ; | ||
|
||
ELitFalse. Expr6 ::= "false" ; | ||
|
||
EApp. Expr6 ::= Ident "(" [Expr] ")" ; | ||
|
||
EString. Expr6 ::= String ; | ||
|
||
Neg. Expr5 ::= "-" Expr6 ; | ||
|
||
Not. Expr5 ::= "!" Expr6 ; | ||
|
||
EMul. Expr4 ::= Expr4 MulOp Expr5 ; | ||
|
||
EAdd. Expr3 ::= Expr3 AddOp Expr4 ; | ||
|
||
ERel. Expr2 ::= Expr2 RelOp Expr3 ; | ||
|
||
EAnd. Expr1 ::= Expr2 "&&" Expr1 ; | ||
|
||
EOr. Expr ::= Expr1 "||" Expr ; | ||
|
||
coercions Expr 6 ; | ||
|
||
separator Expr "," ; | ||
|
||
-- operators ----------------------------------------------- | ||
|
||
Plus. AddOp ::= "+" ; | ||
|
||
Minus. AddOp ::= "-" ; | ||
|
||
Times. MulOp ::= "*" ; | ||
|
||
Div. MulOp ::= "/" ; | ||
|
||
Mod. MulOp ::= "%" ; | ||
|
||
LTH. RelOp ::= "<" ; | ||
|
||
LE. RelOp ::= "<=" ; | ||
|
||
GTH. RelOp ::= ">" ; | ||
|
||
GE. RelOp ::= ">=" ; | ||
|
||
EQU. RelOp ::= "==" ; | ||
|
||
NE. RelOp ::= "!=" ; | ||
|
||
-- comments ------------------------------------------------ | ||
|
||
comment "#" ; | ||
|
||
comment "//" ; | ||
|
||
comment "/*" "*/" ; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
@evenstring = global [7 x i8] c"Even!\0A\00" | ||
@oddstring = global [6 x i8] c"Odd!\0A\00" | ||
|
||
declare void @printString(i8*) | ||
|
||
define i1 @even(i32 %n) { | ||
entry: | ||
%n.addr = alloca i32 | ||
store i32 %n, i32* %n.addr | ||
%z.addr = alloca i32 | ||
store i32 0, i32* %z.addr | ||
%0 = load i32* %z.addr | ||
%t = load i32* %n.addr | ||
%cmp = icmp eq i32 %0, %t | ||
br i1 %cmp, label %if.then, label %if.else | ||
|
||
if.then: | ||
ret i1 1 | ||
|
||
if.else: | ||
%1 = load i32* %n.addr | ||
%sub = sub nsw i32 %1, 1 | ||
%call = call i1 @odd(i32 %sub) | ||
ret i1 %call | ||
|
||
if.end: | ||
unreachable | ||
} | ||
|
||
define i1 @odd(i32 %n) { | ||
entry: | ||
%n.addr = alloca i32 | ||
store i32 %n, i32* %n.addr | ||
%z.addr = alloca i32 | ||
store i32 0, i32* %z.addr | ||
%0 = load i32* %z.addr | ||
%t = load i32* %n.addr | ||
%cmp = icmp eq i32 %0, %t | ||
br i1 %cmp, label %if.then, label %if.else | ||
|
||
if.then: | ||
ret i1 0 | ||
|
||
if.else: | ||
%1 = load i32* %n.addr | ||
%sub = sub nsw i32 %1, 1 | ||
%call = call i1 @even(i32 %sub) | ||
ret i1 %call | ||
|
||
if.end: | ||
unreachable | ||
} | ||
|
||
define i32 @main() { | ||
entry: | ||
%n = alloca i32 | ||
store i32 20, i32* %n | ||
%t = load i32* %n | ||
%b = call i1 @even(i32 %t) | ||
br i1 %b, label %if.then, label %if.else | ||
|
||
if.then: | ||
%strp = getelementptr [7 x i8]* @evenstring, i32 0, i32 0 | ||
call void @printString(i8* %strp) | ||
br label %if.end | ||
|
||
if.else: | ||
%strp1 = getelementptr [6 x i8]* @oddstring, i32 0, i32 0 | ||
call void @printString(i8* %strp1) | ||
br label %if.end | ||
|
||
if.end: | ||
ret i32 0 | ||
|
||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
@dnl = internal constant [4 x i8] c"%d\0A\00" | ||
@fnl = internal constant [6 x i8] c"%.1f\0A\00" | ||
@d = internal constant [3 x i8] c"%d\00" | ||
@lf = internal constant [4 x i8] c"%lf\00" | ||
|
||
declare i32 @printf(i8*, ...) | ||
declare i32 @scanf(i8*, ...) | ||
declare i32 @puts(i8*) | ||
|
||
define void @printInt(i32 %x) { | ||
entry: %t0 = getelementptr [4 x i8]* @dnl, i32 0, i32 0 | ||
call i32 (i8*, ...)* @printf(i8* %t0, i32 %x) | ||
ret void | ||
} | ||
|
||
define void @printDouble(double %x) { | ||
entry: %t0 = getelementptr [6 x i8]* @fnl, i32 0, i32 0 | ||
call i32 (i8*, ...)* @printf(i8* %t0, double %x) | ||
ret void | ||
} | ||
|
||
define void @printString(i8* %s) { | ||
entry: call i32 @puts(i8* %s) | ||
ret void | ||
} | ||
|
||
define i32 @readInt() { | ||
entry: %res = alloca i32 | ||
%t1 = getelementptr [3 x i8]* @d, i32 0, i32 0 | ||
call i32 (i8*, ...)* @scanf(i8* %t1, i32* %res) | ||
%t2 = load i32* %res | ||
ret i32 %t2 | ||
} | ||
|
||
define double @readDouble() { | ||
entry: %res = alloca double | ||
%t1 = getelementptr [4 x i8]* @lf, i32 0, i32 0 | ||
call i32 (i8*, ...)* @scanf(i8* %t1, double* %res) | ||
%t2 = load double* %res | ||
ret double %t2 | ||
} | ||
|
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.