123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- {
- Copyright (c) 1999-2009 by Florian Klaempfl and David Zhang
- This unit implements an asmoutput class for MIPS assembly syntax
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit cpugas;
- {$i fpcdefs.inc}
- interface
- uses
- cpubase, aasmbase, globtype, systems,
- aasmtai, aasmcpu, assemble, aggas;
- type
- TMIPSGNUAssembler = class(TGNUassembler)
- nomacro, noreorder, noat : boolean;
- constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
- {# Constructs the command line for calling the assembler }
- function MakeCmdLine: TCmdStr; override;
- end;
- TMIPSInstrWriter = class(TCPUInstrWriter)
- procedure WriteInstruction(hp : tai);override;
- end;
- const
- use_std_regnames : boolean =
- {$ifndef USE_MIPS_GAS_REGS}
- true;
- {$else}
- false;
- {$endif}
- implementation
- uses
- cutils, cpuinfo,
- globals, verbose, itcpugas, cgbase, cgutils;
- function asm_regname(reg : TRegister) : string;
- begin
- if use_std_regnames then
- asm_regname:='$'+std_regname(reg)
- else
- asm_regname:=gas_regname(reg);
- end;
- {****************************************************************************}
- { GNU MIPS Assembler writer }
- {****************************************************************************}
- constructor TMIPSGNUAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
- begin
- inherited;
- InstrWriter:=TMIPSInstrWriter.create(self);
- nomacro:=false;
- noreorder:=false;
- noat:=false;
- end;
- function TMIPSGNUAssembler.MakeCmdLine: TCmdStr;
- begin
- result := Inherited MakeCmdLine;
- { ABI selection }
- Replace(result,'$ABI','-mabi='+abitypestr[mips_abi]);
- { ARCH selection }
- Replace(result,'$ARCH','-march='+lower(cputypestr[current_settings.cputype]));
- // Replace(result,'$ARCH','-march=pic32mx -mtune=pic32mx');
- end;
- {****************************************************************************}
- { Helper routines for Instruction Writer }
- {****************************************************************************}
- function GetReferenceString(var ref: TReference): string;
- var
- reg: TRegister;
- regstr: string;
- begin
- result:='';
- if assigned(ref.symbol) then
- result:=ref.symbol.name;
- if (ref.offset<0) then
- result:=result+tostr(ref.offset)
- else if (ref.offset>0) then
- begin
- if assigned(ref.symbol) then
- result:=result+'+';
- result:=result+tostr(ref.offset);
- end
- { asmreader appears to treat literal numbers as references }
- else if (ref.symbol=nil) and (ref.base=NR_NO) and (ref.index=NR_NO) then
- result:='0';
- { either base or index may be present, but not both }
- reg:=ref.base;
- if (reg=NR_NO) then
- reg:=ref.index
- else if (ref.index<>NR_NO) then
- InternalError(2013013001);
- if (reg=NR_NO) then
- regstr:=''
- else
- regstr:='('+asm_regname(reg)+')';
- case ref.refaddr of
- addr_no,
- addr_full:
- if assigned(ref.symbol) and (reg<>NR_NO) then
- InternalError(2013013002)
- else
- begin
- result:=result+regstr;
- exit;
- end;
- addr_pic:
- result:='%got('+result;
- addr_high:
- result:='%hi('+result;
- addr_low:
- result:='%lo('+result;
- addr_pic_call16:
- result:='%call16('+result;
- addr_low_pic:
- result:='%got_lo('+result;
- addr_high_pic:
- result:='%got_hi('+result;
- addr_low_call:
- result:='%call_lo('+result;
- addr_high_call:
- result:='%call_hi('+result;
- else
- InternalError(2013013003);
- end;
- result:=result+')'+regstr;
- end;
- function getopstr(const Oper: TOper): string;
- begin
- with Oper do
- case typ of
- top_reg:
- getopstr := asm_regname(reg);
- top_const:
- getopstr := tostr(longint(val));
- top_ref:
- getopstr := getreferencestring(ref^);
- else
- internalerror(10001);
- end;
- end;
- procedure TMIPSInstrWriter.WriteInstruction(hp: Tai);
- var
- Op: TAsmOp;
- s: string;
- i: integer;
- begin
- if hp.typ <> ait_instruction then
- exit;
- op := taicpu(hp).opcode;
- case op of
- A_P_SET_NOMIPS16:
- begin
- owner.writer.AsmWriteLn(#9'.set'#9'nomips16');
- end;
- A_P_MASK,
- A_P_FMASK:
- begin
- s := #9 + gas_op2str[op] + #9'0x' + hexstr(taicpu(hp).oper[0]^.val,8)+ ',' + getopstr(taicpu(hp).oper[1]^) ;
- owner.writer.AsmWriteLn(s);
- end;
- A_P_SET_MACRO:
- begin
- owner.writer.AsmWriteLn(#9'.set'#9'macro');
- TMIPSGNUAssembler(owner).nomacro:=false;
- end;
- A_P_SET_REORDER:
- begin
- owner.writer.AsmWriteLn(#9'.set'#9'reorder');
- TMIPSGNUAssembler(owner).noreorder:=false;
- end;
- A_P_SET_NOMACRO:
- begin
- owner.writer.AsmWriteLn(#9'.set'#9'nomacro');
- TMIPSGNUAssembler(owner).nomacro:=true;
- end;
- A_P_SET_NOREORDER:
- begin
- owner.writer.AsmWriteLn(#9'.set'#9'noreorder');
- TMIPSGNUAssembler(owner).noreorder:=true;
- end;
- A_P_SET_NOAT:
- begin
- owner.writer.AsmWriteln(#9'.set'#9'noat');
- TMIPSGNUAssembler(owner).noat:=true;
- end;
- A_P_SET_AT:
- begin
- owner.writer.AsmWriteln(#9'.set'#9'at');
- TMIPSGNUAssembler(owner).noat:=false;
- end;
- else
- begin
- if taicpu(hp).is_macro and TMIPSGNUAssembler(owner).nomacro then
- owner.writer.AsmWriteln(#9'.set'#9'macro');
- s := #9 + gas_op2str[op] + cond2str[taicpu(hp).condition];
- if taicpu(hp).ops > 0 then
- begin
- s := s + #9 + getopstr(taicpu(hp).oper[0]^);
- for i := 1 to taicpu(hp).ops - 1 do
- s := s + ',' + getopstr(taicpu(hp).oper[i]^);
- end;
- owner.writer.AsmWriteLn(s);
- if taicpu(hp).is_macro and TMIPSGNUAssembler(owner).nomacro then
- owner.writer.AsmWriteln(#9'.set'#9'nomacro');
- end;
- end;
- end;
- const
- {$ifdef MIPSEL}
- as_MIPSEL_as_info: tasminfo =
- (
- id: as_gas;
- idtxt: 'AS';
- asmbin: 'as';
- asmcmd: '$ABI $ARCH $NOWARN -EL $PIC -o $OBJ $EXTRAOPT $ASM';
- supported_targets: [system_mipsel_linux,system_mipsel_android,system_mipsel_embedded];
- flags: [ af_needar, af_smartlink_sections];
- labelprefix: '.L';
- comment: '# ';
- dollarsign: '$';
- );
- {$else MIPSEL}
- as_MIPSEB_as_info: tasminfo =
- (
- id: as_gas;
- idtxt: 'AS';
- asmbin: 'as';
- asmcmd: '$ABI $ARCH $NOWARN -EB $PIC -o $OBJ $EXTRAOPT $ASM';
- supported_targets: [system_mipseb_linux];
- flags: [ af_needar, af_smartlink_sections];
- labelprefix: '.L';
- comment: '# ';
- dollarsign: '$';
- );
- {$endif MIPSEL}
- begin
- {$ifdef MIPSEL}
- RegisterAssembler(as_MIPSEL_as_info, TMIPSGNUAssembler);
- {$else MIPSEL}
- RegisterAssembler(as_MIPSEB_as_info, TMIPSGNUAssembler);
- {$endif MIPSEL}
- end.
|