agarmgas.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. This unit implements an asm for the ARM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { This unit implements the GNU Assembler writer for the ARM
  18. }
  19. unit agarmgas;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,
  24. aasmtai,aasmdata,
  25. aggas,
  26. cpubase;
  27. type
  28. TARMGNUAssembler=class(TGNUassembler)
  29. constructor create(smart: boolean); override;
  30. function MakeCmdLine: TCmdStr; override;
  31. procedure WriteExtraHeader; override;
  32. end;
  33. TArmInstrWriter=class(TCPUInstrWriter)
  34. procedure WriteInstruction(hp : tai);override;
  35. end;
  36. TArmAppleGNUAssembler=class(TAppleGNUassembler)
  37. constructor create(smart: boolean); override;
  38. end;
  39. const
  40. gas_shiftmode2str : array[tshiftmode] of string[3] = (
  41. '','lsl','lsr','asr','ror','rrx');
  42. implementation
  43. uses
  44. cutils,globals,verbose,
  45. systems,
  46. assemble,
  47. cpuinfo,aasmcpu,
  48. itcpugas,
  49. cgbase,cgutils;
  50. {****************************************************************************}
  51. { GNU Arm Assembler writer }
  52. {****************************************************************************}
  53. constructor TArmGNUAssembler.create(smart: boolean);
  54. begin
  55. inherited create(smart);
  56. InstrWriter := TArmInstrWriter.create(self);
  57. end;
  58. function TArmGNUAssembler.MakeCmdLine: TCmdStr;
  59. begin
  60. result:=inherited MakeCmdLine;
  61. if (current_settings.fputype = fpu_soft) then
  62. result:='-mfpu=softvfp '+result;
  63. if current_settings.cputype = cpu_cortexm3 then
  64. result:='-mcpu=cortex-m3 -mthumb -mthumb-interwork '+result;
  65. if current_settings.cputype = cpu_armv7m then
  66. result:='-march=armv7m -mthumb -mthumb-interwork '+result;
  67. end;
  68. procedure TArmGNUAssembler.WriteExtraHeader;
  69. begin
  70. inherited WriteExtraHeader;
  71. if current_settings.cputype in cpu_thumb2 then
  72. AsmWriteLn(#9'.syntax unified');
  73. end;
  74. {****************************************************************************}
  75. { GNU/Apple PPC Assembler writer }
  76. {****************************************************************************}
  77. constructor TArmAppleGNUAssembler.create(smart: boolean);
  78. begin
  79. inherited create(smart);
  80. InstrWriter := TArmInstrWriter.create(self);
  81. end;
  82. {****************************************************************************}
  83. { Helper routines for Instruction Writer }
  84. {****************************************************************************}
  85. function getreferencestring(var ref : treference) : string;
  86. var
  87. s : string;
  88. begin
  89. with ref do
  90. begin
  91. {$ifdef extdebug}
  92. // if base=NR_NO then
  93. // internalerror(200308292);
  94. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  95. // internalerror(200308293);
  96. {$endif extdebug}
  97. if assigned(symbol) then
  98. begin
  99. if (base<>NR_NO) and not(is_pc(base)) then
  100. internalerror(200309011);
  101. s:=symbol.name;
  102. if offset<0 then
  103. s:=s+tostr(offset)
  104. else if offset>0 then
  105. s:=s+'+'+tostr(offset);
  106. end
  107. else
  108. begin
  109. s:='['+gas_regname(base);
  110. if addressmode=AM_POSTINDEXED then
  111. s:=s+']';
  112. if index<>NR_NO then
  113. begin
  114. if signindex<0 then
  115. s:=s+', -'
  116. else
  117. s:=s+', ';
  118. s:=s+gas_regname(index);
  119. if shiftmode<>SM_None then
  120. s:=s+', '+gas_shiftmode2str[shiftmode]+' #'+tostr(shiftimm);
  121. end
  122. else if offset<>0 then
  123. s:=s+', #'+tostr(offset);
  124. case addressmode of
  125. AM_OFFSET:
  126. s:=s+']';
  127. AM_PREINDEXED:
  128. s:=s+']!';
  129. end;
  130. end;
  131. end;
  132. getreferencestring:=s;
  133. end;
  134. const
  135. shiftmode2str: array[tshiftmode] of string[3] = ('','lsl','lsr','asr','ror','rrx');
  136. function getopstr(const o:toper) : string;
  137. var
  138. hs : string;
  139. first : boolean;
  140. r : tsuperregister;
  141. begin
  142. case o.typ of
  143. top_reg:
  144. getopstr:=gas_regname(o.reg);
  145. top_shifterop:
  146. begin
  147. if (o.shifterop^.rs<>NR_NO) and (o.shifterop^.shiftimm=0) then
  148. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' '+gas_regname(o.shifterop^.rs)
  149. else if (o.shifterop^.rs=NR_NO) then
  150. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' #'+tostr(o.shifterop^.shiftimm)
  151. else internalerror(200308282);
  152. end;
  153. top_const:
  154. getopstr:='#'+tostr(longint(o.val));
  155. top_regset:
  156. begin
  157. getopstr:='{';
  158. first:=true;
  159. for r:=RS_R0 to RS_R15 do
  160. if r in o.regset^ then
  161. begin
  162. if not(first) then
  163. getopstr:=getopstr+',';
  164. getopstr:=getopstr+gas_regname(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  165. first:=false;
  166. end;
  167. getopstr:=getopstr+'}';
  168. end;
  169. top_conditioncode:
  170. getopstr:=cond2str[o.cc];
  171. top_ref:
  172. if o.ref^.refaddr=addr_full then
  173. begin
  174. hs:=o.ref^.symbol.name;
  175. if o.ref^.offset>0 then
  176. hs:=hs+'+'+tostr(o.ref^.offset)
  177. else
  178. if o.ref^.offset<0 then
  179. hs:=hs+tostr(o.ref^.offset);
  180. getopstr:=hs;
  181. end
  182. else
  183. getopstr:=getreferencestring(o.ref^);
  184. else
  185. internalerror(2002070604);
  186. end;
  187. end;
  188. Procedure TArmInstrWriter.WriteInstruction(hp : tai);
  189. var op: TAsmOp;
  190. s: string;
  191. i: byte;
  192. sep: string[3];
  193. begin
  194. op:=taicpu(hp).opcode;
  195. if current_settings.cputype in cpu_thumb2 then
  196. begin
  197. if taicpu(hp).ops = 0 then
  198. s:=#9+gas_op2str[op]+' '+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix]
  199. else
  200. s:=#9+gas_op2str[op]+oppostfix2str[taicpu(hp).oppostfix]+cond2str[taicpu(hp).condition]; // Conditional infixes are deprecated in unified syntax
  201. end
  202. else
  203. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix];
  204. if taicpu(hp).ops<>0 then
  205. begin
  206. sep:=#9;
  207. for i:=0 to taicpu(hp).ops-1 do
  208. begin
  209. // debug code
  210. // writeln(s);
  211. // writeln(taicpu(hp).fileinfo.line);
  212. { LDM and STM use references as first operand but they are written like a register }
  213. if (i=0) and (op in [A_LDM,A_STM]) then
  214. begin
  215. case taicpu(hp).oper[0]^.typ of
  216. top_ref:
  217. begin
  218. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.ref^.index);
  219. if taicpu(hp).oper[0]^.ref^.addressmode=AM_PREINDEXED then
  220. s:=s+'!';
  221. end;
  222. top_reg:
  223. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.reg);
  224. else
  225. internalerror(200311292);
  226. end;
  227. end
  228. { register count of SFM and LFM is written without # }
  229. else if (i=1) and (op in [A_SFM,A_LFM]) then
  230. begin
  231. case taicpu(hp).oper[1]^.typ of
  232. top_const:
  233. s:=s+sep+tostr(taicpu(hp).oper[1]^.val);
  234. else
  235. internalerror(200311292);
  236. end;
  237. end
  238. else
  239. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  240. sep:=',';
  241. end;
  242. end;
  243. owner.AsmWriteLn(s);
  244. end;
  245. const
  246. as_arm_gas_info : tasminfo =
  247. (
  248. id : as_gas;
  249. idtxt : 'AS';
  250. asmbin : 'as';
  251. asmcmd : '-o $OBJ $ASM';
  252. supported_targets : [system_arm_linux,system_arm_wince,system_arm_gba,system_arm_palmos,system_arm_nds,system_arm_embedded,system_arm_symbian];
  253. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  254. labelprefix : '.L';
  255. comment : '# ';
  256. );
  257. as_arm_gas_darwin_info : tasminfo =
  258. (
  259. id : as_darwin;
  260. idtxt : 'AS-Darwin';
  261. asmbin : 'as';
  262. asmcmd : '-o $OBJ $ASM -arch arm';
  263. supported_targets : [system_arm_darwin];
  264. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  265. labelprefix : 'L';
  266. comment : '# ';
  267. );
  268. begin
  269. RegisterAssembler(as_arm_gas_info,TARMGNUAssembler);
  270. RegisterAssembler(as_arm_gas_darwin_info,TArmAppleGNUAssembler);
  271. end.