Skip to content

Commit

Permalink
Revert "Emit & Dyn_emit now wrap string.format + Vector instruction s…
Browse files Browse the repository at this point in the history
…egment fix (wiremod#27)"

This reverts commit 52f9dcc.
  • Loading branch information
DerelictDrone committed Dec 10, 2023
1 parent 524d49b commit 7cfb84a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 168 deletions.
8 changes: 4 additions & 4 deletions lua/entities/gmod_wire_cpu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@ function ENT:Run()
self:Dyn_EmitState()
self:Emit("VM.CPUIF.OnBreakpointInstruction(VM.IP)")
self:Emit("VM.CPUIF.VMStopped = true")
self:Emit("VM.TMR = VM.TMR + %d",self.PrecompileInstruction)
self:Emit("VM.CODEBYTES = VM.CODEBYTES + %d",self.PrecompileBytes)
self:Emit("VM.TMR = VM.TMR + "..self.PrecompileInstruction)
self:Emit("VM.CODEBYTES = VM.CODEBYTES + "..self.PrecompileBytes)
self:Emit("if true then return end")
self:Emit("end")
self:Emit("if VM.CPUIF.LastInstruction and ((VM.IP > VM.CPUIF.LastInstruction) or VM.CPUIF.ForceLastInstruction) then")
self:Dyn_EmitState()
self:Emit("VM.CPUIF.ForceLastInstruction = nil")
self:Emit("VM.CPUIF.OnLastInstruction()")
self:Emit("VM.CPUIF.VMStopped = true")
self:Emit("VM.TMR = VM.TMR + %d",self.PrecompileInstruction)
self:Emit("VM.CODEBYTES = VM.CODEBYTES + %d",self.PrecompileBytes)
self:Emit("VM.TMR = VM.TMR + "..self.PrecompileInstruction)
self:Emit("VM.CODEBYTES = VM.CODEBYTES + "..self.PrecompileBytes)
self:Emit("if true then return end")
self:Emit("end")
end)
Expand Down
52 changes: 25 additions & 27 deletions lua/wire/zvm/zvm_core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
-- Virtual machine implementation core
--------------------------------------------------------------------------------
ZVM = {}
-- TODO: Remove microcode debugging, it's not possible to access normally.
if not SERVER and not CLIENT then
ZVM.MicrocodeDebug = true
end
Expand All @@ -27,7 +26,6 @@ include("wire/zvm/zvm_data.lua")
if ZVM.MicrocodeDebug then -- Debug microcode generator
local pad = 0
function ZVM:Emit(text)
-- TODO: Remove microcode debugging, it's not possible to access normally.
if string.find(text,"end") and (not string.find(text,"if"))
then pad = pad - 1 end

Expand All @@ -42,8 +40,8 @@ if ZVM.MicrocodeDebug then -- Debug microcode generator
then pad = pad + 1 end
end
else
function ZVM:Emit(...)
self.EmitBlock = self.EmitBlock..string.format(...).."\n"
function ZVM:Emit(text)
self.EmitBlock = self.EmitBlock..text.."\n"
end
end

Expand Down Expand Up @@ -157,8 +155,8 @@ end

--------------------------------------------------------------------------------
-- Emit preprocessed text
function ZVM:Dyn_Emit(...)
self:Emit(self:Dyn_PreprocessEmit(string.format(...)))
function ZVM:Dyn_Emit(text)
self:Emit(self:Dyn_PreprocessEmit(text))
end


Expand Down Expand Up @@ -198,11 +196,11 @@ end
--------------------------------------------------------------------------------
-- Emit forced block return
function ZVM:Dyn_EmitBreak(emitIP)
self:Emit("VM.TMR = VM.TMR + %d",self.PrecompileInstruction)
self:Emit("VM.CODEBYTES = VM.CODEBYTES + %d",self.PrecompileBytes)
self:Emit("VM.TMR = VM.TMR + "..self.PrecompileInstruction)
self:Emit("VM.CODEBYTES = VM.CODEBYTES + "..self.PrecompileBytes)
if emitIP then
self:Emit("VM.IP = %d",(self.PrecompileIP or 0))
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IP = "..(self.PrecompileIP or 0))
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
end
if self.ExtraEmitFunction then self.ExtraEmitFunction(self) end
self:Emit("if true then return end")
Expand Down Expand Up @@ -267,9 +265,9 @@ end
-- Emit interrupt call
function ZVM:Dyn_EmitInterrupt(intNo,intParam)
self:Dyn_EmitState()
self:Emit("VM.IP = %d",(self.PrecompileIP or 0))
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
self:Dyn_Emit("VM:Interrupt(%d,%d)",intNo,intParam)
self:Emit("VM.IP = "..(self.PrecompileIP or 0))
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
self:Dyn_Emit("VM:Interrupt("..intNo..","..intParam..")")
self:Dyn_EmitBreak()
end

Expand All @@ -282,23 +280,23 @@ function ZVM:Dyn_EmitInterruptCheck()
if self.RQCAP == 1 then
self:Emit("if VM.MEMRQ > 0 then") -- Extended memory request
self:Emit("if VM.MEMRQ == 1 then") -- Delayed request
self:Emit("VM.IP = %d",self.PrecompileStartIP)
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IP = "..self.PrecompileStartIP)
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IDLE = 1")
self:Dyn_EmitState(true)
self:Dyn_EmitBreak()
self:Emit("elseif VM.MEMRQ == 2 then") -- Reading
self:Dyn_EmitState(true)
self:Emit("VM.MEMRQ = 4")
self:Emit("VM.IP = %d",self.PrecompileStartIP)
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IP = "..self.PrecompileStartIP)
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
self:Emit("VM:Interrupt(28,VM.LADD)")
self:Dyn_EmitBreak()
self:Emit("elseif VM.MEMRQ == 3 then") -- Writing
self:Dyn_EmitState(true)
self:Emit("VM.MEMRQ = 5")
self:Emit("VM.IP = %d",self.PrecompileStartIP)
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IP = "..self.PrecompileStartIP)
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
self:Emit("VM:Interrupt(29,VM.LADD)")
self:Dyn_EmitBreak()
self:Emit("end")
Expand All @@ -319,7 +317,7 @@ function ZVM:Dyn_EndBlock()
self:Dyn_EmitState()
self:Dyn_EmitBreak(true)
end
-- TODO: Remove microcode debugging, it's not possible to access normally.

if self.MicrocodeDebug then
if Msg then
local str = self.EmitBlock
Expand Down Expand Up @@ -408,12 +406,12 @@ function ZVM:Precompile_Step()

-- Check if we crossed the page boundary, if so - repeat the check
if math.floor(self.PrecompileXEIP / 128) ~= self.PrecompilePreviousPage then
self:Emit("VM:SetCurrentPage(%d)",math.floor(self.PrecompileXEIP/128))
self:Emit("VM:SetCurrentPage("..math.floor(self.PrecompileXEIP/128)..")")
self:Emit("if (VM.PCAP == 1) and (VM.CurrentPage.Execute == 0) and")
self:Emit(" (VM.PreviousPage.RunLevel ~= 0) then")
self:Dyn_EmitInterrupt("14",self.PrecompileIP)
self:Emit("end")
self:Emit("VM:SetPreviousPage(%d)",math.floor(self.PrecompileXEIP/128))
self:Emit("VM:SetPreviousPage("..math.floor(self.PrecompileXEIP/128)..")")

self.PrecompilePreviousPage = math.floor(self.PrecompileXEIP / 128)
end
Expand Down Expand Up @@ -444,7 +442,7 @@ function ZVM:Precompile_Step()

-- Check opcode runlevel
if self.OpcodeRunLevel[Opcode] then
self:Emit("if (VM.PCAP == 1) and (VM.CurrentPage.RunLevel > %d) then",self.OpcodeRunLevel[Opcode])
self:Emit("if (VM.PCAP == 1) and (VM.CurrentPage.RunLevel > "..self.OpcodeRunLevel[Opcode]..") then")
self:Dyn_EmitInterrupt("13",Opcode)
self:Emit("end")
end
Expand Down Expand Up @@ -538,8 +536,8 @@ function ZVM:Precompile_Step()

-- Emit interrupt check prefix
if self.EmitNeedInterruptCheck then
self:Emit("VM.IP = %d",(self.PrecompileIP or 0))
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IP = "..(self.PrecompileIP or 0))
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
end

-- Emit opcode
Expand Down Expand Up @@ -602,8 +600,8 @@ function ZVM:Step(overrideSteps,extraEmitFunction)
local instruction = 1
while (instruction <= overrideSteps) and self:Precompile_Step() do
if self.ExtraEmitFunction then
self:Emit("VM.IP = %d",(self.PrecompileIP or 0))
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IP = "..(self.PrecompileIP or 0))
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
self.ExtraEmitFunction(self)
end
instruction = instruction + 1
Expand Down
Loading

0 comments on commit 7cfb84a

Please sign in to comment.