diff --git a/compiler/src/prog8/ast/AstToplevel.kt b/compiler/src/prog8/ast/AstToplevel.kt
index dc05cee75..8e9a3509f 100644
--- a/compiler/src/prog8/ast/AstToplevel.kt
+++ b/compiler/src/prog8/ast/AstToplevel.kt
@@ -6,7 +6,6 @@ import prog8.ast.expressions.IdentifierReference
 import prog8.ast.walk.IAstVisitor
 import prog8.ast.statements.*
 import prog8.ast.walk.AstWalker
-import prog8.functions.BuiltinFunctions
 import java.nio.file.Path
 import kotlin.math.abs
 
@@ -246,8 +245,8 @@ interface IAssignable {
 
 /*********** Everything starts from here, the Program; zero or more modules *************/
 
-class Program(val name: String, val modules: MutableList<Module>): Node {
-    val namespace = GlobalNamespace(modules)
+class Program(val name: String, val modules: MutableList<Module>, builtinFunctionNames: Set<String>): Node {
+    val namespace = GlobalNamespace(modules, builtinFunctionNames)
 
     val definedLoadAddress: Int
         get() = modules.first().loadAddress
@@ -322,7 +321,7 @@ class Module(override val name: String,
 }
 
 
-class GlobalNamespace(val modules: List<Module>): Node, INameScope {
+class GlobalNamespace(val modules: List<Module>, private val builtinFunctionNames: Set<String>): Node, INameScope {
     override val name = "<<<global>>>"
     override val position = Position("<<<global>>>", 0, 0, 0)
     override val statements = mutableListOf<Statement>()        // not used
@@ -337,7 +336,7 @@ class GlobalNamespace(val modules: List<Module>): Node, INameScope {
     }
 
     override fun lookup(scopedName: List<String>, localContext: Node): Statement? {
-        if (scopedName.size == 1 && scopedName[0] in BuiltinFunctions) {
+        if (scopedName.size == 1 && scopedName[0] in builtinFunctionNames) {
             // builtin functions always exist, return a dummy localContext for them
             val builtinPlaceholder = Label("builtin::${scopedName.last()}", localContext.position)
             builtinPlaceholder.parent = ParentSentinel
diff --git a/compiler/src/prog8/compiler/Main.kt b/compiler/src/prog8/compiler/Main.kt
index 2d8476fe5..c94ff7413 100644
--- a/compiler/src/prog8/compiler/Main.kt
+++ b/compiler/src/prog8/compiler/Main.kt
@@ -4,9 +4,15 @@ import prog8.ast.AstToSourceCode
 import prog8.ast.Program
 import prog8.ast.base.*
 import prog8.ast.statements.Directive
+import prog8.compiler.astprocessing.*
+import prog8.compiler.astprocessing.addTypecasts
+import prog8.compiler.astprocessing.checkValid
+import prog8.compiler.astprocessing.processAstBeforeAsmGeneration
+import prog8.compiler.astprocessing.reorderStatements
 import prog8.compiler.target.C64Target
 import prog8.compiler.target.CompilationTarget
 import prog8.compiler.target.Cx16Target
+import prog8.functions.BuiltinFunctions
 import prog8.optimizer.*
 import prog8.optimizer.UnusedCodeRemover
 import prog8.optimizer.constantFold
@@ -91,13 +97,13 @@ fun compileProgram(filepath: Path,
         throw x
     }
 
-    return CompilationResult(false, Program("failed", mutableListOf()), programName, emptyList())
+    return CompilationResult(false, Program("failed", mutableListOf(), setOf()), programName, emptyList())
 }
 
 private fun parseImports(filepath: Path, errors: ErrorReporter): Triple<Program, CompilationOptions, List<Path>> {
     println("Compiler target: ${CompilationTarget.instance.name}. Parsing...")
     val importer = ModuleImporter()
-    val programAst = Program(moduleName(filepath.fileName), mutableListOf())
+    val programAst = Program(moduleName(filepath.fileName), mutableListOf(), BuiltinFunctions.keys)
     importer.importModule(programAst, filepath)
     errors.handle()
 
diff --git a/compiler/src/prog8/ast/processing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt
similarity index 99%
rename from compiler/src/prog8/ast/processing/AstChecker.kt
rename to compiler/src/prog8/compiler/astprocessing/AstChecker.kt
index 04b1df99c..78aac2ba5 100644
--- a/compiler/src/prog8/ast/processing/AstChecker.kt
+++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt
@@ -1,4 +1,4 @@
-package prog8.ast.processing
+package prog8.compiler.astprocessing
 
 import prog8.ast.INameScope
 import prog8.ast.Module
@@ -999,7 +999,8 @@ internal class AstChecker(private val program: Program,
             }
         }
 
-        val error = VerifyFunctionArgTypes.checkTypes(functionCallStatement, functionCallStatement.definingScope(), program)
+        val error =
+            VerifyFunctionArgTypes.checkTypes(functionCallStatement, functionCallStatement.definingScope(), program)
         if(error!=null) {
             errors.err(error, functionCallStatement.args.firstOrNull()?.position ?: functionCallStatement.position)
         }
diff --git a/compiler/src/prog8/compiler/AstExtensions.kt b/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt
similarity index 96%
rename from compiler/src/prog8/compiler/AstExtensions.kt
rename to compiler/src/prog8/compiler/astprocessing/AstExtensions.kt
index 37ea6a667..3ee7d2a96 100644
--- a/compiler/src/prog8/compiler/AstExtensions.kt
+++ b/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt
@@ -1,11 +1,12 @@
-package prog8.compiler
+package prog8.compiler.astprocessing
 
 import prog8.ast.Module
 import prog8.ast.Program
 import prog8.ast.base.ErrorReporter
 import prog8.ast.base.FatalAstException
-import prog8.ast.processing.*
 import prog8.ast.statements.Directive
+import prog8.compiler.BeforeAsmGenerationAstChanger
+import prog8.compiler.CompilationOptions
 
 
 internal fun Program.checkValid(compilerOptions: CompilationOptions, errors: ErrorReporter) {
diff --git a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt
similarity index 99%
rename from compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt
rename to compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt
index ac5756f9e..8f9af9f00 100644
--- a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt
+++ b/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt
@@ -1,4 +1,4 @@
-package prog8.ast.processing
+package prog8.compiler.astprocessing
 
 import prog8.ast.Module
 import prog8.ast.Program
diff --git a/compiler/src/prog8/ast/processing/AstVariousTransforms.kt b/compiler/src/prog8/compiler/astprocessing/AstVariousTransforms.kt
similarity index 99%
rename from compiler/src/prog8/ast/processing/AstVariousTransforms.kt
rename to compiler/src/prog8/compiler/astprocessing/AstVariousTransforms.kt
index 9ca1980f3..74e416e95 100644
--- a/compiler/src/prog8/ast/processing/AstVariousTransforms.kt
+++ b/compiler/src/prog8/compiler/astprocessing/AstVariousTransforms.kt
@@ -1,4 +1,4 @@
-package prog8.ast.processing
+package prog8.compiler.astprocessing
 
 import prog8.ast.Node
 import prog8.ast.Program
diff --git a/compiler/src/prog8/ast/processing/ImportedModuleDirectiveRemover.kt b/compiler/src/prog8/compiler/astprocessing/ImportedModuleDirectiveRemover.kt
similarity index 95%
rename from compiler/src/prog8/ast/processing/ImportedModuleDirectiveRemover.kt
rename to compiler/src/prog8/compiler/astprocessing/ImportedModuleDirectiveRemover.kt
index 4cc75c1f0..6c83a0aa5 100644
--- a/compiler/src/prog8/ast/processing/ImportedModuleDirectiveRemover.kt
+++ b/compiler/src/prog8/compiler/astprocessing/ImportedModuleDirectiveRemover.kt
@@ -1,4 +1,4 @@
-package prog8.ast.processing
+package prog8.compiler.astprocessing
 
 import prog8.ast.INameScope
 import prog8.ast.Node
diff --git a/compiler/src/prog8/ast/processing/LiteralsToAutoVars.kt b/compiler/src/prog8/compiler/astprocessing/LiteralsToAutoVars.kt
similarity index 98%
rename from compiler/src/prog8/ast/processing/LiteralsToAutoVars.kt
rename to compiler/src/prog8/compiler/astprocessing/LiteralsToAutoVars.kt
index e797da10c..ab4ac6ffd 100644
--- a/compiler/src/prog8/ast/processing/LiteralsToAutoVars.kt
+++ b/compiler/src/prog8/compiler/astprocessing/LiteralsToAutoVars.kt
@@ -1,4 +1,4 @@
-package prog8.ast.processing
+package prog8.compiler.astprocessing
 
 import prog8.ast.Node
 import prog8.ast.Program
diff --git a/compiler/src/prog8/ast/processing/ReflectionAstWalker.kt b/compiler/src/prog8/compiler/astprocessing/ReflectionAstWalker.kt
similarity index 98%
rename from compiler/src/prog8/ast/processing/ReflectionAstWalker.kt
rename to compiler/src/prog8/compiler/astprocessing/ReflectionAstWalker.kt
index c8eab449b..5e60ceff3 100644
--- a/compiler/src/prog8/ast/processing/ReflectionAstWalker.kt
+++ b/compiler/src/prog8/compiler/astprocessing/ReflectionAstWalker.kt
@@ -1,4 +1,4 @@
-package prog8.ast.processing
+package prog8.compiler.astprocessing
 
 
 /*
diff --git a/compiler/src/prog8/ast/processing/StatementReorderer.kt b/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt
similarity index 99%
rename from compiler/src/prog8/ast/processing/StatementReorderer.kt
rename to compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt
index eef6be1e3..af66d4149 100644
--- a/compiler/src/prog8/ast/processing/StatementReorderer.kt
+++ b/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt
@@ -1,4 +1,4 @@
-package prog8.ast.processing
+package prog8.compiler.astprocessing
 
 import prog8.ast.*
 import prog8.ast.base.*
diff --git a/compiler/src/prog8/ast/processing/TypecastsAdder.kt b/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt
similarity index 99%
rename from compiler/src/prog8/ast/processing/TypecastsAdder.kt
rename to compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt
index 76aa35212..b8b54ccb4 100644
--- a/compiler/src/prog8/ast/processing/TypecastsAdder.kt
+++ b/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt
@@ -1,4 +1,4 @@
-package prog8.ast.processing
+package prog8.compiler.astprocessing
 
 import prog8.ast.IFunctionCall
 import prog8.ast.INameScope
diff --git a/compiler/src/prog8/ast/processing/VariousCleanups.kt b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt
similarity index 97%
rename from compiler/src/prog8/ast/processing/VariousCleanups.kt
rename to compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt
index 9cfe9c5de..efd3d3d29 100644
--- a/compiler/src/prog8/ast/processing/VariousCleanups.kt
+++ b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt
@@ -1,4 +1,4 @@
-package prog8.ast.processing
+package prog8.compiler.astprocessing
 
 import prog8.ast.INameScope
 import prog8.ast.Node
diff --git a/compiler/src/prog8/ast/processing/VerifyFunctionArgTypes.kt b/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt
similarity index 99%
rename from compiler/src/prog8/ast/processing/VerifyFunctionArgTypes.kt
rename to compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt
index 310c23256..7e313ebfe 100644
--- a/compiler/src/prog8/ast/processing/VerifyFunctionArgTypes.kt
+++ b/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt
@@ -1,4 +1,4 @@
-package prog8.ast.processing
+package prog8.compiler.astprocessing
 
 import prog8.ast.IFunctionCall
 import prog8.ast.INameScope
diff --git a/compiler/src/prog8/parser/ModuleParsing.kt b/compiler/src/prog8/parser/ModuleParsing.kt
index f2f85a9bb..9166fa583 100644
--- a/compiler/src/prog8/parser/ModuleParsing.kt
+++ b/compiler/src/prog8/parser/ModuleParsing.kt
@@ -8,7 +8,7 @@ import prog8.ast.base.Position
 import prog8.ast.base.SyntaxError
 import prog8.ast.statements.Directive
 import prog8.ast.statements.DirectiveArg
-import prog8.compiler.checkImportedValid
+import prog8.compiler.astprocessing.checkImportedValid
 import prog8.compiler.target.CompilationTarget
 import prog8.pathFrom
 import java.io.InputStream