Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cmm #286

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Cmm #286

Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ the narrower the range, the longer the code, and the forward matching will tell

## Supported Languages

103 languages supported
104 languages supported

([JavaScript](#in-JavaScript-HTML) / [TypeScript](#in-TypeScript-Deno) / [Zen](#in-Zen) / [C](#in-C) / [C++](#in-C-1) / [C#](#in-C-2) / [Swift](#in-Swift) / [Java](#in-Java) / [Python](#in-Python) / [Ruby](#in-Ruby) / [PHP](#in-PHP) / [Go](#in-Go) / [Kotlin](#in-Kotlin) / [Dart](#in-Dart) / [Rust](#in-Rust) / [Haskell](#in-haskell-ghc-84x-or-later) / [OpenVBS](#in-OpenVBS) / [VBScript](#in-VBScript) / [Visual Basic](#in-Visual-Basic) / [Scala](#in-Scala) / [R](#in-R) / [GAS](#in-GAS-Google-App-Script) / [Nim](#in-Nim) / [Lua](#in-Lua) / [Perl](#in-Perl) / [Elixir](#in-Elixir) / [Groovy](#in-Groovy) / [D](#in-D) / [Julia](#in-Julia) / [Racket](#in-Racket) / [OCaml](#in-OCaml) / [Erlang](#in-Erlang) / [Clojure](#in-Clojure) / [F#](#in-F) / [Haxe](#in-Haxe) / [Scheme](#in-Scheme-R6RS) / [Common Lisp](#in-Common-Lisp) / [Elm](#in-Elm) / [Hack](#in-Hack) / [PureScript](#in-PureScript) / [CoffeeScript](#in-CoffeeScript) / [Objective-C](#in-Objective-C) / [Frege](#in-Frege) / [Eiffel](#in-Eiffel) / [Ada](#in-Ada) / [Free Pascal](#in-Free-Pascal) / [Crystal](#in-Crystal) / [Forth](#in-Forth) / [Bash](#in-Bash) / [AWK](#in-AWK) / [Vim script](#in-Vim-script) / [Vim9 script](#in-Vim9-script) / [IchigoJam BASIC](#in-IchigoJam-BASIC) / [GnuCOBOL](#in-GnuCOBOL) / [MoonScript](#in-MoonScript) / [Octave](#in-Octave) / [Emacs Lisp](#in-Emacs-Lisp) / [Fortran 90](#in-Fortran-90) / [MariaDB SQL/PSM](#in-MariaDB-SQL) / [PL/pgSQL](#in-PLpgSQL) / [Tcl](#in-Tcl) / [V](#in-V) / [Pike](#in-Pike) / [Io](#in-Io) / [Wren](#in-Wren) / [GNU Smalltalk](#in-GNU-Smalltalk) / [JScript](#in-JScript) / [Pharo](#in-Pharo) / [Scratch](#in-Scratch) / [Standard ML](#in-Standard-ML) / [なでしこ](#in-なでしこ) / [Kuin](#in-Kuin) / [ClojureScript](#in-ClojureScript) / [HSP](#in-HSP) / [Reason](#in-Reason) / [THPL](#in-THPL) / [Janet](#in-Janet) / [Phel](#in-Phel) / [Raku](#in-Raku) / [文言](#in-文言) / [Vala](#in-Vala) / [SmileBASIC](#in-SmileBASIC) / [Small Basic](#in-Small-Basic) / [Flix](#in-Flix) / [PowerShell](#in-PowerShell) / [Koka](#in-Koka) / [Zig](#in-Zig) / [BanchaScript](#in-BanchaScript) / [AssemblyScript](#in-AssemblyScript) / [Clawn](#in-Clawn) / [FORTRAN 77](#in-FORTRAN-77) / [orelang](#in-orelang) / [Laze](#in-Laze) / [WebAssembly](#in-WebAssembly) / [Nelua](#in-Nelua) / [Roland](#in-Roland) / [ReScript](#in-ReScript) / [Effekt](#in-Effekt) / [Ceylon](#in-Ceylon) / [Mochi](#in-Mochi) / [Neko](#in-Neko) / [NekoML](#in-NekoML) / [LLVM IR](#in-LLVM-IR))
([JavaScript](#in-JavaScript-HTML) / [TypeScript](#in-TypeScript-Deno) / [Zen](#in-Zen) / [C](#in-C) / [C++](#in-C-1) / [C#](#in-C-2) / [Swift](#in-Swift) / [Java](#in-Java) / [Python](#in-Python) / [Ruby](#in-Ruby) / [PHP](#in-PHP) / [Go](#in-Go) / [Kotlin](#in-Kotlin) / [Dart](#in-Dart) / [Rust](#in-Rust) / [Haskell](#in-haskell-ghc-84x-or-later) / [OpenVBS](#in-OpenVBS) / [VBScript](#in-VBScript) / [Visual Basic](#in-Visual-Basic) / [Scala](#in-Scala) / [R](#in-R) / [GAS](#in-GAS-Google-App-Script) / [Nim](#in-Nim) / [Lua](#in-Lua) / [Perl](#in-Perl) / [Elixir](#in-Elixir) / [Groovy](#in-Groovy) / [D](#in-D) / [Julia](#in-Julia) / [Racket](#in-Racket) / [OCaml](#in-OCaml) / [Erlang](#in-Erlang) / [Clojure](#in-Clojure) / [F#](#in-F) / [Haxe](#in-Haxe) / [Scheme](#in-Scheme-R6RS) / [Common Lisp](#in-Common-Lisp) / [Elm](#in-Elm) / [Hack](#in-Hack) / [PureScript](#in-PureScript) / [CoffeeScript](#in-CoffeeScript) / [Objective-C](#in-Objective-C) / [Frege](#in-Frege) / [Eiffel](#in-Eiffel) / [Ada](#in-Ada) / [Free Pascal](#in-Free-Pascal) / [Crystal](#in-Crystal) / [Forth](#in-Forth) / [Bash](#in-Bash) / [AWK](#in-AWK) / [Vim script](#in-Vim-script) / [Vim9 script](#in-Vim9-script) / [IchigoJam BASIC](#in-IchigoJam-BASIC) / [GnuCOBOL](#in-GnuCOBOL) / [MoonScript](#in-MoonScript) / [Octave](#in-Octave) / [Emacs Lisp](#in-Emacs-Lisp) / [Fortran 90](#in-Fortran-90) / [MariaDB SQL/PSM](#in-MariaDB-SQL) / [PL/pgSQL](#in-PLpgSQL) / [Tcl](#in-Tcl) / [V](#in-V) / [Pike](#in-Pike) / [Io](#in-Io) / [Wren](#in-Wren) / [GNU Smalltalk](#in-GNU-Smalltalk) / [JScript](#in-JScript) / [Pharo](#in-Pharo) / [Scratch](#in-Scratch) / [Standard ML](#in-Standard-ML) / [なでしこ](#in-なでしこ) / [Kuin](#in-Kuin) / [ClojureScript](#in-ClojureScript) / [HSP](#in-HSP) / [Reason](#in-Reason) / [THPL](#in-THPL) / [Janet](#in-Janet) / [Phel](#in-Phel) / [Raku](#in-Raku) / [文言](#in-文言) / [Vala](#in-Vala) / [SmileBASIC](#in-SmileBASIC) / [Small Basic](#in-Small-Basic) / [Flix](#in-Flix) / [PowerShell](#in-PowerShell) / [Koka](#in-Koka) / [Zig](#in-Zig) / [BanchaScript](#in-BanchaScript) / [AssemblyScript](#in-AssemblyScript) / [Clawn](#in-Clawn) / [FORTRAN 77](#in-FORTRAN-77) / [orelang](#in-orelang) / [Laze](#in-Laze) / [WebAssembly](#in-WebAssembly) / [Nelua](#in-Nelua) / [Roland](#in-Roland) / [ReScript](#in-ReScript) / [Effekt](#in-Effekt) / [Ceylon](#in-Ceylon) / [Mochi](#in-Mochi) / [Neko](#in-Neko) / [NekoML](#in-NekoML) / [LLVM IR](#in-LLVM-IR) / [Cmm](#in-Cmm))

supported languages list / サポート言語一覧
https://github.com/taisukef/Geo3x3/blob/master/langlist.csv
Expand Down Expand Up @@ -2331,6 +2331,27 @@ $ clang geo3x3.ll simple_geo3x3.ll
$ ./a.out
```

### in Cmm
[Cmm](https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/rts/cmm)
[geo3x3.cmm](https://github.com/taisukef/Geo3x3/blob/master/geo3x3.cmm),
[simple_geo3x3.cmm](https://github.com/taisukef/Geo3x3/blob/master/simple_geo3x3.cmm)
```
call encode(35.65858, 139.745433, 14, buff, 32);
foreign "C" printf ("%s\n",buff);

(float64 lat, float64 lng, bits8 level, float64 unit) = call decode (code);
foreign "C" printf ("%lf %lf %d %lf\n", lat, lng, level, unit);
```
setup:
```bash
$ brew install ghc
```
to run:
```bash
$ ghc -dcmm-lint geo3x3.cmm simple_geo3x3.cmm -no-hs-main simple_geo3x3.cmm.c
$ ./a.out
```

## How to contribute
1. add a lang
2. add a lang section in [README.md](README.md) (ex. [Neko](#in-Neko))
Expand Down
102 changes: 102 additions & 0 deletions geo3x3.cmm
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/rts/cmm
// https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/compiler/cmm-type
// https://www.cs.tufts.edu/~nr/c--/extern/man2.pdf

section "data" { ac_0: bits8 48; } // '0'
section "data" { ac_Plus: bits8 43; } // '+'
section "data" { ac_Minus: bits8 45; } // '-'
section "data" { ac_E: bits8 69; } // 'E'
section "data" { ac_W: bits8 87; } // 'W'

encode (float64 lat, float64 lng, bits8 level, bits64 buff, bits64 blen) {

if (level < 1::bits8) { // level is too small
bits8[buff] = 0::bits8; // terminate string
return (1);
}

if (%zx64(level) >= blen) { // buffer size is not enough
bits8[buff] = 0::bits8; // terminate string
return (2);
}

if lng >= 0.0 {
bits8[buff] = bits8[ac_E];
} else {
bits8[buff] = bits8[ac_W];
lng = %fadd (lng,180.0);
}
lat = %fadd(lat,90.0);

float64 unit; unit = 180.0;
bits64 i; i = 1;
loop:
if i < %zx64(level) {
unit = %fquot(unit,3.0);
float64 x0; (x0) = foreign "C" floor(%fquot(lng,unit));
float64 y0; (y0) = foreign "C" floor(%fquot(lat,unit));
bits8 x; x = %lobits8(%f2i64(x0));
bits8 y; y = %lobits8(%f2i64(y0));
bits8 c; c = bits8[ac_0] + x + y * 3::bits8 + 1::bits8;
bits8[buff+i] = c;
lng = %fsub(lng,%fmul(%i2f64(%zx64(x)),unit));
lat = %fsub(lat,%fmul(%i2f64(%zx64(y)),unit));
i = i + 1;
goto loop;
}
bits8[buff+i] = 0; // terminate string

return (0);
}

decode (bits64 code) {
bits64 clen; (clen) = foreign "C" strlen (code);
if (clen == 0) {
return (0.0,0.0,0,0.0);
}

bits64 begin; begin = 0;
bits8 flg; flg = 0::bits8;
bits8 c; c = bits8[code];

if c == bits8[ac_Minus] || c == bits8[ac_W] {
flg = 1::bits8;
begin = 1;
} else {
if c == bits8[ac_Plus] || c == bits8[ac_E] {
begin = 1;
}
}

float64 unit; unit = 180.0;
float64 lat; lat = 0.0;
float64 lng; lng = 0.0;
bits8 level; level = 1::bits8;
bits64 i; i = begin;
loop:
if i < clen {
bits8 n; n = bits8[code+i] - bits8[ac_0];
if (n < 1::bits8 || 9::bits8 < n) {
return (0.0,0.0,0,0.0);
}
unit = %fquot(unit,3.0);
n = n - 1::bits8;
float64 lng0; lng0 = %i2f64(%zx64(n % 3::bits8));
float64 lat0; lat0 = %i2f64(%zx64(n / 3::bits8));
lng = %fadd(lng,%fmul(lng0,unit));
lat = %fadd(lat,%fmul(lat0,unit));
level = level + 1::bits8;
i = i + 1;
goto loop;
}

lat = %fadd(lat,%fquot(unit,2.0));
lng = %fadd(lng,%fquot(unit,2.0));
lat = %fsub(lat,90.0);
if flg == 1::bits8 {
lng = %fsub(lng,180.0);
}

return (lat,lng,level,unit);
}

1 change: 1 addition & 0 deletions langlist.csv
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ Mochi,mochi.js,blacket,type,none,compile,https://github.com/taisukef/Geo3x3/blob
Neko,neko,blacket,var,none,compile,https://github.com/taisukef/Geo3x3/blob/master/geo3x3.neko
NekoML,nml,blacket,var,none,compile,https://github.com/taisukef/Geo3x3/blob/master/Geo3x3.nml
LLVM IR,ll,blacket,none,none,compile,https://github.com/taisukef/Geo3x3/blob/master/geo3x3.ll
Cmm,cmm,blacket,type,none,compile,https://github.com/taisukef/Geo3x3/blob/master/geo3x3.cmm
24 changes: 24 additions & 0 deletions simple_geo3x3.cmm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ghc -dcmm-lint geo3x3.cmm simple_geo3x3.cmm -no-hs-main simple_geo3x3.cmm.c
// ./a.out

section "data" {
buff: bits8[32];
}

section "data" {
code: bits8[] "E9139659937288\0";
}

cmmmain () {
float64 flat; flat = 35.65858;
float64 flng; flng = 139.745433;
// float64 flat; flat = %fneg( 35.65858 ); // could be used instead of negative literal
// float64 flng; flng = %fneg(139.745433); // could be used instead of negative literal
call encode(flat, flng, 14, buff, 32);
foreign "C" printf ("%s\n",buff);

(float64 lat, float64 lng, bits8 level, float64 unit) = call decode (code);
foreign "C" printf ("%lf %lf %d %lf\n", lat, lng, level, unit);

foreign "C" exit(0);
}
5 changes: 5 additions & 0 deletions simple_geo3x3.cmm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void cmmmain ();

int main (int argc, char ** argv) {
cmmmain ();
}