/util/util.pas
unit Util;
interface

uses Opcode;

function ToUpCase(s: string): string;
function ChangeExtension(const instr: string; const newext: string): string;

procedure DumpOperand(const o: Operand);

implementation

function ToUpCase(s: string): string;
var i: Byte;
begin
    for i := 1 to Length(s) do
        s[i] := UpCase(s[i]);
    ToUpCase := s;
end;

function ChangeExtension(const instr: string; const newext: string): string;
var i, j: Integer;
    outstr: string;
begin
    outstr := instr;
    i := Length(outstr);
    while i >= 0 do begin
        if outstr[i] = Char($2E) then break;
        i := i - 1;
    end;
    if i = -1 then begin
        i := Length(outstr) + 1;
        outstr[i] := '.';
    end;
    for j := 0 to Length(newext) - 1 do
        outstr[i + j + 1] := newext[j + 1];
    outstr[0] := Char(i + j + 1);

    ChangeExtension := outstr;
end;

procedure DumpReg(const r: EncRRR);
begin
    Write('reg ');
    case r of
        rGA: Write('GA');
        rGB: Write('GB');
        rGC: Write('GC');
        rBC: Write('BC');
        rTP: Write('TP');
        rIX: Write('IX');
        rCC: Write('CC');
        rMC: Write('MC');
    end;
end;

procedure DumpPtrReg(const r: EncPPP);
begin
    Write('ptrreg ');
    case r of
        pGA: Write('GA');
        pGB: Write('GB');
        pGC: Write('GC');
        pReserved: Write('Reserved');
        pTP: Write('TP');
    end;
end;

procedure DumpMemBase(const m: EncMM);
begin
    Write('mem ');
    case m of 
        mGA: Write('GA');
        mGB: Write('GB');
        mGC: Write('GC');
        mPP: Write('PP');
    end;
end;

procedure DumpOperand(const o: Operand);
begin
    case o.Kind of
        OpUnknown: Write('!Unknown!');
        OpRegister: DumpReg(o.reg);
        OpPointer: DumpPtrReg(o.preg);
        OpImmediate: Write('immed ', o.immed);
        OpLocation: Write('loc ', o.loc);
        OpMemory: begin
            DumpMemBase(o.mm);
            case o.aa of
                BaseOffset: Write(' + ', o.offset);
                BaseIndex: Write(' + IX');
                BaseIndexIncrement: Write(' + IX+');
            end;
        end;
        OpBit: Write('bit ', Byte(o.bit));
        OpAbsent: Write('absent');
    end;
end;

end.