|
Class: ByteCodeCompiler
Object
|
+--Scanner
|
+--Parser
|
+--ByteCodeCompiler
|
+--ByteCodeCompilerWithBreakpointSupport
|
+--Decompiler
|
+--InstrumentingCompiler
- Package:
- stx:libcomp
- Category:
- System-Compiler
- Version:
- rev:
1.316
date: 2019/05/23 09:09:35
- user: cg
- file: ByteCodeCompiler.st directory: libcomp
- module: stx stc-classLibrary: libcomp
- Author:
- Claus Gittinger
This class performs compilation into ByteCodes.
First, parsing is done using superclass methods,
then the parse-tree is converted into an array of symbolic codes
and a relocation table;
these two are finally combined into a byteArray of the codes.
(the intermediate step through symbolic codes is for debugging
only - it may vanish in future releases)
There are many dependencies to the run-time-system (especially the
interpreter) in here - be careful when playing around ...
[Instance variables:]
codeBytes <ByteArry> bytecodes
codeIndex <SmallInteger> next index to put into code array
litArray <OrderedCollection> literals
stackDelta <SmallInteger> return value of byteCodeFor:
extra <Symbol> return value of byteCodeFor:
lineno <Boolean> return value of byteCodeFor:
extraLiteral <Symbol> return value of byteCodeFor:
maxStackDepth <SmallInteger> stack need of method
relocList <Array> used temporary for relocation
[Class variables:]
JumpToAbsJump <Dictionary> internal table to map opcodes
STCCompilationDefines passed to stc as command line arguments
STCCompilationIncludes
STCCompilationOptions
<String>
STCCompilation <Symbol> #always, #primitiveOnly or #never
controls when stc compilation is wanted
ShareCode <Boolean> reuse byteArrays for common (simple) code sequences
This is normally a 'good' optimization,
except if you plan to modify the byteCodes.
compiling methods
-
compile: methodText forClass: classToCompileFor
-
compile a source-string for a method in classToCompileFor.
Returns the new method, #Error or nil.
-
compile: aString forClass: aClass inCategory: cat
-
compile a source-string for a method in classToCompileFor.
The method will get cat as category.
Returns the new method, #Error or nil.
-
compile: aString forClass: aClass inCategory: cat notifying: requestor
-
compile a source-string for a method in classToCompileFor.
errors are forwarded to requestor.
The method will get cat as category.
Returns the new method, #Error or nil.
-
compile: aString forClass: aClass inCategory: cat notifying: requestor install: install
-
compile a source-string for a method in classToCompileFor.
The install-argument controls if the method is to be installed into the
classes method-dictionary, or just to be compiled and a method object to be returned.
Errors are forwarded to requestor. The method will get cat as category.
Returns the new method, #Error or nil.
-
compile: aString forClass: aClass inCategory: cat notifying: requestor install: install skipIfSame: skipIfSame
-
compile a source-string for a method in classToCompileFor.
The install-argument controls if the method is to be installed into the
classes method-dictionary, or just to be compiled and a method object to be returned.
Errors are forwarded to requestor. The method will get cat as category.
If skipIsSame is true, and the source is the same as an existing
methods source, this is a noop (for fast fileIn).
Returns the new method, #Error or nil.
-
compile: aString forClass: aClass inCategory: cat notifying: requestor install: install skipIfSame: skipIfSame silent: silent
-
compile a source-string for a method in aClass.
errors are forwarded to requestor.
The method will get cat as category.
if install is true, the method is installed in the class.
if skipIfSame, the method is not installed if there is no change
(used when filing in).
if silent is true, no warnings are output.
Returns the new method, #Error or nil.
-
compile: aStringArg forClass: aClassArg inCategory: cat notifying: requestor install: install skipIfSame: skipIfSame silent: silent foldConstants: fold
-
the basic workhorse method for compiling:
compile a source-string for a method in classToCompileFor.
errors are forwarded to requestor
(report on Transcript and return #Error, if requestor is nil).
The new method will get cat as category.
If install is true, the method will go into the classes method-table,
otherwise the method is simply returned (for anonymous methods).
If skipIsSame is true, and the source is the same as an existing
methods source, this is a noop (for fast fileIn).
The argument, silent controls if errors are to be reported.
Returns the method, #Error or nil.
-
compile: methodText forClass: classToCompileFor install: doInstall
-
compile a source-string for a method in classToCompileFor.
The install-argument controls if the method is to be installed into the
classes method-dictionary, or just to be compiled and a method object to be returned.
Returns the new method, #Error or nil.
-
compile: methodText forClass: classToCompileFor notifying: requestor
-
compile a source-string for a method in classToCompileFor.
Errors are forwarded to requestor.
Returns the new method, #Error or nil.
-
compile: textOrStream in: aClass notifying: requestor ifFail: exceptionBlock
-
name alias for ST-80 compatibility.
Returns the new method, or the value from exceptionBlock.
-
compile: textOrStream in: aClass notifying: requestor install: install ifFail: exceptionBlock
-
name alias for ST-80 compatibility.
Returns the new method, or the value from exceptionBlock.
-
stcCompileMethod: aMethod
-
constants
-
byteCodeFor: aSymbol
-
returns the numeric code for some symbolic bytecodes.
defaults
-
allowExtensionsToPrivateClasses
-
-
asYetUncategorizedMethodCategory
-
-
defaultMethodCategory
-
^ '** As yet uncategorized **'.
initialization
-
initialize
-
(comment from inherited method)
usually set to true in your .rc file
-
newCodeSet
-
-
newCodeSet: aBoolean
-
ByteCodeCompiler newCodeSet:true
-
newPrimitives
-
-
newPrimitives: aBoolean
-
ByteCodeCompiler newPrimitives:true
instance creation
-
new
-
Pretty ugly hack. A caller to compiler may provide a set of breakpoints
private-utilities
-
stringWithSimpleCRs: aString
-
stc compilation defaults
-
canCreateMachineCode
-
return true, if compilation to machine code is supported.
Currently, all SYSV4, Linux and WinNT/XP systems do so;
REAL/IX and HPUX9.x do not
(due to the need for dynamic loading of object files, which is not supported by those).
MIPS ULTRIX is almost finished, but not yet released.
(late note - we no longer care for REAL/IX, HPUX9.x and MIPS ULTRIX)
usage example(s):
Compiler canCreateMachineCode
|
-
ccCompilationOptions
-
return the options used with cc compilation.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
ccCompilationOptions: aString
-
define the compilation options
to be used when compiling to machine code.
These are passed to cc. Can be set from your private.rc file.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
ccPath
-
return the path to (name of) the cc command for incremental method compilation.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
ccPath: aPathOrCommandName
-
set the path to the cc command for incremental method compilation.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcCompilation
-
return the flag which controls compilation to machine code.
If #always, methods are always compiled to machine code (which takes
longer, but provides faster code). If #none, methods are never compiled
to machine code, instead for non-primitive ones, compilation is to bytecode
and for primitive ones, a trapping stub is generated.
Anything else lets the compiler compile to bytecode,
except for methods containing primitive code.
This can be set from your private.rc file or from a workspace
for selective compilation to machine code.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcCompilation: how
-
set the flag which controls compilation to machine code.
If #always, methods are always compiled to machine code (which takes
longer, but provides faster code). If #none, methods are never compiled
to machine code, instead for non-primitive ones, compilation is to bytecode
and for primitive ones, a trapping stub is generated.
Anything else lets the compiler compile to bytecode,
except for methods containing primitive code.
This can be set from your private.rc file or from a workspace
for selective compilation to machine code.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcCompilationDefines
-
return the defines used with stc compilation.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcCompilationDefines: aString
-
define the flags (for example, additional -D defines)
to be used when compiling to machine code.
These are passed to stc. Can be set from your private.rc file.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcCompilationIncludes
-
return the includes used with stc compilation.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcCompilationIncludes: aString
-
define the include directories via additional -I flags.
These are passed to stc. Can be set in your private.rc file.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcCompilationOptions
-
return the options used with stc compilation.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcCompilationOptions: aString
-
define the compilation options
to be used when compiling to machine code.
These are passed to stc. Can be set from your private.rc file.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcModulePath
-
return the path, where temporary modules are created.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcModulePath: aPath
-
set the path to the directory, where temporary modules are created.
Obsolete; knowledge moved to parserFlags,
where it is also obsolete now, as this should not be set from the outside,
but instead rely totally on the userPreferences.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcPath
-
return the path to the stc command, or nil if not found.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcPath: aPath
-
set the path to the stc command - useful if private stc is wanted.
This method remains here for backward compatibility (older script files)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
stcPathOf: command
-
return the path to an stc command, or nil if not found.
usage example(s):
Compatibility-ST80
-
compile: textOrStream in: aClass notifying: requestor ifFail: exceptionBlock
-
name alias for ST-80 compatibility
-
compile: textOrStream in: aClass notifying: requestor install: install ifFail: exceptionBlock
-
name alias for ST-80 compatibility
-
from: aStream class: aClass context: ctx notifying: aRequestor
-
accessing
-
code
-
return the bytecode array - only valid after code-generation
-
literalArray
-
return the literal array - only valid after parsing
-
maxStackDepth
-
return the stack-need of the method - only valid after code-generation
-
methodClass
-
-
methodClass: aClass
-
code generation
-
byteCodeFor: aSymbol
-
given a symbolic instruction, return the corresponding bytecode.
as a side-effect, leave number of bytes pushed/popped by this instr.
in stackDelta, and, if the instruction needs extra arguments, leave
this info in extra. Also lineno is set to true, if this code has line
information and extraLiteral is set if any hidden send is performed by it.
-
checkForCommonCode: symbolicCodeArray
-
hook to return the code for common code sequences.
This reduces the in-memory number of byteArrays somewhat.
Not yet fully implemented - just an idea ... there is certainly more to do here
(does it make sense to scan all methods, collect code in a set and unify things
automatically in the background - or upon request ?)
-
checkForPrimitiveCode: nr
-
return the code for an ST-80 primitive method.
Since many primitives available on ST-80 should also be available
somewhere in ST/X, this may work for many primitive numbers.
However, more information is needed and more things to be added below.
This was added to allow emulation of (some) ST-80
primitives (to fileIn RemoteInvocation & Monitor41 packages)
-
codeLineNumber: nr on: codeStream
-
generate lineNumber information
-
createMethod
-
fixup CheapBlocks method-field in literal array,
-
genByteCodeFrom: symbolicCodeArray
-
convert symbolicCode into bytecodes
-
genSpecialStatement: selector on: codeStream
-
generate: thisContext selector (to force a context).
-
genSymbolicCode
-
traverse the parse-tree producing symbolicCode - return the codeArray
-
generateVariables: varCollection on: codeStream
-
generate code to set it up.
-
relocateWith: symbolicCodeArray relocInfo: relocInfo
-
helper for genByteCodeFrom - relocate code using relocInfo.
if relocation fails badly (due to long relative jumps) patch
symbolicCode to use absolute jumps instead and return false
(genByteCodeFrom will then try again). Otherwise return true.
Also, on the fly, jumps to jumps and jumps to return are handled.
code generation helpers
-
absJumpFromJump: code
-
given a jump-symbolic code, return corresponding absolute jump
-
addLiteral: anObject
-
add a literal to the literalArray - watch for and eliminate
duplicates. return the index of the literal in the Array
-
addReloc: symIndex
-
remember to relocate offset at symIndex later ...
-
addTempVar
-
add a temporary variable; return its position (1-based).
Used when a block with args/locals is inlined.
-
appendByte: aByte
-
append a byte to the code-Array, checking for byte-range (debug-only)
-
appendByteCodeFor: codeSymbol
-
append the byteCode for an instructionSymbol to the code-Array
-
appendEmptyByte
-
append an empty byte to the code-Array
-
appendEmptyLong
-
append an empty long (4 bytes) to the code-Array
-
appendEmptyShort
-
append an empty short (2 bytes) to the code-Array
-
appendLongWord: aWord
-
append an unsigned long word (low-high) to the code-Array,
checking for long word-range (debug-only)
-
appendSignedByte: aByte
-
append a signedbyte (as in jump-offsets) to the code-Array,
check range and report an error if invalid
-
appendSignedWord: aWord
-
append a signed word to the code-Array,
check range and report an error if invalid
-
appendWord: aWord
-
append an unsigned word (low-high) to the code-Array,
checking for word-range (debug-only)
-
nameSpaceSelectorFor: aSymbol
-
Caring for the current namespace, return the real selector used for a send.
-
removeTempVar
-
remove a temporary variable
code generation hooks
-
startCodeGenerationHookOn: codeStream
-
invoked before code is generated;
gives subclasses a chance to prepare and to inject code to be
executed on entry (instrumentation)
compilation
-
compile: aString forClass: aClass inCategory: cat
-
compile a source-string for a method in classToCompileFor.
The method will get cat as category.
Returns the new method, #Error or nil.
-
compile: aStringArg forClass: aClassArg inCategory: cat notifying: aRequestor install: install skipIfSame: skipIfSame silent: silent foldConstants: fold
-
if proceeded, install a trap method
-
compile: sourceCodeStringArg forClass: aClassArg inCategory: cat notifying: aRequestor install: install skipIfSame: skipIfSame silent: silent foldConstants: fold ifFail: failBlock
-
the basic workhorse method for compiling:
compile a source-string for a method in classToCompileFor.
errors are forwarded to requestor
(report on Transcript and return the value of failBlock, if requestor is nil).
The new method will get cat as category.
If install is true, the method will go into the classes method-table,
otherwise the method is simply returned (for anonymous methods).
If skipIsSame is true, and the source is the same as an existing
methods source, this is a noop (for fast fileIn).
The argument, silent controls if errors are to be reported.
Returns the method, #Error or nil.
-
compile: methodText forClass: aBehavior install: doInstall
-
compile a source-string for a method in classToCompileFor.
The install-argument controls if the method is to be installed into the
classes method-dictionary, or just to be compiled and a method object to be returned.
Returns the new method, #Error or nil.
-
compileTree: aTree forClass: aClass
-
given an already parsed AST, generate code and return a method
-
compileTree: aMethodNode forClass: aClass ifFail: failBlock
-
given an already parsed AST, generate code and return a method
-
showErrorNotification: message
-
error handling
-
codeGeneratorError: aMessage
-
machine code generation
-
trappingStubMethodFor: aString inCategory: cat
-
return a stub method which traps and reports an error whenever
called.
queries
-
hasLineNumber: sel
-
return true, if special send code needs lineNr
-
isBuiltInSelector: sel forReceiver: receiver
-
return true, if selector sel is built-in.
(i.e. there is a single bytecode for it)
-
isCompiling
-
return true if compiling code as opposed to evaluating
-
numberOfTempVars
-
return the number of additional temporary variables which
were created from inlined blocks (valid after parsing)
-
specialGlobalCodeFor: aSymbol
-
codeExtension for globals,
which can be accessed by specialGlobal opCode
-
specialGlobals
-
list of globals which can be accessed by specialGlobal opCode;
adding any here requires a new VM (i.e. you cannot change it)
-
specialSendCodeFor: sel
-
return the codeExtension for sends,
which can be performed by specialSend opCode
-
specialSends
-
list of selectors which can be sent by specialSend opCode;
adding any here requires a new VM (i.e. you cannot change it)
a GNU-Smalltalk method:
Compiler
compile:'bla
<category: ''tests''>
^ 123
'
forClass: Object
|
|