<commit_before>esprima = require 'esprima'
escodegen = require 'escodegen'
syntax = esprima.Syntax
debug = require 'debug'
path = require 'path'

{ Stack } = require 'stack'
{ Set } = require 'set'
{ TreeTransformer } = require 'nodevisitor'
closure_conversion = require 'closure-conversion'
optimizations = require 'optimizations'
{ startGenerator, bold, reset, is_intrinsic } = require 'echo-util'

{ ExitableScope, TryExitableScope, SwitchExitableScope, LoopExitableScope } = require 'exitable-scope'

types = require 'types'
consts = require 'consts'
runtime = require 'runtime'

llvm = require 'llvm'
ir = llvm.IRBuilder

opencode_intrinsics = true

BUILTIN_PARAMS = [
  { type: syntax.Identifier, name: "%closure", llvm_type: types.EjsValue } # should be EjsClosureEnv
  { type: syntax.Identifier, name: "%this",    llvm_type: types.EjsValue }
  { type: syntax.Identifier, name: "%argc",    llvm_type: types.int32 }
]

hasOwn = Object::hasOwnProperty

class LLVMIRVisitor extends TreeTransformer
        constructor: (@module, @filename, @options) ->

                @idgen = startGenerator()
                
                if @options.record_types
                        @genRecordId = startGenerator()
                        
                # build up our runtime method table
                @ejs_intrinsics =
                        getLocal:             @handleGetLocal
                        setLocal:             @handleSetLocal
                        getGlobal:            @handleGetGlobal
                        setGlobal:            @handleSetGlobal
                        slot:                 @handleGetSlot
                        setSlot:              @handleSetSlot
                        invokeClosure:        @handleInvokeClosure
                        makeClosure:          @handleMakeClosure
                        makeAnonClosure:      @handleMakeAnonClosure
                        createArgScratchArea: @handleCreateArgScratchArea
                        makeClosureEnv:       @handleMakeClosureEnv
                        typeofIsObject:       @handleTypeofIsObject
                        typeofIsFunction:     @handleTypeofIsFunction
                        typeofIsString:       @handleTypeofIsString
                        typeofIsSymbol:       @handleTypeofIsSymbol
                        typeofIsNumber:       @handleTypeofIsNumber
                        typeofIsUndefined:    @handleTypeofIsUndefined
                        typeofIsNull:         @handleTypeofIsNull
                        typeofIsBoolean:      @handleTypeofIsBoolean
                        builtinUndefined:     @handleBuiltinUndefined

                @llvm_intrinsics =
                        gcroot: -> module.getOrInsert "@llvm.gcroot"
                
                @ejs_runtime = runtime.createInterface module

                @module_atoms = Object.create null
                @literalInitializationFunction = @module.getOrInsertFunction "_ejs_module_init_string_literals_#{@filename}", types.void, []

                # initialize the scope stack with the global (empty) scope
                @scope_stack = new Stack Object.create null

                entry_bb = new llvm.BasicBlock "entry", @literalInitializationFunction
                return_bb = new llvm.BasicBlock "return", @literalInitializationFunction

                @doInsideBBlock entry_bb, =>
                        ir.createBr return_bb

                @doInsideBBlock return_bb, =>
                        ir.createRetVoid()

                @literalInitializationBB = entry_bb

        # lots of helper methods

        # result should be the landingpad's value
        beginCatch: (result) -> ir.createCall @ejs_runtime.begin_catch, [(ir.createPointerCast result, types.int8Pointer, "")], "catchval"
        endCatch:            -> ir.createCall @ejs_runtime.end_catch, [], "endcatch"

        doInsideExitableScope: (scope, f) ->
                scope.enter()
                f()
                scope.leave()
                
        doInsideBBlock: (b, f) ->
                saved = ir.getInsertBlock()
                ir.setInsertPoint b
                f()
                ir.setInsertPoint saved
        <commit_msg>put these on the same line<commit_after>esprima = require 'esprima'
escodegen = require 'escodegen'
syntax = esprima.Syntax
debug = require 'debug'
path = require 'path'

{ Stack } = require 'stack'
{ Set } = require 'set'
{ TreeTransformer } = require 'nodevisitor'
closure_conversion = require 'closure-conversion'
optimizations = require 'optimizations'
{ startGenerator, bold, reset, is_intrinsic } = require 'echo-util'

{ ExitableScope, TryExitableScope, SwitchExitableScope, LoopExitableScope } = require 'exitable-scope'

types = require 'types'
consts = require 'consts'
runtime = require 'runtime'

llvm = require 'llvm'
ir = llvm.IRBuilder

opencode_intrinsics = true

BUILTIN_PARAMS = [
  { type: syntax.Identifier, name: "%closure", llvm_type: types.EjsValue } # should be EjsClosureEnv
  { type: syntax.Identifier, name: "%this",    llvm_type: types.EjsValue }
  { type: syntax.Identifier, name: "%argc",    llvm_type: types.int32 }
]

hasOwn = Object::hasOwnProperty

class LLVMIRVisitor extends TreeTransformer
        constructor: (@module, @filename, @options) ->

                @idgen = startGenerator()
                
                if @options.record_types
                        @genRecordId = startGenerator()
                        
                # build up our runtime method table
                @ejs_intrinsics =
                        getLocal:             @handleGetLocal
                        setLocal:             @handleSetLocal
                        getGlobal:            @handleGetGlobal
                        setGlobal:            @handleSetGlobal
                        slot:                 @handleGetSlot
                        setSlot:              @handleSetSlot
                        invokeClosure:        @handleInvokeClosure
                        makeClosure:          @handleMakeClosure
                        makeAnonClosure:      @handleMakeAnonClosure
                        createArgScratchArea: @handleCreateArgScratchArea
                        makeClosureEnv:       @handleMakeClosureEnv
                        typeofIsObject:       @handleTypeofIsObject
                        typeofIsFunction:     @handleTypeofIsFunction
                        typeofIsString:       @handleTypeofIsString
                        typeofIsSymbol:       @handleTypeofIsSymbol
                        typeofIsNumber:       @handleTypeofIsNumber
                        typeofIsUndefined:    @handleTypeofIsUndefined
                        typeofIsNull:         @handleTypeofIsNull
                        typeofIsBoolean:      @handleTypeofIsBoolean
                        builtinUndefined:     @handleBuiltinUndefined

                @llvm_intrinsics =
                        gcroot: -> module.getOrInsert "@llvm.gcroot"
                
                @ejs_runtime = runtime.createInterface module

                @module_atoms = Object.create null
                @literalInitializationFunction = @module.getOrInsertFunction "_ejs_module_init_string_literals_#{@filename}", types.void, []

                # initialize the scope stack with the global (empty) scope
                @scope_stack = new Stack Object.create null

                entry_bb = new llvm.BasicBlock "entry", @literalInitializationFunction
                return_bb = new llvm.BasicBlock "return", @literalInitializationFunction

                @doInsideBBlock entry_bb, => ir.createBr return_bb
                @doInsideBBlock return_bb, => ir.createRetVoid()

                @literalInitializationBB = entry_bb

        # lots of helper methods

        # result should be the landingpad's value
        beginCatch: (result) -> ir.createCall @ejs_runtime.begin_catch, [(ir.createPointerCast result, types.int8Pointer, "")], "catchval"
        endCatch:            -> ir.createCall @ejs_runtime.end_catch, [], "endcatch"

        doInsideExitableScope: (scope, f) ->
                scope.enter()
                f()
                scope.leave()
                
        doInsideBBlock: (b, f) ->
                saved = ir.getInsertBlock()
                ir.setInsertPoint b
                f()
                ir.setInsertPoint saved
        
{'fraction_non_alphanumeric': 0.0726668249821895, 'fraction_numerical': 0.0007124198527665638, 'mean_word_length': 1.679923639834553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2928251', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2027, 'n_words': 459}