Skip to content

Commit

Permalink
Add const string literals (#517)
Browse files Browse the repository at this point in the history
* Add const string literals

* Add Aaron Kaiser to AUTHORS
  • Loading branch information
Rixxc authored Jul 12, 2023
1 parent 4fae756 commit f142e7b
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
The following people have contributed code and/or ideas to Jasmin:

Aaron Kaiser
Adrien Koutsos
Amber Sprenkels
Antoine Séré
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
arrays;
([PR #452](https://github.com/jasmin-lang/jasmin/pull/452)).

- Notation for string literals; there is no implicit zero terminator;
([PR #517](https://github.com/jasmin-lang/jasmin/pull/517)).

## Bug fixes

- Type-checking rejects wrongly casted primitive operators
Expand Down
7 changes: 6 additions & 1 deletion compiler/src/latex_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open Annotations
open Syntax

module F = Format
module L = Location

type ('a, 'b, 'c, 'd) str = ('a, 'b, 'c, 'd, 'd, 'a) CamlinternalFormatBasics.format6

Expand Down Expand Up @@ -345,6 +346,9 @@ let pp_fundef fmt { pdf_cc ; pdf_name ; pdf_args ; pdf_rty ; pdf_body ; pdf_anno
(pp_inbraces 0 pp_funbody) pdf_body;
F.fprintf fmt eol

let pp_string fmt s =
F.fprintf fmt "%S " (L.unloc s)

let pp_param fmt { ppa_ty ; ppa_name ; ppa_init } =
F.fprintf fmt "%a %a %a = %a;"
kw "param"
Expand All @@ -360,6 +364,7 @@ let pp_pgexpr fmt = function
openbrace ()
(pp_list ",@ " pp_expr) es
closebrace ()
| GEstring e -> pp_string fmt e

let pp_global fmt { pgd_type ; pgd_name ; pgd_val } =
F.fprintf fmt "%a %a = %a;"
Expand All @@ -379,7 +384,7 @@ let pp_pitem fmt pi =
Option.may (fun name ->
F.fprintf fmt "%a %s " kw "from" (L.unloc name)) in
F.fprintf fmt "%a%a " pp_from from kw "require";
List.iter (fun s -> F.fprintf fmt "%S " (L.unloc s)) s;
List.iter (pp_string fmt) s;
F.fprintf fmt eol

let pp_prog fmt =
Expand Down
1 change: 1 addition & 0 deletions compiler/src/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ pparam:
pgexpr:
| e=pexpr { GEword e }
| LBRACE es = rtuple1(pexpr) RBRACE { GEarray es }
| e=loc(STRING) { GEstring e }

pglobal:
| pgd_type=ptype pgd_name=ident EQ pgd_val=pgexpr SEMICOLON
Expand Down
6 changes: 6 additions & 0 deletions compiler/src/pretyping.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1892,11 +1892,17 @@ let tt_global_def pd env (gd:S.gpexpr) =
let f e =
let pe,ety = tt_expr ~mode:`OnlyParam pd env e in
(L.mk_loc e.pl_loc pe, ety) in
let array_of_string s =
L.unloc s |> String.to_list |> List.map @@ fun c ->
c |> Char.code |> Z.of_int |> fun z ->
P.(L.mk_loc (L.loc s) (Papp1 (E.Oword_of_int W.U8, Pconst z)), u8) in
match gd with
| S.GEword e ->
`Word (f e)
| S.GEarray es ->
`Array (List.map f es)
| S.GEstring e ->
`Array (array_of_string e)

let tt_global pd (env : 'asm Env.env) _loc (gd: S.pglobal) : 'asm Env.env =

Expand Down
1 change: 1 addition & 0 deletions compiler/src/syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ type pfundef = {
type gpexpr =
| GEword of pexpr
| GEarray of pexpr list
| GEstring of string L.located

type pglobal = { pgd_type: ptype; pgd_name: pident ; pgd_val: gpexpr }

Expand Down
1 change: 1 addition & 0 deletions compiler/tests/exec/dune
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
../success/x86-64/test_ptr_fn_inline.jazz
vpsxldq.jazz ../success/x86-64/vpsxldq.jazz
sem_unit.jazz
../success/x86-64/test_global_string_literal.jazz
)
(flags -rectypes)
(names
Expand Down
1 change: 1 addition & 0 deletions compiler/tests/exec/exec.expected
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ etest() = [0X12; 0X90; 0X78; 0X56; 0X34; 0X12; 0X90; 0X78; 0X56; 0X34; 0X12;
0X78; 0X56; 0X34; 0X12; 0X90; 0X78; 0X56; 0X34; 0X12; 0X90; 0X78;
0X0; 0X0; 0X0; 0X0; 0X78; 0X56; 0X34; 0X12; 0X90; 0X78; 0X56; 0X34;
0X12; 0X90; 0X78; 0X56; 0X0; 0X0; 0X0; 0X0]
main() = 0X0
4 changes: 4 additions & 0 deletions compiler/tests/exec/exec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ let () =
let () =
let prog = load_file "vpsxldq.jazz" in
exec prog [ (Z.of_int 0x480, Z.of_int 64) ] "etest" []

let () =
let prog = load_file "../success/x86-64/test_global_string_literal.jazz" in
exec prog [] "main" []
12 changes: 12 additions & 0 deletions compiler/tests/success/arm-m4/test_global_string_literal.jazz
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
u8[10] test = "Teststring";

export fn main() -> reg u32
{
reg u32 res tmp;
res = 0x54;

tmp = (32u)test[0];
res = res ^ tmp;

return res;
}
12 changes: 12 additions & 0 deletions compiler/tests/success/x86-64/test_global_string_literal.jazz
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
u8[10] test = "Teststring";

export fn main() -> reg u8
{
reg u8 res tmp;
res = 0x54;

tmp = test[0];
res = res ^ tmp;

return res;
}

0 comments on commit f142e7b

Please sign in to comment.