Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
118 commits
Select commit Hold shift + click to select a range
d3a574a
Lexer prototype
andrey-komarov Feb 27, 2015
679a07a
Parser prototype (not works)
andrey-komarov Feb 27, 2015
97695c5
Parser is able to parse simple program
andrey-komarov Feb 27, 2015
52bc69a
Some attempt for types AST
andrey-komarov Mar 15, 2015
25162a3
ARM AST started
andrey-komarov Mar 18, 2015
f3850b0
gitignore
andrey-komarov Mar 18, 2015
ef774e8
Typechecking started
andrey-komarov Mar 21, 2015
14d5fab
some more typecheck
andrey-komarov Mar 21, 2015
3bcfe48
Slight AST change
andrey-komarov Mar 22, 2015
02a6aa4
Typechecker rewriting started
andrey-komarov Mar 22, 2015
f608c54
Typecheck of expression finished
andrey-komarov Mar 22, 2015
45e3d99
Started typecheck of Statement
andrey-komarov Mar 22, 2015
b35a577
Migrate from Reader to State
andrey-komarov Mar 22, 2015
16ebc3d
Typecheck of Statement done
andrey-komarov Mar 22, 2015
7e5a195
Finished typechecking
andrey-komarov Mar 22, 2015
53628e3
Typechecking looks correct
andrey-komarov Mar 22, 2015
bf03332
Added number of arguments check
andrey-komarov Mar 22, 2015
77046ce
Fix example
andrey-komarov Mar 22, 2015
455ec33
Rename examples
andrey-komarov Mar 22, 2015
cd7c0f2
Compiler started
andrey-komarov Mar 22, 2015
b1a53d7
Compilable instances for Program and TopLevel
andrey-komarov Mar 23, 2015
f12c19e
instance Compilable AST.Statement started
andrey-komarov Mar 23, 2015
d84eb64
Example added
andrey-komarov Mar 23, 2015
9aca74a
Some examples of ARM assembly
andrey-komarov Mar 23, 2015
6ce0ae8
Labels primitives
andrey-komarov Mar 24, 2015
8e5dd9b
Some work with labels
andrey-komarov Mar 24, 2015
10e68f6
Assembly started
andrey-komarov Mar 24, 2015
a5d0253
Some more assembly
andrey-komarov Mar 24, 2015
44d5672
Fuck ARM AST
andrey-komarov Mar 24, 2015
344a5a7
Compilable AST.Statement ~finished
andrey-komarov Mar 24, 2015
eab326f
I'm able to compile examples/example2.l !
andrey-komarov Mar 24, 2015
e497ff4
First compiled program!
andrey-komarov Mar 24, 2015
927f41c
TestCompiler not outputs trash
andrey-komarov Mar 24, 2015
51a1068
"if" works
andrey-komarov Mar 25, 2015
5345f44
Factorial successfully compiles
andrey-komarov Mar 25, 2015
3afe37b
"while" works
andrey-komarov Mar 25, 2015
86e2ea7
"isPrime.l" compiles successfully
andrey-komarov Mar 25, 2015
6112064
Fixed grouping by segment
andrey-komarov Mar 26, 2015
9f88e3c
Add missing clauses in Compilable Expr
Mar 26, 2015
b005ee9
Fix some warnings
Mar 26, 2015
2ab34ef
Fixed bug with global vars
Mar 26, 2015
ad4d67a
Register args order fixed
Mar 26, 2015
bc3c096
report stub added
Mar 26, 2015
05f2afb
Really fix order of arguments
andrey-komarov Mar 26, 2015
f439276
demo added
andrey-komarov Mar 26, 2015
969f111
Report finished
andrey-komarov Mar 26, 2015
0000000
Remove trash
andrey-komarov Mar 28, 2015
29c7558
Grammar changed
andrey-komarov Mar 28, 2015
0559200
Stuck with Lexer hack (`(a)*b`)
andrey-komarov Mar 28, 2015
4469f4e
Make lexer and parser monadic
andrey-komarov Mar 29, 2015
763978f
Lexer now tracks set of type to deal with `(a)*b`
andrey-komarov Mar 29, 2015
d66f45b
(probably unusable) typecheck stub
andrey-komarov Apr 11, 2015
3d028c5
Complete rewriting started
andrey-komarov Apr 21, 2015
d600949
Merge branch 'master' into v2
andrey-komarov Apr 21, 2015
766cd80
Merge commit '3d028c5' into v2
andrey-komarov Apr 21, 2015
f19066d
Uncomment interesting features
andrey-komarov Apr 21, 2015
a72e274
Started typechecking
andrey-komarov Apr 21, 2015
6445743
Slightly change AST
andrey-komarov Apr 21, 2015
98ddfe1
.gitignore
andrey-komarov Apr 21, 2015
c038830
cabal init
andrey-komarov Apr 21, 2015
d06cc95
cabal is able to build something
andrey-komarov Apr 21, 2015
7820e2a
rename project
andrey-komarov Apr 21, 2015
39897ad
rename examples
andrey-komarov Apr 22, 2015
61f326a
Tests are starting
andrey-komarov Apr 22, 2015
d6d5b0e
Test passed
andrey-komarov Apr 22, 2015
776ec61
some parser tests
andrey-komarov Apr 22, 2015
93a8f6b
PrettyPrinter started
andrey-komarov Apr 22, 2015
bf0eb56
QuickCheck tests are able to start
andrey-komarov Apr 22, 2015
6f00173
prettyprinter
andrey-komarov Apr 27, 2015
811f30d
prettyprinter fixes
andrey-komarov Apr 27, 2015
86ab7d7
Stronger prettyprinter property
andrey-komarov Apr 27, 2015
3a4347b
One more attempt started
andrey-komarov May 13, 2015
547d97a
new parser works
May 13, 2015
6a64c5a
typechecking started
May 13, 2015
72059fc
move Native out from Expr
May 13, 2015
1a03e84
typechecking started
May 13, 2015
b1defc4
typecheck almost finished
May 13, 2015
a61ad63
idea how to finish typechecker for Expr
May 13, 2015
cbbdb23
need to think
andrey-komarov May 14, 2015
1fe6183
Expr's typecheck finished
May 14, 2015
0326a35
typecheck
May 16, 2015
196d79d
typecheck finished
andrey-komarov May 17, 2015
7cde924
run typecheck
andrey-komarov May 17, 2015
6a40fea
codegen started
andrey-komarov May 17, 2015
c357694
codegen for expr finished
andrey-komarov May 18, 2015
6d9c8fa
codegen finished
andrey-komarov May 18, 2015
6a25cfb
fixes
andrey-komarov May 18, 2015
3bf2fef
remove trash
andrey-komarov May 18, 2015
c93d5b3
even more trash
andrey-komarov May 18, 2015
2a39018
cabal fixed
andrey-komarov May 19, 2015
5284f59
improve stdlib
May 19, 2015
b472425
fix bugs
May 19, 2015
ede6678
fix codegen for LitInt
May 19, 2015
2d91cbc
fix order of arguments
May 19, 2015
3bcec08
add _builtin_less
May 19, 2015
e555b62
fix while
May 19, 2015
bff2b3c
stdlib
May 19, 2015
966fc11
new works
andrey-komarov May 19, 2015
fbd6d3d
fix mmap
andrey-komarov May 19, 2015
84f7449
sieve example
andrey-komarov May 19, 2015
70d84d1
shrink unreachable code
andrey-komarov May 19, 2015
ae21de9
fix read global var
andrey-komarov May 19, 2015
4aed328
Int -> Int32
andrey-komarov May 19, 2015
c5eebb6
optimization started
andrey-komarov May 20, 2015
d1794b3
optimize stuck
andrey-komarov May 20, 2015
e5f31a1
cat works!
May 21, 2015
0e6059a
cont started
May 21, 2015
1ecefda
it compiles
andrey-komarov May 21, 2015
6fcf7f5
optimisations disabled
andrey-komarov May 21, 2015
4d99d66
better optimize signature
andrey-komarov May 21, 2015
47c816c
trivial optimisations work
andrey-komarov May 21, 2015
f0ddfa5
it optimizes fact!
andrey-komarov May 21, 2015
63ddc5f
even more optimisations
andrey-komarov May 22, 2015
7e5210b
lalala
andrey-komarov May 22, 2015
26ea8fc
examples
andrey-komarov May 22, 2015
022010c
add example for global
andrey-komarov May 22, 2015
fecc8de
add README
andrey-komarov May 22, 2015
26dd041
vm
andrey-komarov May 22, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions komarov.andrey/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
arm
.cabal-sandbox
*flymake*
dist
14 changes: 14 additions & 0 deletions komarov.andrey/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

67 changes: 67 additions & 0 deletions komarov.andrey/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Что должно быть установлено в системе #

* qemu
* cabal

# Установка эмулятора #

В папке `vm` лежит файл `download.sh`, который скачает всё необходимое
для запуска виртуальной машины (всё взято отсюда:
https://people.debian.org/~aurel32/qemu/armel/).

Затем, `run.sh` запустит виртуальную машину (несколько минут). После
запуска, можно будет заходить на неё по ssh на 2222 порту. Пароли:
user/user, root/root.

На виртуальной машине выполнить:

``````
~# apt-get update && apt-get install -y gcc strace
``````

Убедиться, что `as` и `ld` установились.

# Установка компилятора #

``````
cabal install
``````

После этого должен появиться файл `~/.cabal/bin/fcc`. Это компилятор,
который принимает на вход программу, и выводит на стандартный вывод
ассемблер для ARM. Процесс компиляции и запуска:

`````
local $ fcc < examples/cat.fc > cat.S
local $ scp -P 2222 cat.S user@localhost:~
user@debian-armel $ as cat.S -o cat.o
user@debian-armel $ ld cat.o -o cat
user@debian-armel $ echo hello | ./cat
hello
`````

# Как запустить что-то, отличное от `cat` #

В остальных примерах не используется ввод-вывод, а для этого результат
возвращается из функции main как код возврата. Наглядно за этим можно,
например, наблюдать, запуская программу через strace и смотря на
аргумент, с которым вызван системный вызов `exit`

# Оптимизации #

Реализовано два вида оптимизаций:

* Удаление ненужного кода
Начинаем с точки входа, смотрим, какие функции и глобальные
переменные достижимы, остальные удаляем
* Пытаемся посчитать значения подвыражений
* Выделяем <<хорошие>> функции и пытаемся вычислять их во время компиляции
* Хорошие --- те, которые не используют <<неправильных>>
<<библиотечных>> вызовов, массивов (TBD) и глобальных переменных
* Удаляем недостижимый код и упраздняем очевидные `if`-ы и `while`-ы
* Вычислятор -решает проблему останова- делает 10000 итераций и
успокаивается, если вычислить не удалось

Примеры хорошо оптимизируемых программ: `examples/fact.fc`,
`examples/sum.fc`

2 changes: 2 additions & 0 deletions komarov.andrey/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
9 changes: 9 additions & 0 deletions komarov.andrey/asm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

hello: example.o
gcc -g hello.c example.o -o hello

example.o: example.S
as example.S -o example.o

clean:
rm -f *.o hello
52 changes: 52 additions & 0 deletions komarov.andrey/asm/example.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.data
x: .word 0

.text

xx: .word x

.global f
.global fact
.global ret5
.global ret6

f:
ldr r1, xx
ldr r0, [r1]
add r0, #1
str r0, [r1]
mov pc, lr

# calling conv.
ret5:
ret6:
ldr r0, [sp]
mov pc, lr

# fact(n)
fact:
push {fp, lr} @
mov fp, sp @ ENTER
sub sp, sp, #4 @

mov r2, #0
teq r0, r2
beq ret1

str r0, [fp, #-4]
sub r0, r0, #1
bl fact
ldr r1, [fp, #-4]
mul r0, r1, r0
b ret

ret1:
mov r0, #1
b ret

ret:
mov sp, fp @
pop {fp, lr} @ EXIT
mov pc, lr @


27 changes: 27 additions & 0 deletions komarov.andrey/asm/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>

int f();

int fact(int x);

int ret5(int a, int b, int c, int d, int e);

int ret6(int a, int b, int c, int d, int e, int f);

int add(int a, int b)
{
int c = a + b;
return c;
}

int main()
{
printf("hello\n");
int i;
for (i = 0; i < 10; i++)
printf("%d ", f());
printf("\n");
printf("%d\n", fact(5));
printf("%d\n", ret5(1, 2, 3, 4, 5));
printf("%d\n", ret6(1, 2, 3, 4, 5, 6));
}
13 changes: 13 additions & 0 deletions komarov.andrey/demo/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
all:
make -C ../src
../src/TestCompiler < callc.l > callc.S
../src/TestCompiler < isPrime.l > prime.S
../src/TestCompiler < fact.l > fact.S
../src/TestCompiler < global.l > global.S

compile:
gcc callc.S call.c -o call
gcc fact.S fact.c -o fact
gcc global.S global.c -o global
gcc prime.S prime.c -o prime

14 changes: 14 additions & 0 deletions komarov.andrey/demo/call.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>

int add(int a, int b)
{
return a + b;
}

int f();

int main()
{
printf("%d\n", f());
}

6 changes: 6 additions & 0 deletions komarov.andrey/demo/callc.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int add(int a, int b);

int f()
{
return add(40, 2);
}
9 changes: 9 additions & 0 deletions komarov.andrey/demo/fact.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>

int fact(int n);

int main()
{
printf("%d\n", fact(5));
return 0;
}
6 changes: 6 additions & 0 deletions komarov.andrey/demo/fact.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int fact(int n) {
if (n == 0)
return 1;
else
return n * fact(n - 1);
}
12 changes: 12 additions & 0 deletions komarov.andrey/demo/global.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>

int inc();

int main()
{
int i;
for (i = 0; i < 10; i++)
printf("%d ", inc());
printf("\n");
return 0;
}
8 changes: 8 additions & 0 deletions komarov.andrey/demo/global.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

int v;

int inc()
{
v = v + 1;
return v;
}
50 changes: 50 additions & 0 deletions komarov.andrey/demo/isPrime.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
int div(int a, int b);
int mod(int a, int b);

bool isPrime(int n)
{
int i;
i = 2;
while (i * i <= n)
{
if (mod(n, i) == 0)
return false;
else {}
i = i + 1;
}
return true;
}

int numPrimes(int from, int to)
{
int i;
i = from;
int ans;
ans = 0;
while (i < to)
{
if (isPrime(i))
ans = ans + 1;
else {}
i = i + 1;
}
return ans;
}

int mod(int a, int b)
{
return a - b * div(a, b);
}

int div(int a, int b)
{
int c;
c = 0;
while (a >= 0)
{
c = c + 1;
a = a - b;
}
return c - 1;
}

15 changes: 15 additions & 0 deletions komarov.andrey/demo/prime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>

int isPrime(int n);
int numPrimes(int from, int to);

int main()
{
int i;
for (i = 2; i < 20; i++)
{
printf("%d is %s\n", i, isPrime(i) ? "prime" : "not prime");
}
printf("%d primes between 100 and 5000\n", numPrimes(100, 5000));
}

9 changes: 9 additions & 0 deletions komarov.andrey/examples/cat-global.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
int c;
int main() {
c = getchar();
while (c != 0-1) {
putchar(c);
c = getchar();
}
return 0;
}
9 changes: 9 additions & 0 deletions komarov.andrey/examples/cat.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
int main() {
int c;
c = getchar();
while (c != 0-1) {
putchar(c);
c = getchar();
}
return 0;
}
1 change: 1 addition & 0 deletions komarov.andrey/examples/e.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int main() { return 10 + 20; }
11 changes: 11 additions & 0 deletions komarov.andrey/examples/e2.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int f() {
if (true) {
return 10;
} else {
return 20;
}
}

int main() {
return f();
}
9 changes: 9 additions & 0 deletions komarov.andrey/examples/e3.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
int fact(int n) {
if (n == 0) {
return 10;
} else {
return 20;
}
}

int main() { return fact(10); }
9 changes: 9 additions & 0 deletions komarov.andrey/examples/e4.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
int fact(int n) {
if (n == 0) {
return 10;
} else {
return fact(n - 3);
}
}

int main() { return fact(10); }
10 changes: 10 additions & 0 deletions komarov.andrey/examples/e5.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
int f() {
int a;
a = 0;
while (a < 10) {
a = a + 3;
}
return a;
}

int main() { return f(); }
9 changes: 9 additions & 0 deletions komarov.andrey/examples/e6.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
int f() {
int a;
a = 10;
return a;
}

int main() {
return f();
}
Loading