Skip to content

Commit

Permalink
x64 support
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaProductions committed Dec 4, 2023
1 parent 65421be commit 764a824
Show file tree
Hide file tree
Showing 20 changed files with 2,576 additions and 2,557 deletions.
31 changes: 4 additions & 27 deletions source/XSharp/XSharp/Assembler/Gen1/Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void Add(params Instruction[] aReaders)
}

// Allows to emit footers to the code and datamember sections
protected virtual void OnBeforeFlush()
protected virtual void OnBeforeFlush(TextWriter output)
{

}
Expand All @@ -188,43 +188,20 @@ public string GetIdentifier(string aPrefix)

private bool mFlushInitializationDone = false;

protected void BeforeFlush()
protected void BeforeFlush(TextWriter output)
{
if (mFlushInitializationDone)
{
return;
}
mFlushInitializationDone = true;
OnBeforeFlush();
OnBeforeFlush(output);
//MergeAllElements();
}

public virtual void FlushBinary(Stream aOutput, ulong aBaseAddress)
{
BeforeFlush();
var xMax = AllAssemblerElementCount;
var xCurrentAddresss = aBaseAddress;
for (int i = 0; i < xMax; i++)
{
GetAssemblerElement(i).UpdateAddress(this, ref xCurrentAddresss);
}
aOutput.SetLength(aOutput.Length + (long) (xCurrentAddresss - aBaseAddress));
for (int i = 0; i < xMax; i++)
{
var xItem = GetAssemblerElement(i);
if (!xItem.IsComplete(this))
{
throw new Exception("Incomplete element encountered.");
}
//var xBuff = xItem.GetData(this);
//aOutput.Write(xBuff, 0, xBuff.Length);
xItem.WriteData(this, aOutput);
}
}

public virtual void FlushText(TextWriter aOutput)
{
BeforeFlush();
BeforeFlush(aOutput);
BeforeFlushText(aOutput);
// Write out data declarations
aOutput.WriteLine();
Expand Down
136 changes: 70 additions & 66 deletions source/XSharp/XSharp/Assembler/Gen1/x86/JumpToSegment.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,70 @@
using System;

namespace XSharp.Assembler.x86
{
[XSharp.Assembler.OpCode("jmp")]
public class JumpToSegment : Instruction {
public XSharp.Assembler.ElementReference DestinationRef {
get;
set;
}

public ushort Segment {
get;
set;
}

public override void WriteText( XSharp.Assembler.Assembler aAssembler, System.IO.TextWriter aOutput )
{
if (DestinationRef != null) {
aOutput.Write("jmp ");
aOutput.Write(Segment);
aOutput.Write(":");
aOutput.Write(DestinationRef.ToString());
} else {
aOutput.Write("jmp ");
aOutput.Write(Segment);
aOutput.Write(":0x0");
}
}

public string DestinationLabel {
get {
if (DestinationRef != null) {
return DestinationRef.Name;
}
return String.Empty;
}
set {
DestinationRef = XSharp.Assembler.ElementReference.New(value);
}
}

public override bool IsComplete( XSharp.Assembler.Assembler aAssembler )
{
ulong xAddress;
return DestinationRef == null || DestinationRef.Resolve(aAssembler, out xAddress);
}

public override void UpdateAddress(XSharp.Assembler.Assembler aAssembler, ref ulong aAddress) {
base.UpdateAddress(aAssembler, ref aAddress);
aAddress += 7;
}

//public override byte[] GetData(Assembler aAssembler) {
public override void WriteData( XSharp.Assembler.Assembler aAssembler, System.IO.Stream aOutput )
{
aOutput.WriteByte(0xEA);
ulong xAddress = 0;
if (DestinationRef != null && DestinationRef.Resolve(aAssembler, out xAddress)) {
xAddress = (ulong)(((long)xAddress) + DestinationRef.Offset);
}
aOutput.Write(BitConverter.GetBytes((uint)(xAddress)), 0, 4);
aOutput.Write(BitConverter.GetBytes(Segment), 0, 2);
}
}
}
using System;

namespace XSharp.Assembler.x86
{
[XSharp.Assembler.OpCode("jmp")]
public class JumpToSegment : Instruction {
public XSharp.Assembler.ElementReference DestinationRef {
get;
set;
}

public ushort Segment {
get;
set;
}

public JumpToSegment()
{
;
}
public override void WriteText( XSharp.Assembler.Assembler aAssembler, System.IO.TextWriter aOutput )
{
if (DestinationRef != null) {
aOutput.Write("jmp ");
aOutput.Write(Segment);
aOutput.Write(":");
aOutput.Write(DestinationRef.ToString());
} else {
aOutput.Write("jmp ");
aOutput.Write(Segment);
aOutput.Write(":0x0");
}
}

public string DestinationLabel {
get {
if (DestinationRef != null) {
return DestinationRef.Name;
}
return String.Empty;
}
set {
DestinationRef = XSharp.Assembler.ElementReference.New(value);
}
}

public override bool IsComplete( XSharp.Assembler.Assembler aAssembler )
{
ulong xAddress;
return DestinationRef == null || DestinationRef.Resolve(aAssembler, out xAddress);
}

public override void UpdateAddress(XSharp.Assembler.Assembler aAssembler, ref ulong aAddress) {
base.UpdateAddress(aAssembler, ref aAddress);
aAddress += 7;
}

//public override byte[] GetData(Assembler aAssembler) {
public override void WriteData( XSharp.Assembler.Assembler aAssembler, System.IO.Stream aOutput )
{
aOutput.WriteByte(0xEA);
ulong xAddress = 0;
if (DestinationRef != null && DestinationRef.Resolve(aAssembler, out xAddress)) {
xAddress = (ulong)(((long)xAddress) + DestinationRef.Offset);
}
aOutput.Write(BitConverter.GetBytes((uint)(xAddress)), 0, 4);
aOutput.Write(BitConverter.GetBytes(Segment), 0, 2);
}
}
}
20 changes: 10 additions & 10 deletions source/XSharp/XSharp/Assembler/Gen1/x86/Push.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace XSharp.Assembler.x86
{
[XSharp.Assembler.OpCode("push")]
public class Push : InstructionWithDestinationAndSize {

public Push():base("push") {
Size = 32;
}
}
}
namespace XSharp.Assembler.x86
{
[XSharp.Assembler.OpCode("push")]
public class Push : InstructionWithDestinationAndSize {

public Push():base("push") {
Size = 64;
}
}
}
9 changes: 8 additions & 1 deletion source/XSharp/XSharp/Assembler/Gen1/x86/Registers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ public static string GetRegisterName(RegistersEnum aRegister)

public static byte GetSize(RegistersEnum aRegister) {
if (Is128Bit(aRegister)) { return 128; }
if (Is80Bit(aRegister)) { return 80; }
if (Is64Bit(aRegister)) { return 64; }
if (Is80Bit(aRegister)) { return 80; }
if (Is32Bit(aRegister)) { return 32; }
if (Is16Bit(aRegister)) { return 16; }
if (Is8Bit(aRegister)) { return 8; }
Expand Down Expand Up @@ -369,6 +370,12 @@ public static bool IsSegment(RegistersEnum aRegister)
return aRegister == RegistersEnum.CS || aRegister == RegistersEnum.DS || aRegister == RegistersEnum.ES || aRegister == RegistersEnum.FS || aRegister == RegistersEnum.GS || aRegister == RegistersEnum.SS;
}

public static bool Is64Bit(RegistersEnum aRegister)
{
return aRegister == RegistersEnum.RAX || aRegister == RegistersEnum.RBX || aRegister == RegistersEnum.RCX || aRegister == RegistersEnum.RDX || aRegister == RegistersEnum.RSP || aRegister == RegistersEnum.RBP || aRegister == RegistersEnum.RSI || aRegister == RegistersEnum.RDI || aRegister == RegistersEnum.CR0 || aRegister == RegistersEnum.CR1 || aRegister == RegistersEnum.CR2 || aRegister == RegistersEnum.CR3 || aRegister == RegistersEnum.CR4
|| aRegister == RegistersEnum.RIP || aRegister == RegistersEnum.R9 || aRegister == RegistersEnum.R10 || aRegister == RegistersEnum.R11 || aRegister == RegistersEnum.R12 || aRegister == RegistersEnum.R13 || aRegister == RegistersEnum.R14 || aRegister == RegistersEnum.R15;
}

public static bool Is32Bit(RegistersEnum aRegister)
{
return aRegister == RegistersEnum.EAX || aRegister == RegistersEnum.EBX || aRegister == RegistersEnum.ECX || aRegister == RegistersEnum.EDX || aRegister == RegistersEnum.ESP || aRegister == RegistersEnum.EBP || aRegister == RegistersEnum.ESI || aRegister == RegistersEnum.EDI || aRegister == RegistersEnum.CR0 || aRegister == RegistersEnum.CR1 || aRegister == RegistersEnum.CR2 || aRegister == RegistersEnum.CR3 || aRegister == RegistersEnum.CR4
Expand Down
18 changes: 11 additions & 7 deletions source/XSharp/XSharp/Assembler/Gen1/x86/SSE2/ConvertSI2SD.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace XSharp.Assembler.x86.SSE
{
[XSharp.Assembler.OpCode("cvtsi2sd")]
public class ConvertSI2SD : InstructionWithDestinationAndSource
{
}
}
namespace XSharp.Assembler.x86.SSE
{
[XSharp.Assembler.OpCode("cvtsi2sd")]
public class ConvertSI2SD : InstructionWithDestinationAndSource
{
public ConvertSI2SD()
{
SourceRequiresSize = true;
}
}
}
51 changes: 27 additions & 24 deletions source/XSharp/XSharp/Assembler/Gen1/x86/SignExtendAX.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
using System;

namespace XSharp.Assembler.x86
{
[XSharp.Assembler.OpCode("cdq")]
public class SignExtendAX : InstructionWithSize {
public override void WriteText( XSharp.Assembler.Assembler aAssembler, System.IO.TextWriter aOutput )
{
switch (Size) {
case 32:
aOutput.Write("cdq");
return;
case 16:
aOutput.Write("cwde");
return;
case 8:
aOutput.Write("cbw");
return;
default:
throw new NotSupportedException();
}
}
}
}
using System;

namespace XSharp.Assembler.x86
{
[XSharp.Assembler.OpCode("cdq")]
public class SignExtendAX : InstructionWithSize {
public override void WriteText( XSharp.Assembler.Assembler aAssembler, System.IO.TextWriter aOutput )
{
switch (Size) {
case 64:
aOutput.Write("cqo");
return;
case 32:
aOutput.Write("cdq");
return;
case 16:
aOutput.Write("cwde");
return;
case 8:
aOutput.Write("cbw");
return;
default:
throw new NotSupportedException();
}
}
}
}
Loading

0 comments on commit 764a824

Please sign in to comment.