<div dir="ltr">Hi again,<br><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jun 12, 2016 at 10:44 AM, Clément Bera <span dir="ltr">&lt;<a href="mailto:bera.clement@gmail.com" target="_blank">bera.clement@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Ben,<div><br></div><div>I&#39;m glad you&#39;re now looking into the JIT. If you have some blog or something, please write an experience report about you looking into the simulator. It&#39;s helpful for us to have noise around the VM.</div><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Sun, Jun 12, 2016 at 8:35 AM, Ben Coman <span dir="ltr">&lt;<a href="mailto:btc@openinworld.com" target="_blank">btc@openinworld.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><br>
I am stepping for the first time through the CogVM, having [set break<br>
selector...] forkAt:<br>
After stepping in a few times I get to #activateCoggedNewMethod.<br>
  CogVMSimulatorLSB(CoInterpreter)&gt;&gt;dispatchOn:in:<br>
  CogVMSimulatorLSB(CoInterpreter)&gt;&gt;sendLiteralSelector1ArgBytecode<br>
  CogVMSimulatorLSB(CoInterpreter)&gt;&gt;commonSendOrdinary<br>
  CogVMSimulatorLSB(CoInterpreter)&gt;&gt;insternalExecuteNewMethod<br>
  CogVMSimulatorLSB(CoInterpreter)&gt;&gt;activateCoggedNewMethod<br>
<br>
Here from the code at the top.<br>
    methodHeader := self rawHeaderOf: newMethod.<br>
    self assert: (self isCogMethodReference: methodHeader).<br>
    cogMethod := self cCoerceSimple: methodHeader to: #&#39;CogMethod *&#39;.<br>
    methodHeader := cogMethod methodHeader.<br>
<br>
I guess methodHeader&#39;s double assignment above is related to the<br>
machine code frame having two addresses as Clement described...<br></blockquote><div><br></div></span><div>Errr... I don&#39;t really fancy the way you say it but I think yes that&#39;s it.</div><div><br></div><div>A method can have 2 addresses, the address of the bytecoded version in the heap and the address of its jitted version in the machine code zone. In the machine code frame printing, the simulator displays the 2 addresses. But the frame has a single pointer to the method.</div><div><br></div><div>So what you&#39;re looking at is the dispatch logic from the bytecoded method to the jitted method. When the JIT compiles a bytecoded method to machine code, it replaces the bytecoded method compiled method header (first literal) by a pointer to the jitted version. The machine code version of the method keeps the compiled method header, so accessing it is different in methods compiled to machine code and methods not compiled to machine code. </div><div><br></div><div>#rawHeaderOf: answers the first literal of the bytecoded method which is a pointer to the jitted version of the method if the method has a jitted version, else is the compiled method header. In the code you show, the VM ensures the method has a jitted version with the assertion, hence the compiled method header is fetched from the jitted version.</div><span class=""><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<span><br>
&gt;&gt; On Mon, May 30, 2016 at 4:12 PM, Clément Bera &lt;<a href="mailto:bera.clement@gmail.com" target="_blank">bera.clement@gmail.com</a>&gt; wrote:<br>
</span><span>&gt;&gt;&gt; Now that you&#39;ve print the frame, you can see the method addresses in this line:<br>
&gt;&gt;&gt; 16r103144:      method:    16r51578  16r102BDD0 16r102BDD0: a(n) CompiledMethod.<br>
&gt;&gt;&gt; This is a machine code frame, so the method has two addresses:<br>
&gt;&gt;&gt; 16r51578 =&gt; in generated method, so you need to use [disassembleMethod/trampoline...] and write down the hex to see the disassembly.<br>
</span><span>&gt;&gt;&gt; 16r102BDD0 =&gt; in the heap. This is the bytecode version of the method. You can print it using [print oop...]<br>
<br>
</span>This time...<br>
[print ext head frame] ==&gt;<br>
  16r101214 M BlockClosure&gt;forkAt: 16r2FC420: a(n) BlockClosure<br>
  16r101210: method:     16rBBF0  16rC4E948 16rC4E948: a(n) CompiledMethod<br>
<br>
self rawHeaderOf: newMethod ==&gt; 16rBBF0<br>
So the &quot;raw header&quot; is the cogged method.<br>
<br>
Looking at the output below, the space ship operator &lt;-&gt; seems to link<br>
between cogged method headers like a call stack, except   #forkAt:<br>
calls  #newProcess  which calls  #asContext<br>
<br>
[print cog method for...] 16rBBF0 ==&gt;<br>
  16rBBF0 &lt;-&gt; 16rBC80: method: 16rC4E948 selector: 16r6CC798 forkAt:<br>
<br>
[print cog method for...] 16rBC80 ==&gt;<br>
   16rBC80 &lt;-&gt; 16rBEA8: method: 16rC51970 prim 19 selector: 16r6D1620 newProcess<br>
<br>
[print cog method for...] 16rBEA8 ==&gt;<br>
   16rBEA8 &lt;-&gt;     16rBF28: method:   16rC518C0 selector:   16r76A600 asContext<br>
<br>
However the links don&#39;t seem to go back up the call stack but forward,<br>
to statements to be executed in the future.   So I am confused?<br></blockquote><div><br></div></span><div>Yeah it&#39;s the jitted version of the method header address, then &lt;-&gt;, then the jitted method entry point address, the bytecode version address, selector address.</div><div><br></div><div>The cogMethod header is used to store the bytecoded compiled method header (because it was replaced with a pointer to the cogMethod) and various flags.<br></div><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<br>
-------------<br>
<br>
Considering further [print cog method for...] 16rBBF0 ==&gt;<br>
  16rBBF0 &lt;-&gt; 16rBC80: method: 16rC4E948 selector: 16r6CC798 forkAt:<br>
<br>
[print oop...] 16r6CC798 ==&gt;<br>
   a(n) ByteSymbol nbytes 7  forkAt:<br>
<br>
Clement early advised is the bytecode version of the method is this...<br>
[print oop...] 16rC4E948 ==&gt;<br>
  16rC4E948: a(n) CompiledMethod nbytes 37<br>
     16rBBF0  is in generated methods<br>
   16r6D1620 #newProcess   16r6CC650 #priority:   16r6CC690 #resume<br>
   16r6CC798 #forkAt:   16rAE5490 a ClassBinding #BlockClosure -&gt; 16r0088D618<br>
  16rC4E968:  70/112 D0/208 88/136 10/16 E1/225 87/135 D2/210 7C/124<br>
  16rC4E970:  28/40 AF/175 BA/186 F3/243 20/32<br>
<br>
Now I&#39;ve been a bit slow on the uptake and only just realised, but to confirm...<br>
the line 16r6CC798 is the one specifying the method as BlockClosure&gt;&gt;forkAt:<br></blockquote><div><br></div></span><div>16r6CC798 is the address of the selector #forkAt: </div><span class=""><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<br>
For the last two lines, I notice the numbers before the slash (70, 88,<br>
10...) are the method bytecode, but what are the numbers after the<br>
slash?<br></blockquote><div><br></div></span><div>The bytecode in decimal instead of hexa I think.</div><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<br>
----------------<br>
<br>
In #activeCoggedNewMethod: the second assignment to methodHeader<br>
  ==&gt; 16r208000B<br>
<br>
which matches the mthhdr field of the raw header<br>
[print cog method header for...] 16rBBF0 ==&gt;<br>
    BBF0<br>
    objhdr: 8000000A000035<br>
    nArgs: 1 type: 2<br>
    blksiz: 90<br>
    method: C4E948<br>
    mthhdr: 208000B<br>
    selctr: 6CC798=#forkAt:<br>
    blkentry: 0<br>
    stackCheckOffset: 5E/BC4E<br>
    cmRefersToYoung: no cmIsFullBlock: no<br>
<br>
What is &quot;type: 2&quot; ?<br></blockquote><div><br></div></span><div>Haha.</div><div><br></div><div>Well when you iterate over the machine code zone you need to know what the current element you iterate on is. In the machine code zone there can be:</div><div>- cog method</div><div>- closed PICS</div><div>- open PICS</div><div>- free space</div><div>And now we&#39;re adding cog full block method but it&#39;s sharing the index with cog method and have a separated flag :-)</div><div><br></div><div>The type tells you what it is. Look at the Literal variables CMFree, CMClosedPIC, CMOpenPIC, etc .</div><div><br></div><div>2 is CMMethod with is a constant. You can improve the printing there and commit the changes if you feel so.</div></div></div></div></blockquote><div><br></div><div>What did I write here I don&#39;t understand myself ? I mean CMMethod = 2, so type = 2 means the struct you&#39;re looking at in the machine code zone is a method and not free space or a PIC. </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>Ok I have to go I will look at the rest of your mail later.</div></div></div></div></blockquote><div><br></div><div>Let&#39;s do this... </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<br>
--------------------------<br>
<br>
Stepping through to  Cogit&gt;&gt;ceEnterCogCodePopReceiverReg<br>
I notice its protocol is &quot;simulation only&quot;<br>
and it calls  &quot;simulateEnilopmart:numArgs: ceEnterCogCodePopReceiverReg&quot;<br>
but I don&#39;t see any other implementors of #ceEnterCogCodePopReceiverReg.<br>
Also there is a pragma &lt;doNotGenerate&gt;.<br>
<br>
Obviously the real non-simulated VM works differently, but I can&#39;t<br>
determine how.<br>
<br>
btw, I have noticed that  ceEnterCogCodePopReceiverReg<br>
   ==&gt; 16r10B8<br>
and [print cog method for...] 16r10B8<br>
   ==&gt; trampoline ceEnterCogCodePopReceiverReg<br>
<br>
Is ceEnterCogCodePopReceiverReg provided by the platform C code?<br></blockquote></div></div></div></div></div></blockquote><div><br></div><div>Well it&#39;s in cogitIA32.c. I don&#39;t remember where it comes from.</div><div><br></div><div>Basically in Cog you have specific machine code routines, called trampolines, that switch from machine code to C code. When trampoline is written backward (Enilopmart) it means that the routine is meant to switch from C code to machine code.</div><div><br></div><div>The real VM calls in <span style="color:rgb(80,0,80)">ceEnterCogCodePopReceiverReg</span> a machine code routine that does the right thing (register remapped, maybe fp and sp saved, etc) to switch from the C runtime from the C compiler to the machine code runtime executing code generated by the JIT.</div><div><br></div><div>In simulation, the C code is simulated by executing Slang as Smalltalk code and the machine code is simulated using the processor simulator (Bochs for IA32). So it has to be done differently as there is no C stack with register state and stuff. Both trampolines and enilmoparts are simulated with specific code.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<br>
---------------------------<br>
Stepping through to simulateCogCodeAt:<br>
it called processor singleStepIn:minimumAddress:readOnlyBelow:<br>
which called BochsIA32Alien&gt;&gt;primitiveSingleStepInMemory:minimumAddress:readOnlyBelow:<br>
     &lt;primitive: &#39;primitiveSingleStepInMemoryMinimumAddressReadWrite&#39;<br>
       module: &#39;BochsIA32Plugin&#39;<br>
       error: ec&gt;<br>
     ^ec == #&#39;inappropriate operation&#39;<br>
         ifTrue: [self handleExecutionPrimitiveFailureIn: memoryArray<br>
                minimumAddress: minimumAddress]<br>
         ifFalse: [self reportPrimitiveFailure]<br>
<br>
and the debugger cursor was inside the ifTrue: statement.  I found I<br>
didn&#39;t have bochs installed, but after installing bochs-2.6-2, I go<br>
the same result. So could I get some background around this..<br>
<br>
Also I&#39;m curious how the simulator seemed to be running a CogVM before<br>
bochs was installed. Perhaps since I was not debugging through it, the<br>
machine code ran for real rather than being simulated.<br>
<br></blockquote></div></div></div></div></div></blockquote><div><br></div><div>No the machine code is always simulated. Bochs was working for sure if you successfully simulated the image on top of the cog simulator until the display was shown.</div><div><br></div><div>If you have a VM from one of Eliot&#39;s build (from the Cog blog) the processor simulators are present as plugins by default. On Mac you can do [show package contents...] and then look at the file inside to check the Bochs Plugin is there. It&#39;s not the case on the Pharo VMs so don&#39;t use them for CogVM simulation. You don&#39;t need to install anything.</div><div><br></div><div>On normal simulation the simulator goes often in the branch you&#39;ve just shown. It means it reached a simulation trap. As for enilmopart that can&#39;t be properly simulated, trampolines can&#39;t be simulated. So to simulate a trampoline the processor simulator fails a call and the trampoline is done in the simulation code. Look at #handleCallOrJumpSimulationTrap: for example.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
cheers -ben<br>
</blockquote></div></div></div><br></div></div>
</blockquote></div><br></div></div>