Skip to content

Commit

Permalink
* renamed modules to follow ficus style (with underscores to separate…
Browse files Browse the repository at this point in the history
… words instead of "Camel case")

* converted the two last existing K-form transformations, loop fusion and moving loop invariants outside of loop
  • Loading branch information
vpisarev committed Mar 22, 2021
1 parent d077338 commit a9b7ae1
Show file tree
Hide file tree
Showing 19 changed files with 420 additions and 70 deletions.
File renamed without changes.
14 changes: 7 additions & 7 deletions compiler/AstTypeChecker.fx → compiler/Ast_typecheck.fx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
///////////////////////// The type checker //////////////////////

from Ast import *
import AstPP, Options
import Ast_pp, Options

import Map, Set

Expand Down Expand Up @@ -47,9 +47,9 @@ These are the tasks performed by type checker:
** etc.
*/

val pprint_typ_x = AstPP.pprint_typ_x
val pprint_exp_x = AstPP.pprint_exp_x
val pprint_pat_x = AstPP.pprint_pat_x
val pprint_typ_x = Ast_pp.pprint_typ_x
val pprint_exp_x = Ast_pp.pprint_exp_x
val pprint_pat_x = Ast_pp.pprint_pat_x

fun print_env(msg: string, env: env_t, loc: loc_t) {
println(f"{msg}. env at {loc} [")
Expand Down Expand Up @@ -105,7 +105,7 @@ type rec_data_t = (rec_elem_t list, bool)

fun maybe_unify(t1: typ_t, t2: typ_t, loc: loc_t, update_refs: bool): bool {
//val whole_ctx = "ctx:" + "\n\t".join(all_compile_err_ctx)
//print(f"\n<<<trying to unify types at {loc}; {whole_ctx}: "); AstPP.pprint_typ_x(t1, loc); print(" and "); AstPP.pprint_typ_x(t2, loc); println(); File.stdout.flush()
//print(f"\n<<<trying to unify types at {loc}; {whole_ctx}: "); Ast_pp.pprint_typ_x(t1, loc); print(" and "); Ast_pp.pprint_typ_x(t2, loc); println(); File.stdout.flush()
var undo_stack: (typ_t? ref, typ_t?) list = []
var rec_undo_stack: (rec_data_t ref, rec_data_t) list = []
/* checks if a reference to undefined type (an argument of TypVar (ref None))
Expand Down Expand Up @@ -825,7 +825,7 @@ fun match_ty_templ_args(actual_ty_args: typ_t list, templ_args: id_t list, env:
fun check_exp(e: exp_t, env: env_t, sc: scope_t list) {
val (etyp, eloc) as ctx = get_exp_ctx(e)
//print_env("", env, eloc)
//println(f"\n------------------------\nchecking expression at {eloc}: "); AstPP.pprint_exp_x(e); println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
//println(f"\n------------------------\nchecking expression at {eloc}: "); Ast_pp.pprint_exp_x(e); println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")

fun check_for_clause(p: pat_t, e: exp_t, env: env_t, idset: idset_t, sc: scope_t list) {
val e = check_exp(e, env, sc)
Expand Down Expand Up @@ -1875,7 +1875,7 @@ fun check_exp(e: exp_t, env: env_t, sc: scope_t list) {
| DefExn _ | DefTyp _ | DirImport(_, _) | DirImportFrom(_, _, _) | DirPragma(_, _) =>
throw compile_err(eloc, "internal err: should not get here; all the declarations and directives must be handled in check_eseq")
}
//println(f"\nresult of type '{typ2str(get_exp_typ(new_e))}': "); AstPP.pprint_exp_x(new_e); println("\n---------------------------\n")
//println(f"\nresult of type '{typ2str(get_exp_typ(new_e))}': "); Ast_pp.pprint_exp_x(new_e); println("\n---------------------------\n")
new_e
}

Expand Down
70 changes: 35 additions & 35 deletions compiler/Compiler.fx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
// (calls all other parts of the compiler in the proper order)

import Filename, Sys, Map
import Ast, AstPP, Lexer, Parser, Options
import AstTypeChecker
import KForm, KPP, KNormalize, KAnnotate, KMangle
import KRemoveUnused, KLiftSimple, KFlatten, KTailRec,
KConstFold, KLift, KFastIdx, KInline
import Ast, Ast_pp, Lexer, Parser, Options
import Ast_typecheck
import K_form, K_pp, K_normalize, K_annotate, K_mangle
import K_remove_unused, K_lift_simple, K_flatten, K_tailrec,
K_cfold_dealias, K_lift, K_fast_idx, K_inline, K_loop_inv, K_fuse_loops

exception CumulativeParseError

type id_t = Ast.id_t
type kmodule_t = KForm.kmodule_t
type kmodule_t = K_form.kmodule_t
val pr_verbose = Ast.pr_verbose

fun get_preamble(mfname: string): Lexer.token_t list {
Expand Down Expand Up @@ -122,15 +122,15 @@ fun toposort(graph: dep_graph_t): id_t list
fun typecheck_all(modules: id_t list): bool
{
Ast.all_compile_errs = []
for m <- modules {AstTypeChecker.check_mod(m)}
for m <- modules {Ast_typecheck.check_mod(m)}
Ast.all_compile_errs.empty()
}

fun k_normalize_all(modules: id_t list): (kmodule_t list, bool)
{
Ast.all_compile_errs = []
KForm.init_all_idks()
val kmods = KNormalize.normalize_all_modules(modules)
K_form.init_all_idks()
val kmods = K_normalize.normalize_all_modules(modules)
(kmods, Ast.all_compile_errs.empty())
}

Expand All @@ -141,49 +141,49 @@ fun k_optimize_all(kmods: kmodule_t list): (kmodule_t list, bool) {
val niters = Options.opt.optim_iters
var temp_kmods = kmods
prf("initial dead code elim")
temp_kmods = KRemoveUnused.remove_unused(temp_kmods, true)
temp_kmods = K_remove_unused.remove_unused(temp_kmods, true)
for i <- 1: niters+1 {
pr_verbose(f"Optimization pass #{i}:")
if i <= 2 {
prf("simple lifting")
temp_kmods = KLiftSimple.lift(temp_kmods)
temp_kmods = K_lift_simple.lift(temp_kmods)
prf("annotate types")
temp_kmods = KAnnotate.annotate_types(temp_kmods)
temp_kmods = K_annotate.annotate_types(temp_kmods)
}
prf("tailrec")
temp_kmods = KTailRec.tailrec2loops_all(temp_kmods)
//prf("loop inv")
//temp_kmods = KLoopInv.move_loop_invs_all(temp_kmods)
//prf("inline")
temp_kmods = K_tailrec.tailrec2loops_all(temp_kmods)
prf("loop inv")
temp_kmods = K_loop_inv.move_loop_invs_all(temp_kmods)
prf("inline")
if Options.opt.inline_thresh > 0 {
temp_kmods = KInline.inline_some(temp_kmods)
temp_kmods = K_inline.inline_some(temp_kmods)
}
prf("flatten")
temp_kmods = KFlatten.flatten_all(temp_kmods)
//prf("fuse loops")
//temp_kmods = KFuseLoops.fuse_loops_all(temp_kmods)
temp_kmods = K_flatten.flatten_all(temp_kmods)
prf("fuse loops")
temp_kmods = K_fuse_loops.fuse_loops_all(temp_kmods)
prf("fast idx")
temp_kmods = KFastIdx.optimize_idx_checks_all(temp_kmods)
temp_kmods = K_fast_idx.optimize_idx_checks_all(temp_kmods)
prf("const folding")
temp_kmods = KConstFold.cfold_dealias(temp_kmods)
temp_kmods = K_cfold_dealias.cfold_dealias(temp_kmods)
prf("dead code elim")
temp_kmods = KRemoveUnused.remove_unused(temp_kmods, false)
temp_kmods = K_remove_unused.remove_unused(temp_kmods, false)
}
pr_verbose("Finalizing K-form:")
prf("lambda lifting")
temp_kmods = KLift.lift_all(temp_kmods)
temp_kmods = K_lift.lift_all(temp_kmods)
prf("flatten")
temp_kmods = KFlatten.flatten_all(temp_kmods)
temp_kmods = K_flatten.flatten_all(temp_kmods)
prf("dead code elim")
temp_kmods = KRemoveUnused.remove_unused(temp_kmods, false)
temp_kmods = K_remove_unused.remove_unused(temp_kmods, false)
prf("mangle")
temp_kmods = KMangle.mangle_all(temp_kmods)
temp_kmods = K_mangle.mangle_all(temp_kmods)
prf("dead code elim")
temp_kmods = KRemoveUnused.remove_unused(temp_kmods, false)
//prf("mark recursive")
//temp_kmods = KInline.find_recursive_funcs_all(temp_kmods)
temp_kmods = K_remove_unused.remove_unused(temp_kmods, false)
prf("mark recursive")
temp_kmods = K_inline.find_recursive_funcs_all(temp_kmods)
prf("annotate types")
temp_kmods = KAnnotate.annotate_types(temp_kmods)
temp_kmods = K_annotate.annotate_types(temp_kmods)
(temp_kmods, compile_errs.empty())
}

Expand Down Expand Up @@ -332,7 +332,7 @@ fun process_all(fname0: string): bool {
if Options.opt.print_ast0 {
for m <- Ast.all_modules_sorted {
val minfo = Ast.get_module(m)
AstPP.pprint_mod(minfo)
Ast_pp.pprint_mod(minfo)
}
}
val modules_used = ", ".join(Ast.all_modules_sorted.map(Ast.pp))
Expand All @@ -342,16 +342,16 @@ fun process_all(fname0: string): bool {
if ok && Options.opt.print_ast {
for m <- Ast.all_modules_sorted {
val minfo = Ast.get_module(m)
AstPP.pprint_mod(minfo)
Ast_pp.pprint_mod(minfo)
}
}
val (kmods, ok) = if ok { k_normalize_all(Ast.all_modules_sorted) } else { ([], false) }
pr_verbose("K-normalization complete")
if ok && Options.opt.print_k0 { KPP.pp_kmods(kmods) }
if ok && Options.opt.print_k0 { K_pp.pp_kmods(kmods) }
pr_verbose("K-form optimization started")
val (kmods, ok) = if ok { k_optimize_all(kmods) } else { ([], false) }
if ok { pr_verbose("K-form optimization complete") }
if ok && Options.opt.print_k { KPP.pp_kmods(kmods) }
if ok && Options.opt.print_k { K_pp.pp_kmods(kmods) }
/*if !options.gen_c { ok } else {
val (cmods, ok) = if ok { k2c_all(kmods) } else { ([], false) }
val (cmods, builddir, ok) = if ok { emit_c_files(fname0, cmods) } else { (cmods, ".", ok) }
Expand Down
2 changes: 1 addition & 1 deletion compiler/KAnnotate.fx → compiler/K_annotate.fx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

from Ast import *
from KForm import *
from K_form import *
import Map, Set

// For each type finds the set of its direct dependencies,
Expand Down
6 changes: 3 additions & 3 deletions compiler/KConstFold.fx → compiler/K_cfold_dealias.fx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
*/

from Ast import *
from KForm import *
import KPP
from K_form import *
import K_pp
import Math, Map

type aclass_t =
Expand Down Expand Up @@ -294,7 +294,7 @@ fun print_subst_map(m: idamap_t, loc: loc_t) {
println("subsitution map {")
m.app(fun (n, a) {
println(f"\t{idk2str(n, loc)}: ")
KPP.pp_atom(a, loc)
K_pp.pp_atom(a, loc)
println(";")
})
println("}")
Expand Down
2 changes: 1 addition & 1 deletion compiler/KFastIdx.fx → compiler/K_fast_idx.fx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
for-loop with while-loop, where such optimization is not applied.
*/
from Ast import *
from KForm import *
from K_form import *

import Map, Set

Expand Down
2 changes: 1 addition & 1 deletion compiler/KFlatten.fx → compiler/K_flatten.fx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
(hence the name `flatten`).
*/
from Ast import *
from KForm import *
from K_form import *

@private fun flatten_ktyp_(t: ktyp_t, loc: loc_t, callb: k_callb_t) = t
@private fun flatten_kexp_(e: kexp_t, callb: k_callb_t) =
Expand Down
File renamed without changes.
Loading

0 comments on commit a9b7ae1

Please sign in to comment.