CompiledCodeObject.st
changeset 2 88445baa732f
child 5 7b9132bf8295
equal deleted inserted replaced
1:aa002d0c231b 2:88445baa732f
       
     1 "
       
     2 Copyright (c) 2015-now Jan Vrany <jan.vrany@fit.cvut.cz>
       
     3 
       
     4 Permission is hereby granted, free of charge, to any person obtaining a copy
       
     5 of this software and associated documentation files (the 'Software'), to deal
       
     6 in the Software without restriction, including without limitation the rights
       
     7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       
     8 copies of the Software, and to permit persons to whom the Software is
       
     9 furnished to do so, subject to the following conditions:
       
    10 
       
    11 The above copyright notice and this permission notice shall be included in
       
    12 all copies or substantial portions of the Software.
       
    13 
       
    14 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
       
    15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
       
    16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
       
    17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       
    18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
       
    19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
       
    20 THE SOFTWARE.
       
    21 "
       
    22 "{ Package: 'jv:dragonfly' }"
       
    23 
       
    24 "{ NameSpace: Smalltalk }"
       
    25 
       
    26 ExternalAddress subclass:#CompiledCodeObject
       
    27 	instanceVariableNames:'sections'
       
    28 	classVariableNames:''
       
    29 	poolDictionaries:''
       
    30 	category:'System-Compiler-Interface'
       
    31 !
       
    32 
       
    33 !CompiledCodeObject primitiveDefinitions!
       
    34 %{
       
    35 
       
    36 /*
       
    37  * includes, defines, structure definitions
       
    38  * and typedefs come here.
       
    39  */
       
    40 #include "../librun/mcompiler.h"
       
    41 
       
    42 %}
       
    43 ! !
       
    44 
       
    45 !CompiledCodeObject class methodsFor:'documentation'!
       
    46 
       
    47 copyright
       
    48 "
       
    49 Copyright (c) 2015-now Jan Vrany <jan.vrany@fit.cvut.cz>
       
    50 
       
    51 Permission is hereby granted, free of charge, to any person obtaining a copy
       
    52 of this software and associated documentation files (the 'Software'), to deal
       
    53 in the Software without restriction, including without limitation the rights
       
    54 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       
    55 copies of the Software, and to permit persons to whom the Software is
       
    56 furnished to do so, subject to the following conditions:
       
    57 
       
    58 The above copyright notice and this permission notice shall be included in
       
    59 all copies or substantial portions of the Software.
       
    60 
       
    61 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
       
    62 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
       
    63 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
       
    64 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       
    65 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
       
    66 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
       
    67 THE SOFTWARE.
       
    68 "
       
    69 ! !
       
    70 
       
    71 !CompiledCodeObject class methodsFor:'instance creation'!
       
    72 
       
    73 forCompiledCode: aCompiledCode
       
    74     "Given a method or block, return coresponding code
       
    75      object for it. If the method or block is not dynamically compiled,
       
    76      throw an exception"
       
    77 
       
    78      | instance |
       
    79 
       
    80      instance := self new.
       
    81 %{
       
    82     stx_compiled_code_object code_object;
       
    83     code_object = stxCompiledCodeObjectForCompiledCode(aCompiledCode);
       
    84     if (code_object != NULL) {
       
    85         __externalAddressVal(instance) = code_object;
       
    86         RETURN ( instance );
       
    87     }
       
    88 %}.
       
    89     self error: 'Not a dynamic code or not yet compiled'
       
    90 
       
    91     "Created: / 07-12-2015 / 10:58:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    92 ! !
       
    93 
       
    94 !CompiledCodeObject methodsFor:'accessing'!
       
    95 
       
    96 methodOrBlock
       
    97     "Return method or block associated with this code object."
       
    98 %{
       
    99     stx_compiled_code_object code_object = (stx_compiled_code_object)(__externalAddressVal(self));
       
   100     if (code_object != NULL) {
       
   101         RETURN ( stxCompiledCodeObjectGetCompiledCode(code_object) );
       
   102     }
       
   103 %}.
       
   104     ^ 0
       
   105 
       
   106     "Created: / 07-12-2015 / 11:51:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   107 !
       
   108 
       
   109 sections
       
   110     | numSections |
       
   111 
       
   112     numSections := self numSections.
       
   113     (sections isNil or:[ sections size ~= numSections ]) ifTrue:[ 
       
   114         sections := (1 to: numSections) collect:[ :i | self getSection: i ].
       
   115     ].
       
   116     ^ sections
       
   117 
       
   118     "Created: / 07-12-2015 / 17:09:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   119 ! !
       
   120 
       
   121 !CompiledCodeObject methodsFor:'inspecting'!
       
   122 
       
   123 inspector2TabText
       
   124     <inspector2Tab>   
       
   125 
       
   126     self sections do:[:section | 
       
   127         section isTextSection ifTrue:[ 
       
   128             | tab |
       
   129 
       
   130             tab := section inspector2TabAssembly.
       
   131             tab notNil ifTrue:[
       
   132                 tab label: '.text'.
       
   133                 ^ tab.
       
   134             ].                    
       
   135         ].
       
   136     ].
       
   137     ^ nil
       
   138 
       
   139     "Created: / 11-12-2015 / 12:04:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   140     "Modified: / 11-12-2015 / 14:11:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   141 ! !
       
   142 
       
   143 !CompiledCodeObject methodsFor:'private'!
       
   144 
       
   145 getSection: index
       
   146     | name addr size type |
       
   147 %{
       
   148     if (__isSmallInteger(index)) {
       
   149         stx_compiled_code_object code_object = (stx_compiled_code_object)(__externalAddressVal(self));
       
   150         stx_compiled_code_object_section code_section;
       
   151 
       
   152         code_section = stxCompiledCodeObjectGetSection ( code_object , __intVal( index ) - 1 );
       
   153         if (code_section) {
       
   154             name = __MKSTRING ( code_section->section_name );
       
   155             size = __MKSMALLINT ( code_section->section_size );
       
   156             __PROTECT__(name);
       
   157             addr = __MKUINT( code_section->section_addr );
       
   158             __UNPROTECT__(name);
       
   159             type = __MKSMALLINT ( code_section->section_type );
       
   160         }
       
   161     }
       
   162 %}.
       
   163     (name notNil and:[ addr notNil and:[ size notNil ]]) ifTrue:[ 
       
   164         ^ CompiledCodeObjectSection basicNew setObject: self name: name address: addr size: size type: type
       
   165     ].
       
   166     self primitiveFailed.
       
   167 
       
   168     "Created: / 07-12-2015 / 17:15:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   169     "Modified: / 11-12-2015 / 10:06:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   170 !
       
   171 
       
   172 numSections
       
   173     "Return the number of sections of this code object"
       
   174 
       
   175 %{
       
   176     stx_compiled_code_object code_object = (stx_compiled_code_object)(__externalAddressVal(self));
       
   177     RETURN ( __MKSMALLINT( stxCompiledCodeObjectGetSectionCount ( code_object ) ) );
       
   178 %}.
       
   179     self primitiveFailed.
       
   180 
       
   181     "Created: / 07-12-2015 / 16:36:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   182 ! !
       
   183