Skip to content

Commit

Permalink
First stage of compiler refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
mterwoord committed Jun 14, 2016
1 parent d144745 commit ebb1eae
Show file tree
Hide file tree
Showing 94 changed files with 1,356 additions and 1,407 deletions.
4 changes: 2 additions & 2 deletions Users/Matthijs/DebugCompiler/MyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public void Test(Type kernelToRun)

var xEngine = new Engine();
// Sets the time before an error is registered. For example if set to 60 then if a kernel runs for more than 60 seconds then
// that kernel will be marked as a failiure and terminated
xEngine.AllowedSecondsInKernel = 30;
// that kernel will be marked as a failure and terminated
xEngine.AllowedSecondsInKernel = 600;

// If you want to test only specific platforms, add them to the list, like next line. By default, all platforms are run.
xEngine.RunTargets.Add(RunTargetEnum.Bochs);
Expand Down
383 changes: 166 additions & 217 deletions source/Cosmos.Assembler/x86/Registers.cs

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions source/Cosmos.Core.Plugs/Assemblers/UpdateIDT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object a
continue;
}

new CPUx86.Mov { DestinationReg = CPUx86.Registers.EAX, SourceRef = CPUAll.ElementReference.New("__ISR_Handler_" + i.ToString("X2")) };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EAX, SourceRef = CPUAll.ElementReference.New("__ISR_Handler_" + i.ToString("X2")) };
new CPUx86.Mov
{
DestinationRef = CPUAll.ElementReference.New("_NATIVE_IDT_Contents"),
DestinationIsIndirect = true,
DestinationDisplacement = ((i * 8) + 0),
SourceReg = CPUx86.Registers.AL
SourceReg = CPUx86.RegistersEnum.AL
};
new CPUx86.Mov
{
DestinationRef = CPUAll.ElementReference.New("_NATIVE_IDT_Contents"),
DestinationIsIndirect = true,
DestinationDisplacement = ((i * 8) + 1),
SourceReg = CPUx86.Registers.AH
SourceReg = CPUx86.RegistersEnum.AH
};
new CPUx86.Mov
{
Expand All @@ -87,20 +87,20 @@ public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object a
SourceValue = 0x8E,
Size = 8
};
new CPUx86.ShiftRight { DestinationReg = CPUx86.Registers.EAX, SourceValue = 16 };
new CPUx86.ShiftRight { DestinationReg = CPUx86.RegistersEnum.EAX, SourceValue = 16 };
new CPUx86.Mov
{
DestinationRef = CPUAll.ElementReference.New("_NATIVE_IDT_Contents"),
DestinationIsIndirect = true,
DestinationDisplacement = ((i * 8) + 6),
SourceReg = CPUx86.Registers.AL
SourceReg = CPUx86.RegistersEnum.AL
};
new CPUx86.Mov
{
DestinationRef = CPUAll.ElementReference.New("_NATIVE_IDT_Contents"),
DestinationIsIndirect = true,
DestinationDisplacement = ((i * 8) + 7),
SourceReg = CPUx86.Registers.AH
SourceReg = CPUx86.RegistersEnum.AH
};
}

Expand All @@ -118,17 +118,17 @@ public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object a
new CPUx86.Push { DestinationValue = (uint)j };
new CPUx86.Pushad();

new CPUx86.Sub { DestinationReg = CPUx86.Registers.ESP, SourceValue = 4 };
XS.Set(XSRegisters.OldToNewRegister(CPUx86.Registers.EAX), XSRegisters.OldToNewRegister(CPUx86.Registers.ESP)); // preserve old stack address for passing to interrupt handler
new CPUx86.Sub { DestinationReg = CPUx86.RegistersEnum.ESP, SourceValue = 4 };
XS.Set(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.EAX), XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.ESP)); // preserve old stack address for passing to interrupt handler

// store floating point data
new CPUx86.And { DestinationReg = CPUx86.Registers.ESP, SourceValue = 0xfffffff0 }; // fxsave needs to be 16-byte alligned
new CPUx86.Sub { DestinationReg = CPUx86.Registers.ESP, SourceValue = 512 }; // fxsave needs 512 bytes
new FXSave { DestinationReg = CPUx86.Registers.ESP, DestinationIsIndirect = true }; // save the registers
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EAX, DestinationIsIndirect = true, SourceReg = CPUx86.Registers.ESP };
new CPUx86.And { DestinationReg = CPUx86.RegistersEnum.ESP, SourceValue = 0xfffffff0 }; // fxsave needs to be 16-byte alligned
new CPUx86.Sub { DestinationReg = CPUx86.RegistersEnum.ESP, SourceValue = 512 }; // fxsave needs 512 bytes
new FXSave { DestinationReg = CPUx86.RegistersEnum.ESP, DestinationIsIndirect = true }; // save the registers
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EAX, DestinationIsIndirect = true, SourceReg = CPUx86.RegistersEnum.ESP };

new CPUx86.Push { DestinationReg = CPUx86.Registers.EAX }; //
new CPUx86.Push { DestinationReg = CPUx86.Registers.EAX }; // pass old stack address (pointer to InterruptContext struct) to the interrupt handler
new CPUx86.Push { DestinationReg = CPUx86.RegistersEnum.EAX }; //
new CPUx86.Push { DestinationReg = CPUx86.RegistersEnum.EAX }; // pass old stack address (pointer to InterruptContext struct) to the interrupt handler
//new CPUx86.Move("eax",
// "esp");
//new CPUx86.Push("eax");
Expand All @@ -140,30 +140,30 @@ public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object a
xHandler = GetMethodDef(typeof(INTs).Assembly, typeof(INTs).FullName, "HandleInterrupt_Default", true);
}
new CPUx86.Call { DestinationLabel = CPUAll.LabelName.Get(xHandler) };
new CPUx86.Pop { DestinationReg = CPUx86.Registers.EAX };
new FXStore { DestinationReg = CPUx86.Registers.ESP, DestinationIsIndirect = true };
new CPUx86.Pop { DestinationReg = CPUx86.RegistersEnum.EAX };
new FXStore { DestinationReg = CPUx86.RegistersEnum.ESP, DestinationIsIndirect = true };

XS.Set(XSRegisters.OldToNewRegister(CPUx86.Registers.ESP), XSRegisters.OldToNewRegister(CPUx86.Registers.EAX)); // this restores the stack for the FX stuff, except the pointer to the FX data
new CPUx86.Add { DestinationReg = CPUx86.Registers.ESP, SourceValue = 4 }; // "pop" the pointer
XS.Set(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.ESP), XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.EAX)); // this restores the stack for the FX stuff, except the pointer to the FX data
new CPUx86.Add { DestinationReg = CPUx86.RegistersEnum.ESP, SourceValue = 4 }; // "pop" the pointer

new CPUx86.Popad();

new CPUx86.Add { DestinationReg = CPUx86.Registers.ESP, SourceValue = 8 };
new CPUx86.Add { DestinationReg = CPUx86.RegistersEnum.ESP, SourceValue = 8 };
new CPUAll.Label("__ISR_Handler_" + j.ToString("X2") + "_END");
new CPUx86.IRET();
}
new CPUAll.Label("__INTERRUPT_OCCURRED__");
new CPUx86.Return();
new CPUAll.Label("__AFTER__ALL__ISR__HANDLER__STUBS__");
new CPUx86.Noop();
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EAX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = 8 };
new CPUx86.Compare { DestinationReg = CPUx86.Registers.EAX, SourceValue = 0 };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EAX, SourceReg = CPUx86.RegistersEnum.EBP, SourceIsIndirect = true, SourceDisplacement = 8 };
new CPUx86.Compare { DestinationReg = CPUx86.RegistersEnum.EAX, SourceValue = 0 };
new CPUx86.ConditionalJump { Condition = CPUx86.ConditionalTestEnum.Zero, DestinationLabel = ".__AFTER_ENABLE_INTERRUPTS" };

// reload interrupt list
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EAX, SourceRef = Cosmos.Assembler.ElementReference.New("_NATIVE_IDT_Pointer") };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EAX, SourceRef = Cosmos.Assembler.ElementReference.New("_NATIVE_IDT_Pointer") };
new CPUx86.Mov { DestinationRef = CPUAll.ElementReference.New("static_field__Cosmos_Core_CPU_mInterruptsEnabled"), DestinationIsIndirect = true, SourceValue = 1 };
new CPUx86.Lidt { DestinationReg = CPUx86.Registers.EAX, DestinationIsIndirect = true };
new CPUx86.Lidt { DestinationReg = CPUx86.RegistersEnum.EAX, DestinationIsIndirect = true };
// Reenable interrupts
new CPUx86.Sti();

Expand Down
22 changes: 11 additions & 11 deletions source/Cosmos.Core.Plugs/CPUImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public static void UpdateIDT(CPU aThis, bool aEnableInterruptsImmediately) {

public class GetAmountOfRAMAsm : AssemblerMethod {
public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object aMethodInfo) {
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EAX, SourceRef = CPUAll.ElementReference.New("MultiBootInfo_Memory_High"), SourceIsIndirect = true };
new CPUx86.Xor { DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EDX };
new CPUx86.Mov { DestinationReg = CPUx86.Registers.ECX, SourceValue = 1024 };
new CPUx86.Divide { DestinationReg = CPUx86.Registers.ECX };
new CPUx86.Add { DestinationReg = CPUx86.Registers.EAX, SourceValue = 1 };
new CPUx86.Push { DestinationReg = CPUx86.Registers.EAX };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EAX, SourceRef = CPUAll.ElementReference.New("MultiBootInfo_Memory_High"), SourceIsIndirect = true };
new CPUx86.Xor { DestinationReg = CPUx86.RegistersEnum.EDX, SourceReg = CPUx86.RegistersEnum.EDX };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.ECX, SourceValue = 1024 };
new CPUx86.Divide { DestinationReg = CPUx86.RegistersEnum.ECX };
new CPUx86.Add { DestinationReg = CPUx86.RegistersEnum.EAX, SourceValue = 1 };
new CPUx86.Push { DestinationReg = CPUx86.RegistersEnum.EAX };
}
}

Expand All @@ -41,15 +41,15 @@ public static uint GetEndOfKernel() {
public class ZeroFillAsm : AssemblerMethod {
public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object aMethodInfo) {
new CPUx86.ClrDirFlag();
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = 0xC }; //address
new CPUx86.Mov { DestinationReg = CPUx86.Registers.ECX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = 0x8 }; //length
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EDI, SourceReg = CPUx86.RegistersEnum.EBP, SourceIsIndirect = true, SourceDisplacement = 0xC }; //address
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.ECX, SourceReg = CPUx86.RegistersEnum.EBP, SourceIsIndirect = true, SourceDisplacement = 0x8 }; //length
// set EAX to value of fill (zero)
new CPUx86.Xor { DestinationReg = CPUx86.Registers.EAX, SourceReg = CPUx86.Registers.EAX };
new CPUx86.ShiftRight { DestinationReg = CPUx86.Registers.ECX, SourceValue = 1 };
new CPUx86.Xor { DestinationReg = CPUx86.RegistersEnum.EAX, SourceReg = CPUx86.RegistersEnum.EAX };
new CPUx86.ShiftRight { DestinationReg = CPUx86.RegistersEnum.ECX, SourceValue = 1 };
new CPUx86.ConditionalJump { Condition = CPUx86.ConditionalTestEnum.NotBelow, DestinationLabel = ".step2" };
new CPUx86.StoreByteInString();
new CPUAll.Label(".step2");
new CPUx86.ShiftRight { DestinationReg = CPUx86.Registers.ECX, SourceValue = 1 };
new CPUx86.ShiftRight { DestinationReg = CPUx86.RegistersEnum.ECX, SourceValue = 1 };
new CPUx86.ConditionalJump { Condition = CPUx86.ConditionalTestEnum.NotBelow, DestinationLabel = ".step3" };
new CPUx86.StoreWordInString();
new CPUAll.Label(".step3");
Expand Down
40 changes: 20 additions & 20 deletions source/Cosmos.Core.Plugs/IOPortImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object a
//TODO: This is a lot of work to write to a single port.
// We need to have some kind of inline ASM option that can
// emit a single out instruction
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EBP, SourceDisplacement = 0x0C, SourceIsIndirect = true };
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EAX, SourceReg = CPUx86.Registers.EBP, SourceDisplacement = 0x08, SourceIsIndirect = true };
new CPUx86.OutToDX { DestinationReg = CPUx86.Registers.AL };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EDX, SourceReg = CPUx86.RegistersEnum.EBP, SourceDisplacement = 0x0C, SourceIsIndirect = true };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EAX, SourceReg = CPUx86.RegistersEnum.EBP, SourceDisplacement = 0x08, SourceIsIndirect = true };
new CPUx86.OutToDX { DestinationReg = CPUx86.RegistersEnum.AL };
}
}
[PlugMethod(Assembler=typeof(Write8Assembler))]
Expand All @@ -34,9 +34,9 @@ private class Write16Assembler : AssemblerMethod
{
public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object aMethodInfo)
{
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = 0x0C };
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EAX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = 0x08 };
new CPUx86.OutToDX { DestinationReg = CPUx86.Registers.AX };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EDX, SourceReg = CPUx86.RegistersEnum.EBP, SourceIsIndirect = true, SourceDisplacement = 0x0C };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EAX, SourceReg = CPUx86.RegistersEnum.EBP, SourceIsIndirect = true, SourceDisplacement = 0x08 };
new CPUx86.OutToDX { DestinationReg = CPUx86.RegistersEnum.AX };
}
}
[PlugMethod(Assembler = typeof(Write16Assembler))]
Expand All @@ -48,9 +48,9 @@ private class Write32Assembler : AssemblerMethod
{
public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object aMethodInfo)
{
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = 0x0C };
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EAX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = 0x08 };
new CPUx86.OutToDX { DestinationReg = CPUx86.Registers.EAX };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EDX, SourceReg = CPUx86.RegistersEnum.EBP, SourceIsIndirect = true, SourceDisplacement = 0x0C };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EAX, SourceReg = CPUx86.RegistersEnum.EBP, SourceIsIndirect = true, SourceDisplacement = 0x08 };
new CPUx86.OutToDX { DestinationReg = CPUx86.RegistersEnum.EAX };
}
}
[PlugMethod(Assembler = typeof(Write32Assembler))]
Expand All @@ -62,12 +62,12 @@ private class Read8Assembler : AssemblerMethod
{
public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object aMethodInfo)
{
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = 0x08 };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EDX, SourceReg = CPUx86.RegistersEnum.EBP, SourceIsIndirect = true, SourceDisplacement = 0x08 };
//TODO: Do we need to clear rest of EAX first?
// MTW: technically not, as in other places, it _should_ be working with AL too..
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EAX, SourceValue = 0 };
new CPUx86.InFromDX { DestinationReg = CPUx86.Registers.AL };
new CPUx86.Push { DestinationReg = CPUx86.Registers.EAX };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EAX, SourceValue = 0 };
new CPUx86.InFromDX { DestinationReg = CPUx86.RegistersEnum.AL };
new CPUx86.Push { DestinationReg = CPUx86.RegistersEnum.EAX };
}
}
[PlugMethod(Assembler = typeof(Read8Assembler))]
Expand All @@ -79,10 +79,10 @@ private class Read16Assembler : AssemblerMethod
{
public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object aMethodInfo)
{
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = 0x08 };
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EAX, SourceValue = 0 };
new CPUx86.InFromDX { DestinationReg = CPUx86.Registers.AX };
new CPUx86.Push { DestinationReg = CPUx86.Registers.EAX };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EDX, SourceReg = CPUx86.RegistersEnum.EBP, SourceIsIndirect = true, SourceDisplacement = 0x08 };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EAX, SourceValue = 0 };
new CPUx86.InFromDX { DestinationReg = CPUx86.RegistersEnum.AX };
new CPUx86.Push { DestinationReg = CPUx86.RegistersEnum.EAX };
}
}
[PlugMethod(Assembler = typeof(Read16Assembler))]
Expand All @@ -94,9 +94,9 @@ private class Read32Assembler : AssemblerMethod
{
public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object aMethodInfo)
{
new CPUx86.Mov { DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = 0x08 };
new CPUx86.InFromDX { DestinationReg = CPUx86.Registers.EAX };
new CPUx86.Push { DestinationReg = CPUx86.Registers.EAX };
new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.EDX, SourceReg = CPUx86.RegistersEnum.EBP, SourceIsIndirect = true, SourceDisplacement = 0x08 };
new CPUx86.InFromDX { DestinationReg = CPUx86.RegistersEnum.EAX };
new CPUx86.Push { DestinationReg = CPUx86.RegistersEnum.EAX };
}
}
[PlugMethod(Assembler = typeof(Read32Assembler))]
Expand Down
Loading

0 comments on commit ebb1eae

Please sign in to comment.