Skip to content

Commit 2d9cc88

Browse files
committed
Added EVM test js script; Added debugging print
1 parent d16a5da commit 2d9cc88

File tree

4 files changed

+243
-44
lines changed

4 files changed

+243
-44
lines changed

package-lock.json

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/example_contract/simpleStorage.sc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ constructor c (){
1414

1515
method set(x: int) {
1616
guard{
17-
x > 0 ;
17+
/-x > 0 ;-/
1818
}
1919
storage{
2020
storedData |-> x;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env node
2+
3+
// const net = require('net');
4+
// const solc = require('solc');
5+
const fs = require('fs');
6+
const ethers = require("ethers");
7+
8+
if (process.argv.length != 3) {
9+
console.log("invalid args");
10+
process.exit(1);
11+
}
12+
13+
const endpoint = "http://localhost:8545";
14+
const provider = new ethers.providers.JsonRpcProvider(endpoint);
15+
16+
// const abi = [
17+
// "constructor()",
18+
// "function initialize () public returns (uint)",
19+
// "function totalSupply() view returns (uint)",
20+
// "function balanceOf(uint) view returns (uint)",
21+
// "function transfer(uint, int) returns (bool)"
22+
// ];
23+
24+
const abi = [
25+
{"type":"function",
26+
"name":"set",
27+
"inputs":[{"name":"x", "type":"uint256"}],
28+
"outputs":[{"name":"", "type":"uint256"}],
29+
"payable":"true",
30+
"constant":false,
31+
"stateMutability":"view"}];
32+
33+
const bytecode = fs.readFileSync(process.argv[2]).toString().replace(/\n|\t|\r| /g, "");
34+
const signer = provider.getSigner(0);
35+
const creator = signer.getAddress();
36+
37+
async function deploy() {
38+
console.log("sending creation transaction...")
39+
let factory = new ethers.ContractFactory(abi, bytecode, signer);
40+
let contract = await factory.deploy();
41+
await contract.deployed();
42+
console.log("contract address: " + contract.address);
43+
console.log("transaction hash: " + contract.deployTransaction.hash);
44+
let deployedBytecode = await provider.getCode(contract.address);
45+
// console.log("deployed bytecode: " + deployedBytecode);
46+
47+
let setv = 240; // arbitrary uint
48+
console.log("calling set...");
49+
setval = await contract.set(setv);
50+
console.log("value set as: " + setval);
51+
}
52+
53+
deploy();

src/translateMinic.ml

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ let struct_name_to_ident2 = ident_generator "" "struct"
6666
let struct_field_name_to_ident2 = ident_generator "" "field"
6767
let backend_ident_of_globvar = ident_generator "var_" "var2"
6868
let backend_ident_of_funcname = ident_generator "ident_" "function"
69-
let backend_ident_of_tempvar = ident_generator "temp_" "var"
69+
(* let backend_ident_of_tempvar = ident_generator "temp_" "var" *)
70+
let backend_ident_of_tempvar = positive_of_int 11
7071

7172
let rec gen_ctype =
7273
let open Ctypes in
@@ -128,7 +129,8 @@ let rec gen_rexpr e =
128129
let open Language in
129130
match e with
130131
| (t, SId(Sglobal,l)) -> Evar (backend_ident_of_globvar l, gen_ctype t)
131-
| (t, SId(Slocal,l)) -> Etempvar (backend_ident_of_tempvar l, gen_ctype t)
132+
(* | (t, SId(Slocal,l)) -> Etempvar (backend_ident_of_tempvar l, gen_ctype t) *)
133+
| (t, SId(Slocal,l)) -> Etempvar (backend_ident_of_tempvar, gen_ctype t)
132134
| se -> raise (Failure ("Not implemented: " ^ string_of_sexpr se))
133135

134136
let rec gen_lexpr e =
@@ -142,7 +144,8 @@ let rec gen_lexpr e =
142144
|true -> Econst_int256 (Int256.one, Tint (I256, Unsigned))
143145
|false -> Econst_int256 (Int256.zero, Tint (I256, Unsigned)) )
144146
| (t, SId(Sglobal,l)) -> Evar (backend_ident_of_globvar l, gen_ctype t)
145-
| (t, SId(Slocal,l)) -> Etempvar (backend_ident_of_tempvar l, gen_ctype t)
147+
(* | (t, SId(Slocal,l)) -> Etempvar (backend_ident_of_tempvar l, gen_ctype t) *)
148+
| (t, SId(Slocal,l)) -> Etempvar (backend_ident_of_tempvar, gen_ctype t)
146149
| (t1, SBinop ((t2, se1), op, (t3, se2))) -> Ebinop (gen_binop op, gen_lexpr (t2, se1), gen_lexpr (t3, se2), gen_ctype t1)
147150
| (t, SComparsion ((t1, se1), op, (t2, se2))) -> Ebinop (gen_binop op, gen_lexpr (t1, se1), gen_lexpr (t2, se2), gen_ctype t)
148151
| (t, SMapexpr((t1, se1), selist)) ->
@@ -182,7 +185,8 @@ let gen_params sparams =
182185
let open Datatypes in
183186
let open Globalenvs.Genv in
184187
let cvt = function
185-
| Var(Id str, typ) -> Some (Coq_pair(backend_ident_of_tempvar str, gen_ctype typ))
188+
(* | Var(Id str, typ) -> Some (Coq_pair(backend_ident_of_tempvar str, gen_ctype typ)) *)
189+
| Var(Id str, typ) -> Some (Coq_pair(backend_ident_of_tempvar, gen_ctype typ))
186190
| _ -> None
187191
in
188192
coqlist_of_list (filter_map cvt sparams)
@@ -247,13 +251,21 @@ let gen_methoddef m =
247251
{
248252
fn_return = ret_type m.sreturns;
249253
fn_params = gen_params m.sparams; (* (ident, coq_type) prod list; *)
250-
fn_temps = Coq_nil; (* coqlist_of_list (gen_tempenv ((dest,mt.aMethodReturnType.aTypeCtype) :: gen_cmd_locals m.aMethodBody dest))*)
251-
fn_body = (if has_return then
254+
(* fn_temps = Coq_nil; coqlist_of_list (gen_tempenv ((dest,mt.aMethodReturnType.aTypeCtype) :: gen_cmd_locals m.aMethodBody dest)) *)
255+
fn_temps = coqlist_of_list [Coq_pair(positive_of_int 10, gen_ctype (Void "void"))];
256+
fn_body = gen_storage_cmd m.sstorage_body
257+
(* (if has_return then
252258
Ssequence(Ssequence(gen_guard_cmd m.sguard_body, gen_storage_cmd m.sstorage_body), gen_return_cmd m.sreturns)
253259
else
254-
Ssequence(gen_guard_cmd m.sguard_body, gen_storage_cmd m.sstorage_body))
260+
Ssequence(gen_guard_cmd m.sguard_body, gen_storage_cmd m.sstorage_body)) *)
255261
}
256262

263+
(* { fn_return = Tvoid;
264+
fn_params = (11,TInt (SIZE,SIGNEDNESS))::nil;
265+
fn_temps = (10,Tvoid)::nil;
266+
fn_body = Sassign(Evar(550,TInt (SIZE,SIGNEDNESS)),Etempvar(11,TInt (SIZE,SIGNEDNESS)))
267+
} *)
268+
257269
(* let gen_methoddef objname m =
258270
let open Datatypes in
259271
let mt = m.aMethodType in
@@ -273,15 +285,74 @@ let gen_methoddef m =
273285
body)
274286
} *)
275287

288+
(* Print MiniC expressions/statements, for debugging purposes. *)
276289

277290

291+
let rec string_of_ctype =
292+
let open Ctypes in
293+
function
294+
| Tvoid -> "Tvoid"
295+
| Tint (_,_) -> "TInt (SIZE,SIGNEDNESS)"
296+
| Tpointer _ -> "Tpointer"
297+
| Tarray (t,z) -> ("Tarray ("^string_of_ctype t^","^string_of_int(int_of_z z)^")")
298+
| Thashmap (t1,t2) -> ("Thashmap("^string_of_ctype t1^","^string_of_ctype t2^")")
299+
| Tfunction (ts,t) -> "Tfunction (TYPES,TYPE)"
300+
| Tstruct (id,flds) -> "Tstring (IDENT, FIELDS)"
301+
| Tunion (id,flds) -> "Tunion (IDENT, FIELDS)"
302+
| Tcomp_ptr id -> "Tcomp_ptr ID"
303+
304+
let rec string_of_expr = function
305+
| Econst_int (z, t) -> ("Econst_int (" ^ string_of_int (int_of_z z) ^ ","^string_of_ctype t^")")
306+
| Econst_int256 (z, t) -> ("Econst_int (" ^ string_of_int (int_of_z z) ^ ","^string_of_ctype t^")")
307+
| Evar (id,t) -> ("Evar("^string_of_int (int_of_positive id)^","^string_of_ctype t^")")
308+
| Etempvar (id,t) -> ("Etempvar("^string_of_int (int_of_positive id)^","^string_of_ctype t^")")
309+
| Ederef (e,t) -> ("Ederef(" ^ string_of_expr e ^","^ string_of_ctype t ^")")
310+
| Eunop (op,e,t) -> ("Eunop(OP,"^string_of_expr e^","^string_of_ctype t ^")")
311+
| Ebinop (op,e1,e2,t) -> ("Ebinop(OP,"^string_of_expr e1^","^string_of_expr e2^","^string_of_ctype t ^")")
312+
| Efield (e, ident, t) ->("Efield("^string_of_expr e^","^string_of_int (int_of_positive ident)^","^string_of_ctype t^")")
313+
| Earrayderef (e1,e2,t) -> ("Earrayderef("^string_of_expr e1 ^","^string_of_expr e2^","^string_of_ctype t^")")
314+
| Ehashderef (e1,e2,t) -> ("Ehashderef("^string_of_expr e1 ^","^string_of_expr e2^","^string_of_ctype t^")")
315+
| Ecall0 (bt,t) -> "Ecall0(BUILTIN,TYPE)"
316+
| Ecall1 (bt,e,t) -> "Ecall0(BUILTIN,EXPR,TYPE)"
317+
318+
let rec string_of_statement = function
319+
| Sskip -> "Sskip"
320+
| Sassign (e1,e2) -> ("Sassign("^string_of_expr e1 ^","^ string_of_expr e2 ^")")
321+
| Sset (id,e) -> ("Sset("^string_of_int(int_of_positive id)^","^string_of_expr e^")")
322+
| Scall (None, lab, exprs) -> "Scall(None, LABEL, ARGS)"
323+
| Scall (Some id, lab, expr) -> "Scall(Some ID, LABEL, ARGS)"
324+
| Ssequence (s1,s2) -> ("Ssequence("^string_of_statement s1 ^","^ string_of_statement s2^")")
325+
| Sifthenelse (e,s1,s2) -> ("Sifthenelse("^string_of_expr e^","^string_of_statement s1^","^string_of_statement s2 ^")")
326+
| Sloop s -> "(Sloop "^string_of_statement s^")"
327+
| Sbreak -> "Sbreak"
328+
| Sreturn None -> "Sreturn None"
329+
| Sreturn (Some e) -> ("Sreturn Some("^string_of_expr e^")")
330+
| Stransfer (e1,e2) -> "Stransfer ("^string_of_expr e1 ^","^ string_of_expr e2 ^")"
331+
| Scallmethod (e1,ids,z,e,es) -> "Scallmethod TODO"
332+
| Slog e -> "Slog " ^ string_of_expr e
333+
| Srevert -> "Srevert"
334+
335+
let rec string_of_params =
336+
let open Datatypes in
337+
function
338+
| Coq_cons (Coq_pair(id, t) , params) -> "("^string_of_int (int_of_positive id) ^","^string_of_ctype t ^")::"^ string_of_params params
339+
| Coq_nil -> "nil"
340+
341+
let string_of_methoddef md =
342+
"{ fn_return = " ^ string_of_ctype md.fn_return ^ ";\n"
343+
^" fn_params = " ^ string_of_params md.fn_params ^";\n"
344+
^" fn_temps = " ^ string_of_params md.fn_temps ^";\n"
345+
^" fn_body = " ^ string_of_statement md.fn_body ^"\n"
346+
^"}"
347+
348+
278349
(** gen_object_methods :
279350
(Int.int, coq_fun) prod list **)
280351
let gen_object_methods gen_methodname gen_method o =
281352
let open Datatypes in
282353
coqlist_of_list
283354
(List.map
284-
(fun m -> Coq_pair (gen_methodname m, gen_method m))
355+
(fun m -> print_endline(string_of_methoddef (gen_method m));Coq_pair (gen_methodname m, gen_method m)) (* for debugging purpose *)
285356
o.smethods)
286357

287358
(** gen_object_fields :
@@ -340,38 +411,3 @@ type genv = {
340411
genv_constructor : coq_function option
341412
}
342413
**)
343-
344-
(*
345-
(* Print MiniC expressions/statements, for debugging purposes. *)
346-
let rec string_of_ctype =
347-
let open Ctypes in
348-
function
349-
| Tvoid -> "Tvoid"
350-
| Tint (_,_) -> "TInt (SIZE,SIGNEDNESS)"
351-
| Tpointer _ -> "Tpointer"
352-
| Tarray (t,z) -> ("Tarray ("^string_of_ctype t^","^string_of_int(int_of_z z)^")")
353-
| Thashmap (t1,t2) -> ("Thashmap("^string_of_ctype t1^","^string_of_ctype t2^")")
354-
| Tfunction (ts,t) -> "Tfunction (TYPES,TYPE)"
355-
| Tstruct (id,flds) -> "Tstring (IDENT, FIELDS)"
356-
| Tunion (id,flds) -> "Tunion (IDENT, FIELDS)"
357-
| Tcomp_ptr id -> "Tcomp_ptr ID"
358-
359-
let rec string_of_expr = function
360-
| Econst_int (z, t) -> ("Econst_int (" ^ string_of_int (int_of_z z) ^ ","^string_of_ctype t^")")
361-
| Econst_int256 (z, t) -> ("Econst_int (" ^ string_of_int (int_of_z z) ^ ","^string_of_ctype t^")")
362-
| Evar (id,t) -> ("Evar("^string_of_int (int_of_positive id)^","^string_of_ctype t^")")
363-
| Etempvar (id,t) -> ("Etempvar("^string_of_int (int_of_positive id)^","^string_of_ctype t^")")
364-
| Ederef (e,t) -> ("Ederef(" ^ string_of_expr e ^","^ string_of_ctype t ^")")
365-
| Eunop (op,e,t) -> ("Eunop(OP,"^string_of_expr e^","^string_of_ctype t ^")")
366-
| Ebinop (op,e1,e2,t) -> ("Ebinop(OP,"^string_of_expr e1^","^string_of_expr e2^","^string_of_ctype t ^")")
367-
| Efield (e, ident, t) ->("Efield("^string_of_expr e^","^string_of_int (int_of_positive ident)^","^string_of_ctype t^")")
368-
| Earrayderef (e1,e2,t) -> ("Earrayderef("^string_of_expr e1 ^","^string_of_expr e2^","^string_of_ctype t^")")
369-
| Ehashderef (e1,e2,t) -> ("Ehashderef("^string_of_expr e1 ^","^string_of_expr e2^","^string_of_ctype t^")")
370-
| Ecall0 (bt,t) -> "Ecall0(BUILTIN,TYPE)"
371-
| Ecall1 (bt,e,t) -> "Ecall0(BUILTIN,EXPR,TYPE)"
372-
373-
let rec string_of_params =
374-
let open Datatypes in
375-
function
376-
| Coq_cons (Coq_pair(id, t) , params) -> "("^string_of_int (int_of_positive id) ^","^string_of_ctype t ^")::"^ string_of_params params
377-
| Coq_nil -> "nil" *)

0 commit comments

Comments
 (0)