Navigation Menu

Skip to content

Commit

Permalink
Variable assignment with const.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinaychandra committed Sep 10, 2017
1 parent e1c9990 commit e777b36
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 26 deletions.
3 changes: 3 additions & 0 deletions playground/AsmBreak.asm
Expand Up @@ -27,13 +27,15 @@
; ComReadEAX() ; ComReadEAX()
; Save EIP of the break ; Save EIP of the break
; .AsmBreakEIP = EAX ; .AsmBreakEIP = EAX
Mov DWORD [DebugStub_Var_AsmBreakEIP], EAX
; EDI = EAX ; EDI = EAX
Mov EDI, EAX Mov EDI, EAX


; Save the old byte ; Save the old byte
; AL = [EDI] ; AL = [EDI]
Mov AL, BYTE [EDI] Mov AL, BYTE [EDI]
; .AsmOrigByte = AL ; .AsmOrigByte = AL
Mov BYTE [DebugStub_Var_AsmOrigByte], AL


; Inject INT3 ; Inject INT3
; Do in 2 steps to force a byte move to RAM (till X# can do byte in one step) ; Do in 2 steps to force a byte move to RAM (till X# can do byte in one step)
Expand All @@ -56,6 +58,7 @@
Mov BYTE [EDI], AL Mov BYTE [EDI], AL


; .AsmBreakEIP = 0 ; .AsmBreakEIP = 0
Mov DWORD [DebugStub_Var_AsmBreakEIP], 0x0
; } ; }


; function SetINT1_TrapFLAG { ; function SetINT1_TrapFLAG {
Expand Down
2 changes: 2 additions & 0 deletions playground/CmdMisc.asm
Expand Up @@ -10,9 +10,11 @@
; function TraceOn { ; function TraceOn {
; Tracing.On ; Tracing.On
; .TraceMode = 1 ; .TraceMode = 1
Mov DWORD [DebugStub_Var_TraceMode], 0x1
; } ; }


; function TraceOff { ; function TraceOff {
; Tracing.Off ; Tracing.Off
; .TraceMode = 0 ; .TraceMode = 0
Mov DWORD [DebugStub_Var_TraceMode], 0x0
; } ; }
1 change: 1 addition & 0 deletions playground/CmdProcess.asm
Expand Up @@ -19,6 +19,7 @@
Mov EAX, 0x0 Mov EAX, 0x0
; ComReadAL() ; ComReadAL()
; .CommandID = EAX ; .CommandID = EAX
Mov DWORD [DebugStub_Var_CommandID], EAX


; Get AL back so we can compare it, but also leave it for later ; Get AL back so we can compare it, but also leave it for later
; EAX = [ESP] ; EAX = [ESP]
Expand Down
18 changes: 9 additions & 9 deletions playground/CmdSend.asm
Expand Up @@ -13,11 +13,11 @@
; ComWriteX() ; ComWriteX()


; ESI = @.CallerESP ; ESI = @.CallerESP
Mov ESI, DebugStub_Const_CallerESP Mov ESI, DebugStub_Var_CallerESP
; ComWrite32() ; ComWrite32()


; ESI = @.CallerEIP ; ESI = @.CallerEIP
Mov ESI, DebugStub_Const_CallerEIP Mov ESI, DebugStub_Var_CallerEIP
; ComWrite32() ; ComWrite32()
; } ; }


Expand Down Expand Up @@ -184,7 +184,7 @@


; Send Calling EIP. ; Send Calling EIP.
; ESI = @.CallerEIP ; ESI = @.CallerEIP
Mov ESI, DebugStub_Const_CallerEIP Mov ESI, DebugStub_Var_CallerEIP
; ComWrite32() ; ComWrite32()
; } ; }


Expand Down Expand Up @@ -404,7 +404,7 @@ Mov EBP, ESP


; pointer value ; pointer value
; ESI = @.CallerEIP ; ESI = @.CallerEIP
Mov ESI, DebugStub_Const_CallerEIP Mov ESI, DebugStub_Var_CallerEIP
; ComWrite32() ; ComWrite32()
; } ; }


Expand All @@ -419,7 +419,7 @@ Mov EBP, ESP


; pointer value ; pointer value
; ESI = @.CallerEIP ; ESI = @.CallerEIP
Mov ESI, DebugStub_Const_CallerEIP Mov ESI, DebugStub_Var_CallerEIP
; ComWrite32() ; ComWrite32()
; } ; }


Expand Down Expand Up @@ -451,7 +451,7 @@ Mov EBP, ESP


; pointer value ; pointer value
; ESI = @.CallerEIP ; ESI = @.CallerEIP
Mov ESI, DebugStub_Const_CallerEIP Mov ESI, DebugStub_Var_CallerEIP
; ComWrite32() ; ComWrite32()
; } ; }


Expand Down Expand Up @@ -501,15 +501,15 @@ Mov EBP, ESP
; +ESI ; +ESI
Push ESI Push ESI
; EAX = @.CallerEBP ; EAX = @.CallerEBP
Mov EAX, DebugStub_Const_CallerEBP Mov EAX, DebugStub_Var_CallerEBP
; +EAX ; +EAX
Push EAX Push EAX
; EAX = @.CallerEIP ; EAX = @.CallerEIP
Mov EAX, DebugStub_Const_CallerEIP Mov EAX, DebugStub_Var_CallerEIP
; +EAX ; +EAX
Push EAX Push EAX
; EAX = @.CallerESP ; EAX = @.CallerESP
Mov EAX, DebugStub_Const_CallerESP Mov EAX, DebugStub_Var_CallerESP
; +EAX ; +EAX
Push EAX Push EAX
; ECX = 36 ; ECX = 36
Expand Down
18 changes: 15 additions & 3 deletions playground/DebugStub.asm
Expand Up @@ -46,7 +46,7 @@
; Calculate location in table ; Calculate location in table
; Mov [EBX + EAX * 4], ECX would be better, but our X# doesn't handle this yet ; Mov [EBX + EAX * 4], ECX would be better, but our X# doesn't handle this yet
; EBX = @.DebugBPs ; EBX = @.DebugBPs
Mov EBX, DebugStub_Const_DebugBPs Mov EBX, DebugStub_Var_DebugBPs
; EAX << 2 ; EAX << 2
; EBX += EAX ; EBX += EAX


Expand Down Expand Up @@ -92,7 +92,7 @@


; Load the current BP Id we are testing against ; Load the current BP Id we are testing against
; EBX = @.DebugBPs ; EBX = @.DebugBPs
Mov EBX, DebugStub_Const_DebugBPs Mov EBX, DebugStub_Var_DebugBPs
; EAX = ECX ; EAX = ECX
Mov EAX, ECX Mov EAX, ECX
; 4 bytes per Id ; 4 bytes per Id
Expand All @@ -112,6 +112,7 @@
; ECX++ ; ECX++
Inc ECX Inc ECX
; .MaxBPId = ECX ; .MaxBPId = ECX
Mov DWORD [DebugStub_Var_MaxBPId], ECX
; goto Continue ; goto Continue
; } ; }
; Has our count reached 0? If so, exit the loop as no BPs found... ; Has our count reached 0? If so, exit the loop as no BPs found...
Expand All @@ -124,6 +125,7 @@
; No BPs found ; No BPs found
; 0 indicates no BPs - see comment above ; 0 indicates no BPs - see comment above
; .MaxBPId = 0 ; .MaxBPId = 0
Mov DWORD [DebugStub_Var_MaxBPId], 0x0


; Continue: ; Continue:
; Exit: ; Exit:
Expand Down Expand Up @@ -219,7 +221,7 @@
; EAX = .CallerEIP ; EAX = .CallerEIP
Mov EAX, DWORD [DebugStub_Var_CallerEIP] Mov EAX, DWORD [DebugStub_Var_CallerEIP]
; EDI = @.DebugBPs ; EDI = @.DebugBPs
Mov EDI, DebugStub_Const_DebugBPs Mov EDI, DebugStub_Var_DebugBPs
; ECX = .MaxBPId ; ECX = .MaxBPId
Mov ECX, DWORD [DebugStub_Var_MaxBPId] Mov ECX, DWORD [DebugStub_Var_MaxBPId]
; //! repne scasd ; //! repne scasd
Expand Down Expand Up @@ -293,9 +295,12 @@
; Reset request in case we are currently responding to one or we hit a fixed breakpoint ; Reset request in case we are currently responding to one or we hit a fixed breakpoint
; before our request could be serviced (if one existed) ; before our request could be serviced (if one existed)
; .DebugBreakOnNextTrace = #StepTrigger_None ; .DebugBreakOnNextTrace = #StepTrigger_None
Mov [DebugStub_Var_DebugBreakOnNextTrace], DebugStub_Const_StepTrigger_None
; .BreakEBP = 0 ; .BreakEBP = 0
Mov DWORD [DebugStub_Var_BreakEBP], 0x0
; Set break status ; Set break status
; .DebugStatus = #Status_Break ; .DebugStatus = #Status_Break
Mov [DebugStub_Var_DebugStatus], DebugStub_Const_Status_Break
; SendTrace() ; SendTrace()


; Wait for a command ; Wait for a command
Expand All @@ -321,24 +326,30 @@


; if AL = #Vs2Ds_StepInto { ; if AL = #Vs2Ds_StepInto {
; .DebugBreakOnNextTrace = #StepTrigger_Into ; .DebugBreakOnNextTrace = #StepTrigger_Into
Mov [DebugStub_Var_DebugBreakOnNextTrace], DebugStub_Const_StepTrigger_Into
; Not used, but set for consistency ; Not used, but set for consistency
; .BreakEBP = EAX ; .BreakEBP = EAX
Mov DWORD [DebugStub_Var_BreakEBP], EAX
; goto Done ; goto Done
; } ; }


; if AL = #Vs2Ds_StepOver { ; if AL = #Vs2Ds_StepOver {
; .DebugBreakOnNextTrace = #StepTrigger_Over ; .DebugBreakOnNextTrace = #StepTrigger_Over
Mov [DebugStub_Var_DebugBreakOnNextTrace], DebugStub_Const_StepTrigger_Over
; EAX = .CallerEBP ; EAX = .CallerEBP
Mov EAX, DWORD [DebugStub_Var_CallerEBP] Mov EAX, DWORD [DebugStub_Var_CallerEBP]
; .BreakEBP = EAX ; .BreakEBP = EAX
Mov DWORD [DebugStub_Var_BreakEBP], EAX
; goto Done ; goto Done
; } ; }


; if AL = #Vs2Ds_StepOut { ; if AL = #Vs2Ds_StepOut {
; .DebugBreakOnNextTrace = #StepTrigger_Out ; .DebugBreakOnNextTrace = #StepTrigger_Out
Mov [DebugStub_Var_DebugBreakOnNextTrace], DebugStub_Const_StepTrigger_Out
; EAX = .CallerEBP ; EAX = .CallerEBP
Mov EAX, DWORD [DebugStub_Var_CallerEBP] Mov EAX, DWORD [DebugStub_Var_CallerEBP]
; .BreakEBP = EAX ; .BreakEBP = EAX
Mov DWORD [DebugStub_Var_BreakEBP], EAX
; goto Done ; goto Done
; } ; }


Expand All @@ -348,5 +359,6 @@
; Done: ; Done:
; AckCommand() ; AckCommand()
; .DebugStatus = #Status_Run ; .DebugStatus = #Status_Run
Mov [DebugStub_Var_DebugStatus], DebugStub_Const_Status_Run
; } ; }


2 changes: 1 addition & 1 deletion playground/Screen.asm
Expand Up @@ -30,7 +30,7 @@


; function DisplayWaitMsg { ; function DisplayWaitMsg {
; ESI = @.DebugWaitMsg ; ESI = @.DebugWaitMsg
Mov ESI, DebugStub_Const_DebugWaitMsg Mov ESI, DebugStub_Var_DebugWaitMsg


; EDI = #VidBase ; EDI = #VidBase
Mov EDI, DebugStub_Const_VidBase Mov EDI, DebugStub_Const_VidBase
Expand Down
10 changes: 8 additions & 2 deletions playground/Test.asm
@@ -1,4 +1,10 @@
; namespace DebugStub ; namespace DebugStub


; [AX + 12] = EAX ; .v1 = 1
Mov DWORD [AX + 12], EAX Mov DWORD [DebugStub_Var_v1], 0x1
; .v1 = AL
Mov BYTE [DebugStub_Var_v1], AL
; .v1 = EAX
Mov DWORD [DebugStub_Var_v1], EAX
; .v1 = #const
Mov [DebugStub_Var_v1], DebugStub_Const_const
5 changes: 4 additions & 1 deletion playground/Test.xs
@@ -1,3 +1,6 @@
namespace DebugStub namespace DebugStub


[AX + 12] = EAX .v1 = 1
.v1 = AL
.v1 = EAX
.v1 = #const
4 changes: 4 additions & 0 deletions playground/TracerEntry.asm
Expand Up @@ -29,7 +29,9 @@ cli
PushAD PushAD
; Save current ESP so we can look at the results of PushAll later ; Save current ESP so we can look at the results of PushAll later
; .PushAllPtr = ESP ; .PushAllPtr = ESP
Mov DWORD [DebugStub_Var_PushAllPtr], ESP
; .CallerEBP = EBP ; .CallerEBP = EBP
Mov DWORD [DebugStub_Var_CallerEBP], EBP


; Get current ESP and add 32. This will skip over the PushAll and point ; Get current ESP and add 32. This will skip over the PushAll and point
; us at the call data from Int3. ; us at the call data from Int3.
Expand All @@ -43,6 +45,7 @@ Mov EAX, DWORD [EBP]
; 12 bytes for EFLAGS, CS, EIP ; 12 bytes for EFLAGS, CS, EIP
; EBP += 12 ; EBP += 12
; .CallerESP = EBP ; .CallerESP = EBP
Mov DWORD [DebugStub_Var_CallerESP], EBP


; EIP is pointer to op after our call. Int3 is 1 byte so we subtract 1. ; EIP is pointer to op after our call. Int3 is 1 byte so we subtract 1.
; Note - when we used call it was 5 (size of our call + address) ; Note - when we used call it was 5 (size of our call + address)
Expand All @@ -64,6 +67,7 @@ Mov EAX, EBX


; Store it for later use. ; Store it for later use.
; .CallerEIP = EAX ; .CallerEIP = EAX
Mov DWORD [DebugStub_Var_CallerEIP], EAX


; Executing() ; Executing()


Expand Down
1 change: 1 addition & 0 deletions playground/Utilities.asm
Expand Up @@ -15,6 +15,7 @@
; EAX = [ESP] ; EAX = [ESP]
Mov EAX, DWORD [ESP] Mov EAX, DWORD [ESP]
; .CallerEIP = EAX ; .CallerEIP = EAX
Mov DWORD [DebugStub_Var_CallerEIP], EAX
; SendStackCorruptionOccurred() ; SendStackCorruptionOccurred()
; halt: ; halt:
; goto halt ; goto halt
Expand Down
5 changes: 5 additions & 0 deletions source/XSharp.x86/Assemblers/NASM.cs
Expand Up @@ -52,6 +52,11 @@ public NASM(TextWriter aOut)
Add(OpCode.Mov, "{0} {1}, {2}", typeof(Size), typeof(RegisterAddress), typeof(Reg32)); Add(OpCode.Mov, "{0} {1}, {2}", typeof(Size), typeof(RegisterAddress), typeof(Reg32));
Add(OpCode.Mov, "{0} {1}, {2}", typeof(Size), typeof(RegisterAddress), typeof(Identifier)); Add(OpCode.Mov, "{0} {1}, {2}", typeof(Size), typeof(RegisterAddress), typeof(Identifier));
Add(OpCode.Mov, "{0} {1}, 0x{2:X}", typeof(Size), typeof(RegisterAddress), typeof(i32u)); Add(OpCode.Mov, "{0} {1}, 0x{2:X}", typeof(Size), typeof(RegisterAddress), typeof(i32u));
Add(OpCode.Mov, "{0} {1}, 0x{2:X}", typeof(Size), typeof(MemoryAddress), typeof(i32u));
Add(OpCode.Mov, "{0} {1}, {2}", typeof(Size), typeof(MemoryAddress), typeof(Reg08));
Add(OpCode.Mov, "{0} {1}, {2}", typeof(Size), typeof(MemoryAddress), typeof(Reg16));
Add(OpCode.Mov, "{0} {1}, {2}", typeof(Size), typeof(MemoryAddress), typeof(Reg32));
Add(OpCode.Mov, "{0}, {1}", typeof(MemoryAddress), typeof(Identifier));
Add(OpCode.Mov, "{0}, {1}", typeof(Reg08), typeof(Identifier)); Add(OpCode.Mov, "{0}, {1}", typeof(Reg08), typeof(Identifier));
Add(OpCode.Mov, "{0}, {1}", typeof(Reg16), typeof(Identifier)); Add(OpCode.Mov, "{0}, {1}", typeof(Reg16), typeof(Identifier));
Add(OpCode.Mov, "{0}, {1}", typeof(Reg32), typeof(Identifier)); Add(OpCode.Mov, "{0}, {1}", typeof(Reg32), typeof(Identifier));
Expand Down
6 changes: 0 additions & 6 deletions source/XSharp/Emitters/AllEmitters.cs
Expand Up @@ -18,13 +18,7 @@ public AllEmitters(Compiler aCompiler, x86.Assemblers.Assembler aAsm) : base(aCo


// =============================================================== // ===============================================================


[Emitter(typeof(Variable), typeof(OpEquals), typeof(Int08u))]
[Emitter(typeof(Variable), typeof(OpEquals), typeof(Int16u))]
[Emitter(typeof(Variable), typeof(OpEquals), typeof(Int32u))]
[Emitter(typeof(Variable), typeof(OpEquals), typeof(Variable))] [Emitter(typeof(Variable), typeof(OpEquals), typeof(Variable))]
[Emitter(typeof(Variable), typeof(OpEquals), typeof(VariableAddress))]
[Emitter(typeof(Variable), typeof(OpEquals), typeof(Const))]
[Emitter(typeof(Variable), typeof(OpEquals), typeof(Reg))]
protected void VariableAssignment(object aVariableName, string aOpEquals, object aValue) protected void VariableAssignment(object aVariableName, string aOpEquals, object aValue)
{ {
} }
Expand Down
33 changes: 29 additions & 4 deletions source/XSharp/Emitters/Assignments.cs
@@ -1,5 +1,4 @@
using System; using Spruce.Attribs;
using Spruce.Attribs;
using Spruce.Tokens; using Spruce.Tokens;
using XSharp.Tokens; using XSharp.Tokens;
using XSharp.x86; using XSharp.x86;
Expand All @@ -8,7 +7,6 @@
using Reg08 = XSharp.Tokens.Reg08; using Reg08 = XSharp.Tokens.Reg08;
using Reg16 = XSharp.Tokens.Reg16; using Reg16 = XSharp.Tokens.Reg16;
using Reg32 = XSharp.Tokens.Reg32; using Reg32 = XSharp.Tokens.Reg32;
using Size = XSharp.x86.Params.Size;


namespace XSharp.Emitters namespace XSharp.Emitters
{ {
Expand Down Expand Up @@ -86,7 +84,7 @@ protected void RegAssigVar(Register aReg, string aEquals, Address aVal)
[Emitter(typeof(Reg), typeof(OpEquals), typeof(VariableAddress))] [Emitter(typeof(Reg), typeof(OpEquals), typeof(VariableAddress))]
protected void RegAssignVarAddr(Register aReg, string aEquals, string aVal) protected void RegAssignVarAddr(Register aReg, string aEquals, string aVal)
{ {
Asm.Emit(OpCode.Mov, aReg, $"{Compiler.GetPrefixForConst}{aVal}"); Asm.Emit(OpCode.Mov, aReg, $"{Compiler.GetPrefixForVar}{aVal}");
} }


// ESI = [EBP-1] => mov ESI, dword [EBP - 1] // ESI = [EBP-1] => mov ESI, dword [EBP - 1]
Expand All @@ -112,5 +110,32 @@ protected void RegAssignToMemory(string aOpOpenBracket, Register aTargetRegister
{ {
Asm.Emit(OpCode.Mov, source.RegSize, new Address(aTargetRegisterRoot, aOffset, aOpOperator == "-"), source); Asm.Emit(OpCode.Mov, source.RegSize, new Address(aTargetRegisterRoot, aOffset, aOpOperator == "-"), source);
} }

// .v1 = 1 => mov dword [v1], 0x1
[Emitter(typeof(Variable), typeof(OpEquals), typeof(Int32u))]
[Emitter(typeof(Variable), typeof(OpEquals), typeof(Reg))]
[Emitter(typeof(Variable), typeof(OpEquals), typeof(Const))]
protected void VariableAssignment(Address aVariableName, string aOpEquals, object aValue)
{
string size;
switch (aValue)
{
case uint _:
size = "dword";
Asm.Emit(OpCode.Mov, size, aVariableName.AddPrefix(Compiler.GetPrefixForVar), aValue);
break;

case Register aValueReg:
size = aValueReg.RegSize;
Asm.Emit(OpCode.Mov, size, aVariableName.AddPrefix(Compiler.GetPrefixForVar), aValue);
break;

case string _:
//TODO: verify this
aValue = $"{Compiler.GetPrefixForConst}{aValue}";
Asm.Emit(OpCode.Mov, aVariableName.AddPrefix(Compiler.GetPrefixForVar), aValue);
break;
}
}
} }
} }
3 changes: 3 additions & 0 deletions source/XSharp/ToDo.txt
Expand Up @@ -20,6 +20,9 @@ IfPredicate - { and return - goto has value. Can compound front intead wtih comp
-Live input to pl0ay and graph path -Live input to pl0ay and graph path
-Generate attrib signature, and also optionally empty method with args -Generate attrib signature, and also optionally empty method with args


-Assignments.cs
- Check generation of var = const.

Kudzu ---------------------------------------------------- Kudzu ----------------------------------------------------
*** PrivateAssets to G3 *** PrivateAssets to G3
Enrique 500 Enrique 500
Expand Down

0 comments on commit e777b36

Please sign in to comment.