aasmcpu.pas 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Contains the assembler object 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. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,globtype,globals,verbose,
  22. aasmbase,aasmtai,aasmdata,
  23. ogbase,
  24. symtype,
  25. cpubase,cpuinfo,cgbase,cgutils;
  26. const
  27. { "mov reg,reg" source operand number }
  28. O_MOV_SOURCE = 1;
  29. { "mov reg,reg" source operand number }
  30. O_MOV_DEST = 0;
  31. { Operand types }
  32. OT_NONE = $00000000;
  33. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  34. OT_BITS16 = $00000002;
  35. OT_BITS32 = $00000004;
  36. OT_BITS64 = $00000008; { FPU only }
  37. OT_BITS80 = $00000010;
  38. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  39. OT_NEAR = $00000040;
  40. OT_SHORT = $00000080;
  41. OT_BITSTINY = $00000100; { fpu constant }
  42. OT_BITSSHIFTER =
  43. $00000200;
  44. OT_SIZE_MASK = $000003FF; { all the size attributes }
  45. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  46. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  47. OT_TO = $00000200; { operand is followed by a colon }
  48. { reverse effect in FADD, FSUB &c }
  49. OT_COLON = $00000400;
  50. OT_SHIFTEROP = $00000800;
  51. OT_REGISTER = $00001000;
  52. OT_IMMEDIATE = $00002000;
  53. OT_REGLIST = $00008000;
  54. OT_IMM8 = $00002001;
  55. OT_IMM24 = $00002002;
  56. OT_IMM32 = $00002004;
  57. OT_IMM64 = $00002008;
  58. OT_IMM80 = $00002010;
  59. OT_IMMTINY = $00002100;
  60. OT_IMMSHIFTER= $00002200;
  61. OT_IMMEDIATE24 = OT_IMM24;
  62. OT_SHIFTIMM = OT_SHIFTEROP or OT_IMMSHIFTER;
  63. OT_SHIFTIMMEDIATE = OT_SHIFTIMM;
  64. OT_IMMEDIATESHIFTER = OT_IMMSHIFTER;
  65. OT_IMMEDIATEFPU = OT_IMMTINY;
  66. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  67. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  68. OT_REG8 = $00201001;
  69. OT_REG16 = $00201002;
  70. OT_REG32 = $00201004;
  71. OT_REG64 = $00201008;
  72. OT_VREG = $00201010; { vector register }
  73. OT_MEMORY = $00204000; { register number in 'basereg' }
  74. OT_MEM8 = $00204001;
  75. OT_MEM16 = $00204002;
  76. OT_MEM32 = $00204004;
  77. OT_MEM64 = $00204008;
  78. OT_MEM80 = $00204010;
  79. { word/byte load/store }
  80. OT_AM2 = $00010000;
  81. { misc ld/st operations }
  82. OT_AM3 = $00020000;
  83. { multiple ld/st operations }
  84. OT_AM4 = $00040000;
  85. { co proc. ld/st operations }
  86. OT_AM5 = $00080000;
  87. OT_AMMASK = $000f0000;
  88. OT_MEMORYAM2 = OT_MEMORY or OT_AM2;
  89. OT_MEMORYAM3 = OT_MEMORY or OT_AM3;
  90. OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
  91. OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
  92. OT_FPUREG = $01000000; { floating point stack registers }
  93. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  94. { a mask for the following }
  95. OT_MEM_OFFS = $00604000; { special type of EA }
  96. { simple [address] offset }
  97. OT_ONENESS = $00800000; { special type of immediate operand }
  98. { so UNITY == IMMEDIATE | ONENESS }
  99. OT_UNITY = $00802000; { for shift/rotate instructions }
  100. instabentries = {$i armnop.inc}
  101. maxinfolen = 5;
  102. IF_NONE = $00000000;
  103. IF_ARMMASK = $000F0000;
  104. IF_ARM7 = $00070000;
  105. IF_FPMASK = $00F00000;
  106. IF_FPA = $00100000;
  107. { if the instruction can change in a second pass }
  108. IF_PASS2 = longint($80000000);
  109. type
  110. TInsTabCache=array[TasmOp] of longint;
  111. PInsTabCache=^TInsTabCache;
  112. tinsentry = record
  113. opcode : tasmop;
  114. ops : byte;
  115. optypes : array[0..3] of longint;
  116. code : array[0..maxinfolen] of char;
  117. flags : longint;
  118. end;
  119. pinsentry=^tinsentry;
  120. const
  121. InsTab : array[0..instabentries-1] of TInsEntry={$i armtab.inc}
  122. var
  123. InsTabCache : PInsTabCache;
  124. type
  125. taicpu = class(tai_cpu_abstract)
  126. oppostfix : TOpPostfix;
  127. roundingmode : troundingmode;
  128. procedure loadshifterop(opidx:longint;const so:tshifterop);
  129. procedure loadregset(opidx:longint;const s:tcpuregisterset);
  130. constructor op_none(op : tasmop);
  131. constructor op_reg(op : tasmop;_op1 : tregister);
  132. constructor op_const(op : tasmop;_op1 : longint);
  133. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  134. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  135. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  136. constructor op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  137. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  138. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  139. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  140. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  141. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  142. constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  143. { SFM/LFM }
  144. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  145. { *M*LL }
  146. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  147. { this is for Jmp instructions }
  148. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  149. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  150. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  151. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  152. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  153. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  154. function spilling_get_operation_type(opnr: longint): topertype;override;
  155. { assembler }
  156. public
  157. { the next will reset all instructions that can change in pass 2 }
  158. procedure ResetPass1;override;
  159. procedure ResetPass2;override;
  160. function CheckIfValid:boolean;
  161. function GetString:string;
  162. function Pass1(objdata:TObjData):longint;override;
  163. procedure Pass2(objdata:TObjData);override;
  164. protected
  165. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  166. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  167. procedure ppubuildderefimploper(var o:toper);override;
  168. procedure ppuderefoper(var o:toper);override;
  169. private
  170. { next fields are filled in pass1, so pass2 is faster }
  171. inssize : shortint;
  172. insoffset : longint;
  173. LastInsOffset : longint; { need to be public to be reset }
  174. insentry : PInsEntry;
  175. function InsEnd:longint;
  176. procedure create_ot(objdata:TObjData);
  177. function Matches(p:PInsEntry):longint;
  178. function calcsize(p:PInsEntry):shortint;
  179. procedure gencode(objdata:TObjData);
  180. function NeedAddrPrefix(opidx:byte):boolean;
  181. procedure Swapoperands;
  182. function FindInsentry(objdata:TObjData):boolean;
  183. end;
  184. tai_align = class(tai_align_abstract)
  185. { nothing to add }
  186. end;
  187. function spilling_create_load(const ref:treference;r:tregister): tai;
  188. function spilling_create_store(r:tregister; const ref:treference): tai;
  189. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  190. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  191. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  192. { inserts pc relative symbols at places where they are reachable }
  193. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  194. procedure InitAsm;
  195. procedure DoneAsm;
  196. implementation
  197. uses
  198. cutils,rgobj,itcpugas;
  199. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  200. begin
  201. allocate_oper(opidx+1);
  202. with oper[opidx]^ do
  203. begin
  204. if typ<>top_shifterop then
  205. begin
  206. clearop(opidx);
  207. new(shifterop);
  208. end;
  209. shifterop^:=so;
  210. typ:=top_shifterop;
  211. if assigned(add_reg_instruction_hook) then
  212. add_reg_instruction_hook(self,shifterop^.rs);
  213. end;
  214. end;
  215. procedure taicpu.loadregset(opidx:longint;const s:tcpuregisterset);
  216. var
  217. i : byte;
  218. begin
  219. allocate_oper(opidx+1);
  220. with oper[opidx]^ do
  221. begin
  222. if typ<>top_regset then
  223. clearop(opidx);
  224. new(regset);
  225. regset^:=s;
  226. typ:=top_regset;
  227. for i:=RS_R0 to RS_R15 do
  228. begin
  229. if assigned(add_reg_instruction_hook) and (i in regset^) then
  230. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,R_SUBWHOLE));
  231. end;
  232. end;
  233. end;
  234. {*****************************************************************************
  235. taicpu Constructors
  236. *****************************************************************************}
  237. constructor taicpu.op_none(op : tasmop);
  238. begin
  239. inherited create(op);
  240. end;
  241. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  242. begin
  243. inherited create(op);
  244. ops:=1;
  245. loadreg(0,_op1);
  246. end;
  247. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  248. begin
  249. inherited create(op);
  250. ops:=1;
  251. loadconst(0,aint(_op1));
  252. end;
  253. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  254. begin
  255. inherited create(op);
  256. ops:=2;
  257. loadreg(0,_op1);
  258. loadreg(1,_op2);
  259. end;
  260. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  261. begin
  262. inherited create(op);
  263. ops:=2;
  264. loadreg(0,_op1);
  265. loadconst(1,aint(_op2));
  266. end;
  267. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  268. begin
  269. inherited create(op);
  270. ops:=2;
  271. loadref(0,_op1);
  272. loadregset(1,_op2);
  273. end;
  274. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  275. begin
  276. inherited create(op);
  277. ops:=2;
  278. loadreg(0,_op1);
  279. loadref(1,_op2);
  280. end;
  281. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  282. begin
  283. inherited create(op);
  284. ops:=3;
  285. loadreg(0,_op1);
  286. loadreg(1,_op2);
  287. loadreg(2,_op3);
  288. end;
  289. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  290. begin
  291. inherited create(op);
  292. ops:=4;
  293. loadreg(0,_op1);
  294. loadreg(1,_op2);
  295. loadreg(2,_op3);
  296. loadreg(3,_op4);
  297. end;
  298. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  299. begin
  300. inherited create(op);
  301. ops:=3;
  302. loadreg(0,_op1);
  303. loadreg(1,_op2);
  304. loadconst(2,aint(_op3));
  305. end;
  306. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  307. begin
  308. inherited create(op);
  309. ops:=3;
  310. loadreg(0,_op1);
  311. loadconst(1,_op2);
  312. loadref(2,_op3);
  313. end;
  314. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  315. begin
  316. inherited create(op);
  317. ops:=3;
  318. loadreg(0,_op1);
  319. loadreg(1,_op2);
  320. loadsymbol(0,_op3,_op3ofs);
  321. end;
  322. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  323. begin
  324. inherited create(op);
  325. ops:=3;
  326. loadreg(0,_op1);
  327. loadreg(1,_op2);
  328. loadref(2,_op3);
  329. end;
  330. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  331. begin
  332. inherited create(op);
  333. ops:=3;
  334. loadreg(0,_op1);
  335. loadreg(1,_op2);
  336. loadshifterop(2,_op3);
  337. end;
  338. constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  339. begin
  340. inherited create(op);
  341. ops:=4;
  342. loadreg(0,_op1);
  343. loadreg(1,_op2);
  344. loadreg(2,_op3);
  345. loadshifterop(3,_op4);
  346. end;
  347. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  348. begin
  349. inherited create(op);
  350. condition:=cond;
  351. ops:=1;
  352. loadsymbol(0,_op1,0);
  353. end;
  354. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  355. begin
  356. inherited create(op);
  357. ops:=1;
  358. loadsymbol(0,_op1,0);
  359. end;
  360. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  361. begin
  362. inherited create(op);
  363. ops:=1;
  364. loadsymbol(0,_op1,_op1ofs);
  365. end;
  366. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  367. begin
  368. inherited create(op);
  369. ops:=2;
  370. loadreg(0,_op1);
  371. loadsymbol(1,_op2,_op2ofs);
  372. end;
  373. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  374. begin
  375. inherited create(op);
  376. ops:=2;
  377. loadsymbol(0,_op1,_op1ofs);
  378. loadref(1,_op2);
  379. end;
  380. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  381. begin
  382. { allow the register allocator to remove unnecessary moves }
  383. result:=(((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  384. ((opcode=A_MVF) and (regtype = R_FPUREGISTER))
  385. ) and
  386. (condition=C_None) and
  387. (ops=2) and
  388. (oper[0]^.typ=top_reg) and
  389. (oper[1]^.typ=top_reg) and
  390. (oper[0]^.reg=oper[1]^.reg);
  391. end;
  392. function spilling_create_load(const ref:treference;r:tregister): tai;
  393. begin
  394. case getregtype(r) of
  395. R_INTREGISTER :
  396. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  397. R_FPUREGISTER :
  398. { use lfm because we don't know the current internal format
  399. and avoid exceptions
  400. }
  401. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  402. else
  403. internalerror(200401041);
  404. end;
  405. end;
  406. function spilling_create_store(r:tregister; const ref:treference): tai;
  407. begin
  408. case getregtype(r) of
  409. R_INTREGISTER :
  410. result:=taicpu.op_reg_ref(A_STR,r,ref);
  411. R_FPUREGISTER :
  412. { use sfm because we don't know the current internal format
  413. and avoid exceptions
  414. }
  415. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  416. else
  417. internalerror(200401041);
  418. end;
  419. end;
  420. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  421. begin
  422. case opcode of
  423. A_ADC,A_ADD,A_AND,
  424. A_EOR,A_CLZ,
  425. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  426. A_LDRSH,A_LDRT,
  427. A_MOV,A_MVN,A_MLA,A_MUL,
  428. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  429. A_SWP,A_SWPB,
  430. A_LDF,A_FLT,A_FIX,
  431. A_ADF,A_DVF,A_FDV,A_FML,
  432. A_RFS,A_RFC,A_RDF,
  433. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  434. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  435. A_LFM:
  436. if opnr=0 then
  437. result:=operand_write
  438. else
  439. result:=operand_read;
  440. A_BIC,A_BKPT,A_B,A_BL,A_BLX,A_BX,
  441. A_CMN,A_CMP,A_TEQ,A_TST,
  442. A_CMF,A_CMFE,A_WFS,A_CNF:
  443. result:=operand_read;
  444. A_SMLAL,A_UMLAL:
  445. if opnr in [0,1] then
  446. result:=operand_readwrite
  447. else
  448. result:=operand_read;
  449. A_SMULL,A_UMULL:
  450. if opnr in [0,1] then
  451. result:=operand_write
  452. else
  453. result:=operand_read;
  454. A_STR,A_STRB,A_STRBT,
  455. A_STRH,A_STRT,A_STF,A_SFM:
  456. { important is what happens with the involved registers }
  457. if opnr=0 then
  458. result := operand_read
  459. else
  460. { check for pre/post indexed }
  461. result := operand_read;
  462. else
  463. internalerror(200403151);
  464. end;
  465. end;
  466. procedure BuildInsTabCache;
  467. var
  468. i : longint;
  469. begin
  470. new(instabcache);
  471. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  472. i:=0;
  473. while (i<InsTabEntries) do
  474. begin
  475. if InsTabCache^[InsTab[i].Opcode]=-1 then
  476. InsTabCache^[InsTab[i].Opcode]:=i;
  477. inc(i);
  478. end;
  479. end;
  480. procedure InitAsm;
  481. begin
  482. if not assigned(instabcache) then
  483. BuildInsTabCache;
  484. end;
  485. procedure DoneAsm;
  486. begin
  487. if assigned(instabcache) then
  488. begin
  489. dispose(instabcache);
  490. instabcache:=nil;
  491. end;
  492. end;
  493. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  494. begin
  495. i.oppostfix:=pf;
  496. result:=i;
  497. end;
  498. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  499. begin
  500. i.roundingmode:=rm;
  501. result:=i;
  502. end;
  503. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  504. begin
  505. i.condition:=c;
  506. result:=i;
  507. end;
  508. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  509. var
  510. curpos : longint;
  511. lastpos : longint;
  512. curop : longint;
  513. curtai : tai;
  514. curdatatai,hp,hp2 : tai;
  515. curdata : TAsmList;
  516. l : tasmlabel;
  517. removeref : boolean;
  518. begin
  519. curdata:=TAsmList.create;
  520. lastpos:=-1;
  521. curpos:=0;
  522. curtai:=tai(list.first);
  523. while assigned(curtai) do
  524. begin
  525. { instruction? }
  526. if curtai.typ=ait_instruction then
  527. begin
  528. { walk through all operand of the instruction }
  529. for curop:=0 to taicpu(curtai).ops-1 do
  530. begin
  531. { reference? }
  532. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  533. begin
  534. { pc relative symbol? }
  535. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  536. if assigned(curdatatai) and
  537. { move only if we're at the first reference of a label }
  538. (taicpu(curtai).oper[curop]^.ref^.offset=0) then
  539. begin
  540. { check if symbol already used. }
  541. { if yes, reuse the symbol }
  542. hp:=tai(curdatatai.next);
  543. removeref:=false;
  544. if assigned(hp) and (hp.typ=ait_const) then
  545. begin
  546. hp2:=tai(curdata.first);
  547. while assigned(hp2) do
  548. begin
  549. if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
  550. and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label)
  551. then
  552. begin
  553. with taicpu(curtai).oper[curop]^.ref^ do
  554. begin
  555. symboldata:=hp2.previous;
  556. symbol:=tai_label(hp2.previous).labsym;
  557. end;
  558. removeref:=true;
  559. break;
  560. end;
  561. hp2:=tai(hp2.next);
  562. end;
  563. end;
  564. { move or remove symbol reference }
  565. repeat
  566. hp:=tai(curdatatai.next);
  567. listtoinsert.remove(curdatatai);
  568. if removeref then
  569. curdatatai.free
  570. else
  571. curdata.concat(curdatatai);
  572. curdatatai:=hp;
  573. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  574. if lastpos=-1 then
  575. lastpos:=curpos;
  576. end;
  577. end;
  578. end;
  579. inc(curpos);
  580. end;
  581. { split only at real instructions else the test below fails }
  582. if ((curpos-lastpos)>1016) and (curtai.typ=ait_instruction) and
  583. (
  584. { don't split loads of pc to lr and the following move }
  585. not(
  586. (taicpu(curtai).opcode=A_MOV) and
  587. (taicpu(curtai).oper[0]^.typ=top_reg) and
  588. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  589. (taicpu(curtai).oper[1]^.typ=top_reg) and
  590. (taicpu(curtai).oper[1]^.reg=NR_PC)
  591. )
  592. ) then
  593. begin
  594. lastpos:=curpos;
  595. hp:=tai(curtai.next);
  596. current_asmdata.getjumplabel(l);
  597. curdata.insert(taicpu.op_sym(A_B,l));
  598. curdata.concat(tai_label.create(l));
  599. list.insertlistafter(curtai,curdata);
  600. curtai:=hp;
  601. end
  602. else
  603. curtai:=tai(curtai.next);
  604. end;
  605. list.concatlist(curdata);
  606. curdata.free;
  607. end;
  608. (*
  609. Floating point instruction format information, taken from the linux kernel
  610. ARM Floating Point Instruction Classes
  611. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  612. |c o n d|1 1 0 P|U|u|W|L| Rn |v| Fd |0|0|0|1| o f f s e t | CPDT
  613. |c o n d|1 1 0 P|U|w|W|L| Rn |x| Fd |0|0|1|0| o f f s e t | CPDT (copro 2)
  614. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  615. |c o n d|1 1 1 0|a|b|c|d|e| Fn |j| Fd |0|0|0|1|f|g|h|0|i| Fm | CPDO
  616. |c o n d|1 1 1 0|a|b|c|L|e| Fn | Rd |0|0|0|1|f|g|h|1|i| Fm | CPRT
  617. |c o n d|1 1 1 0|a|b|c|1|e| Fn |1|1|1|1|0|0|0|1|f|g|h|1|i| Fm | comparisons
  618. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  619. CPDT data transfer instructions
  620. LDF, STF, LFM (copro 2), SFM (copro 2)
  621. CPDO dyadic arithmetic instructions
  622. ADF, MUF, SUF, RSF, DVF, RDF,
  623. POW, RPW, RMF, FML, FDV, FRD, POL
  624. CPDO monadic arithmetic instructions
  625. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  626. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  627. CPRT joint arithmetic/data transfer instructions
  628. FIX (arithmetic followed by load/store)
  629. FLT (load/store followed by arithmetic)
  630. CMF, CNF CMFE, CNFE (comparisons)
  631. WFS, RFS (write/read floating point status register)
  632. WFC, RFC (write/read floating point control register)
  633. cond condition codes
  634. P pre/post index bit: 0 = postindex, 1 = preindex
  635. U up/down bit: 0 = stack grows down, 1 = stack grows up
  636. W write back bit: 1 = update base register (Rn)
  637. L load/store bit: 0 = store, 1 = load
  638. Rn base register
  639. Rd destination/source register
  640. Fd floating point destination register
  641. Fn floating point source register
  642. Fm floating point source register or floating point constant
  643. uv transfer length (TABLE 1)
  644. wx register count (TABLE 2)
  645. abcd arithmetic opcode (TABLES 3 & 4)
  646. ef destination size (rounding precision) (TABLE 5)
  647. gh rounding mode (TABLE 6)
  648. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  649. i constant bit: 1 = constant (TABLE 6)
  650. */
  651. /*
  652. TABLE 1
  653. +-------------------------+---+---+---------+---------+
  654. | Precision | u | v | FPSR.EP | length |
  655. +-------------------------+---+---+---------+---------+
  656. | Single | 0 | 0 | x | 1 words |
  657. | Double | 1 | 1 | x | 2 words |
  658. | Extended | 1 | 1 | x | 3 words |
  659. | Packed decimal | 1 | 1 | 0 | 3 words |
  660. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  661. +-------------------------+---+---+---------+---------+
  662. Note: x = don't care
  663. */
  664. /*
  665. TABLE 2
  666. +---+---+---------------------------------+
  667. | w | x | Number of registers to transfer |
  668. +---+---+---------------------------------+
  669. | 0 | 1 | 1 |
  670. | 1 | 0 | 2 |
  671. | 1 | 1 | 3 |
  672. | 0 | 0 | 4 |
  673. +---+---+---------------------------------+
  674. */
  675. /*
  676. TABLE 3: Dyadic Floating Point Opcodes
  677. +---+---+---+---+----------+-----------------------+-----------------------+
  678. | a | b | c | d | Mnemonic | Description | Operation |
  679. +---+---+---+---+----------+-----------------------+-----------------------+
  680. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  681. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  682. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  683. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  684. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  685. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  686. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  687. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  688. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  689. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  690. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  691. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  692. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  693. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  694. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  695. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  696. +---+---+---+---+----------+-----------------------+-----------------------+
  697. Note: POW, RPW, POL are deprecated, and are available for backwards
  698. compatibility only.
  699. */
  700. /*
  701. TABLE 4: Monadic Floating Point Opcodes
  702. +---+---+---+---+----------+-----------------------+-----------------------+
  703. | a | b | c | d | Mnemonic | Description | Operation |
  704. +---+---+---+---+----------+-----------------------+-----------------------+
  705. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  706. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  707. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  708. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  709. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  710. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  711. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  712. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  713. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  714. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  715. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  716. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  717. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  718. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  719. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  720. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  721. +---+---+---+---+----------+-----------------------+-----------------------+
  722. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  723. available for backwards compatibility only.
  724. */
  725. /*
  726. TABLE 5
  727. +-------------------------+---+---+
  728. | Rounding Precision | e | f |
  729. +-------------------------+---+---+
  730. | IEEE Single precision | 0 | 0 |
  731. | IEEE Double precision | 0 | 1 |
  732. | IEEE Extended precision | 1 | 0 |
  733. | undefined (trap) | 1 | 1 |
  734. +-------------------------+---+---+
  735. */
  736. /*
  737. TABLE 5
  738. +---------------------------------+---+---+
  739. | Rounding Mode | g | h |
  740. +---------------------------------+---+---+
  741. | Round to nearest (default) | 0 | 0 |
  742. | Round toward plus infinity | 0 | 1 |
  743. | Round toward negative infinity | 1 | 0 |
  744. | Round toward zero | 1 | 1 |
  745. +---------------------------------+---+---+
  746. *)
  747. function taicpu.GetString:string;
  748. var
  749. i : longint;
  750. s : string;
  751. addsize : boolean;
  752. begin
  753. s:='['+gas_op2str[opcode];
  754. for i:=0 to ops-1 do
  755. begin
  756. with oper[i]^ do
  757. begin
  758. if i=0 then
  759. s:=s+' '
  760. else
  761. s:=s+',';
  762. { type }
  763. addsize:=false;
  764. if (ot and OT_VREG)=OT_VREG then
  765. s:=s+'vreg'
  766. else
  767. if (ot and OT_FPUREG)=OT_FPUREG then
  768. s:=s+'fpureg'
  769. else
  770. if (ot and OT_REGISTER)=OT_REGISTER then
  771. begin
  772. s:=s+'reg';
  773. addsize:=true;
  774. end
  775. else
  776. if (ot and OT_REGLIST)=OT_REGLIST then
  777. begin
  778. s:=s+'reglist';
  779. addsize:=false;
  780. end
  781. else
  782. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  783. begin
  784. s:=s+'imm';
  785. addsize:=true;
  786. end
  787. else
  788. if (ot and OT_MEMORY)=OT_MEMORY then
  789. begin
  790. s:=s+'mem';
  791. addsize:=true;
  792. if (ot and OT_AM2)<>0 then
  793. s:=s+' am2 ';
  794. end
  795. else
  796. s:=s+'???';
  797. { size }
  798. if addsize then
  799. begin
  800. if (ot and OT_BITS8)<>0 then
  801. s:=s+'8'
  802. else
  803. if (ot and OT_BITS16)<>0 then
  804. s:=s+'24'
  805. else
  806. if (ot and OT_BITS32)<>0 then
  807. s:=s+'32'
  808. else
  809. if (ot and OT_BITSSHIFTER)<>0 then
  810. s:=s+'shifter'
  811. else
  812. s:=s+'??';
  813. { signed }
  814. if (ot and OT_SIGNED)<>0 then
  815. s:=s+'s';
  816. end;
  817. end;
  818. end;
  819. GetString:=s+']';
  820. end;
  821. procedure taicpu.ResetPass1;
  822. begin
  823. { we need to reset everything here, because the choosen insentry
  824. can be invalid for a new situation where the previously optimized
  825. insentry is not correct }
  826. InsEntry:=nil;
  827. InsSize:=0;
  828. LastInsOffset:=-1;
  829. end;
  830. procedure taicpu.ResetPass2;
  831. begin
  832. { we are here in a second pass, check if the instruction can be optimized }
  833. if assigned(InsEntry) and
  834. ((InsEntry^.flags and IF_PASS2)<>0) then
  835. begin
  836. InsEntry:=nil;
  837. InsSize:=0;
  838. end;
  839. LastInsOffset:=-1;
  840. end;
  841. function taicpu.CheckIfValid:boolean;
  842. begin
  843. end;
  844. function taicpu.Pass1(objdata:TObjData):longint;
  845. var
  846. ldr2op : array[PF_B..PF_T] of tasmop = (
  847. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  848. str2op : array[PF_B..PF_T] of tasmop = (
  849. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  850. begin
  851. Pass1:=0;
  852. { Save the old offset and set the new offset }
  853. InsOffset:=ObjData.CurrObjSec.Size;
  854. { Error? }
  855. if (Insentry=nil) and (InsSize=-1) then
  856. exit;
  857. { set the file postion }
  858. current_filepos:=fileinfo;
  859. { tranlate LDR+postfix to complete opcode }
  860. if (opcode=A_LDR) and (oppostfix<>PF_None) then
  861. begin
  862. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  863. opcode:=ldr2op[oppostfix]
  864. else
  865. internalerror(2005091001);
  866. if opcode=A_None then
  867. internalerror(2005091004);
  868. { postfix has been added to opcode }
  869. oppostfix:=PF_None;
  870. end
  871. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  872. begin
  873. if (oppostfix in [low(str2op)..high(str2op)]) then
  874. opcode:=str2op[oppostfix]
  875. else
  876. internalerror(2005091002);
  877. if opcode=A_None then
  878. internalerror(2005091003);
  879. { postfix has been added to opcode }
  880. oppostfix:=PF_None;
  881. end;
  882. { Get InsEntry }
  883. if FindInsEntry(objdata) then
  884. begin
  885. InsSize:=4;
  886. LastInsOffset:=InsOffset;
  887. Pass1:=InsSize;
  888. exit;
  889. end;
  890. LastInsOffset:=-1;
  891. end;
  892. procedure taicpu.Pass2(objdata:TObjData);
  893. begin
  894. { error in pass1 ? }
  895. if insentry=nil then
  896. exit;
  897. current_filepos:=fileinfo;
  898. { Generate the instruction }
  899. GenCode(objdata);
  900. end;
  901. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  902. begin
  903. end;
  904. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  905. begin
  906. end;
  907. procedure taicpu.ppubuildderefimploper(var o:toper);
  908. begin
  909. end;
  910. procedure taicpu.ppuderefoper(var o:toper);
  911. begin
  912. end;
  913. function taicpu.InsEnd:longint;
  914. begin
  915. end;
  916. procedure taicpu.create_ot(objdata:TObjData);
  917. var
  918. i,l,relsize : longint;
  919. dummy : byte;
  920. currsym : TObjSymbol;
  921. begin
  922. if ops=0 then
  923. exit;
  924. { update oper[].ot field }
  925. for i:=0 to ops-1 do
  926. with oper[i]^ do
  927. begin
  928. case typ of
  929. top_regset:
  930. begin
  931. ot:=OT_REGLIST;
  932. end;
  933. top_reg :
  934. begin
  935. case getregtype(reg) of
  936. R_INTREGISTER:
  937. ot:=OT_REG32 or OT_SHIFTEROP;
  938. R_FPUREGISTER:
  939. ot:=OT_FPUREG;
  940. else
  941. internalerror(2005090901);
  942. end;
  943. end;
  944. top_ref :
  945. begin
  946. if ref^.refaddr=addr_no then
  947. begin
  948. { create ot field }
  949. { we should get the size here dependend on the
  950. instruction }
  951. if (ot and OT_SIZE_MASK)=0 then
  952. ot:=OT_MEMORY or OT_BITS32
  953. else
  954. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  955. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  956. ot:=ot or OT_MEM_OFFS;
  957. { if we need to fix a reference, we do it here }
  958. { pc relative addressing }
  959. if (ref^.base=NR_NO) and
  960. (ref^.index=NR_NO) and
  961. (ref^.shiftmode=SM_None)
  962. { at least we should check if the destination symbol
  963. is in a text section }
  964. { and
  965. (ref^.symbol^.owner="text") } then
  966. ref^.base:=NR_PC;
  967. { determine possible address modes }
  968. if (ref^.base<>NR_NO) and
  969. (
  970. (
  971. (ref^.index=NR_NO) and
  972. (ref^.shiftmode=SM_None) and
  973. (ref^.offset>=-4097) and
  974. (ref^.offset<=4097)
  975. ) or
  976. (
  977. (ref^.shiftmode=SM_None) and
  978. (ref^.offset=0)
  979. ) or
  980. (
  981. (ref^.index<>NR_NO) and
  982. (ref^.shiftmode<>SM_None) and
  983. (ref^.shiftimm<=31) and
  984. (ref^.offset=0)
  985. )
  986. ) then
  987. ot:=ot or OT_AM2;
  988. if (ref^.index<>NR_NO) and
  989. (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  990. (
  991. (ref^.base=NR_NO) and
  992. (ref^.shiftmode=SM_None) and
  993. (ref^.offset=0)
  994. ) then
  995. ot:=ot or OT_AM4;
  996. end
  997. else
  998. begin
  999. l:=ref^.offset;
  1000. currsym:=ObjData.symbolref(ref^.symbol);
  1001. if assigned(currsym) then
  1002. inc(l,currsym.address);
  1003. relsize:=(InsOffset+2)-l;
  1004. if (relsize<-33554428) or (relsize>33554428) then
  1005. ot:=OT_IMM32
  1006. else
  1007. ot:=OT_IMM24;
  1008. end;
  1009. end;
  1010. top_local :
  1011. begin
  1012. { we should get the size here dependend on the
  1013. instruction }
  1014. if (ot and OT_SIZE_MASK)=0 then
  1015. ot:=OT_MEMORY or OT_BITS32
  1016. else
  1017. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1018. end;
  1019. top_const :
  1020. begin
  1021. ot:=OT_IMMEDIATE;
  1022. if is_shifter_const(val,dummy) then
  1023. ot:=OT_IMMSHIFTER
  1024. else
  1025. ot:=OT_IMM32
  1026. end;
  1027. top_none :
  1028. begin
  1029. { generated when there was an error in the
  1030. assembler reader. It never happends when generating
  1031. assembler }
  1032. end;
  1033. top_shifterop:
  1034. begin
  1035. ot:=OT_SHIFTEROP;
  1036. end;
  1037. else
  1038. internalerror(200402261);
  1039. end;
  1040. end;
  1041. end;
  1042. function taicpu.Matches(p:PInsEntry):longint;
  1043. { * IF_SM stands for Size Match: any operand whose size is not
  1044. * explicitly specified by the template is `really' intended to be
  1045. * the same size as the first size-specified operand.
  1046. * Non-specification is tolerated in the input instruction, but
  1047. * _wrong_ specification is not.
  1048. *
  1049. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1050. * three-operand instructions such as SHLD: it implies that the
  1051. * first two operands must match in size, but that the third is
  1052. * required to be _unspecified_.
  1053. *
  1054. * IF_SB invokes Size Byte: operands with unspecified size in the
  1055. * template are really bytes, and so no non-byte specification in
  1056. * the input instruction will be tolerated. IF_SW similarly invokes
  1057. * Size Word, and IF_SD invokes Size Doubleword.
  1058. *
  1059. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1060. * that any operand with unspecified size in the template is
  1061. * required to have unspecified size in the instruction too...)
  1062. }
  1063. var
  1064. i,j,asize,oprs : longint;
  1065. siz : array[0..3] of longint;
  1066. begin
  1067. Matches:=100;
  1068. writeln(getstring,'---');
  1069. { Check the opcode and operands }
  1070. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1071. begin
  1072. Matches:=0;
  1073. exit;
  1074. end;
  1075. { Check that no spurious colons or TOs are present }
  1076. for i:=0 to p^.ops-1 do
  1077. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  1078. begin
  1079. Matches:=0;
  1080. exit;
  1081. end;
  1082. { Check that the operand flags all match up }
  1083. for i:=0 to p^.ops-1 do
  1084. begin
  1085. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  1086. ((p^.optypes[i] and OT_SIZE_MASK) and
  1087. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  1088. begin
  1089. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  1090. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  1091. begin
  1092. Matches:=0;
  1093. exit;
  1094. end
  1095. else
  1096. Matches:=1;
  1097. end;
  1098. end;
  1099. { check postfixes:
  1100. the existance of a certain postfix requires a
  1101. particular code }
  1102. { update condition flags
  1103. or floating point single }
  1104. if (oppostfix=PF_S) and
  1105. not(p^.code[0] in [#$04]) then
  1106. begin
  1107. Matches:=0;
  1108. exit;
  1109. end;
  1110. { floating point size }
  1111. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  1112. not(p^.code[0] in []) then
  1113. begin
  1114. Matches:=0;
  1115. exit;
  1116. end;
  1117. { multiple load/store address modes }
  1118. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1119. not(p^.code[0] in [
  1120. // ldr,str,ldrb,strb
  1121. #$17,
  1122. // stm,ldm
  1123. #$26
  1124. ]) then
  1125. begin
  1126. Matches:=0;
  1127. exit;
  1128. end;
  1129. { we shouldn't see any opsize prefixes here }
  1130. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  1131. begin
  1132. Matches:=0;
  1133. exit;
  1134. end;
  1135. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  1136. begin
  1137. Matches:=0;
  1138. exit;
  1139. end;
  1140. { Check operand sizes }
  1141. { as default an untyped size can get all the sizes, this is different
  1142. from nasm, but else we need to do a lot checking which opcodes want
  1143. size or not with the automatic size generation }
  1144. asize:=longint($ffffffff);
  1145. (*
  1146. if (p^.flags and IF_SB)<>0 then
  1147. asize:=OT_BITS8
  1148. else if (p^.flags and IF_SW)<>0 then
  1149. asize:=OT_BITS16
  1150. else if (p^.flags and IF_SD)<>0 then
  1151. asize:=OT_BITS32;
  1152. if (p^.flags and IF_ARMASK)<>0 then
  1153. begin
  1154. siz[0]:=0;
  1155. siz[1]:=0;
  1156. siz[2]:=0;
  1157. if (p^.flags and IF_AR0)<>0 then
  1158. siz[0]:=asize
  1159. else if (p^.flags and IF_AR1)<>0 then
  1160. siz[1]:=asize
  1161. else if (p^.flags and IF_AR2)<>0 then
  1162. siz[2]:=asize;
  1163. end
  1164. else
  1165. begin
  1166. { we can leave because the size for all operands is forced to be
  1167. the same
  1168. but not if IF_SB IF_SW or IF_SD is set PM }
  1169. if asize=-1 then
  1170. exit;
  1171. siz[0]:=asize;
  1172. siz[1]:=asize;
  1173. siz[2]:=asize;
  1174. end;
  1175. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1176. begin
  1177. if (p^.flags and IF_SM2)<>0 then
  1178. oprs:=2
  1179. else
  1180. oprs:=p^.ops;
  1181. for i:=0 to oprs-1 do
  1182. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1183. begin
  1184. for j:=0 to oprs-1 do
  1185. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1186. break;
  1187. end;
  1188. end
  1189. else
  1190. oprs:=2;
  1191. { Check operand sizes }
  1192. for i:=0 to p^.ops-1 do
  1193. begin
  1194. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1195. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1196. { Immediates can always include smaller size }
  1197. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1198. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1199. Matches:=2;
  1200. end;
  1201. *)
  1202. end;
  1203. function taicpu.calcsize(p:PInsEntry):shortint;
  1204. begin
  1205. result:=4;
  1206. end;
  1207. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1208. begin
  1209. end;
  1210. procedure taicpu.Swapoperands;
  1211. begin
  1212. end;
  1213. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1214. var
  1215. i : longint;
  1216. begin
  1217. result:=false;
  1218. { Things which may only be done once, not when a second pass is done to
  1219. optimize }
  1220. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1221. begin
  1222. { create the .ot fields }
  1223. create_ot(objdata);
  1224. { set the file postion }
  1225. current_filepos:=fileinfo;
  1226. end
  1227. else
  1228. begin
  1229. { we've already an insentry so it's valid }
  1230. result:=true;
  1231. exit;
  1232. end;
  1233. { Lookup opcode in the table }
  1234. InsSize:=-1;
  1235. i:=instabcache^[opcode];
  1236. if i=-1 then
  1237. begin
  1238. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1239. exit;
  1240. end;
  1241. insentry:=@instab[i];
  1242. while (insentry^.opcode=opcode) do
  1243. begin
  1244. if matches(insentry)=100 then
  1245. begin
  1246. result:=true;
  1247. exit;
  1248. end;
  1249. inc(i);
  1250. insentry:=@instab[i];
  1251. end;
  1252. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1253. { No instruction found, set insentry to nil and inssize to -1 }
  1254. insentry:=nil;
  1255. inssize:=-1;
  1256. end;
  1257. procedure taicpu.gencode(objdata:TObjData);
  1258. var
  1259. bytes : dword;
  1260. i_field : byte;
  1261. procedure setshifterop(op : byte);
  1262. begin
  1263. case oper[op]^.typ of
  1264. top_const:
  1265. begin
  1266. i_field:=1;
  1267. bytes:=bytes or (oper[op]^.val and $fff);
  1268. end;
  1269. top_reg:
  1270. begin
  1271. i_field:=0;
  1272. bytes:=bytes or (getsupreg(oper[op]^.reg) shl 16);
  1273. { does a real shifter op follow? }
  1274. if (op+1<=op) and (oper[op+1]^.typ=top_shifterop) then
  1275. begin
  1276. end;
  1277. end;
  1278. else
  1279. internalerror(2005091103);
  1280. end;
  1281. end;
  1282. begin
  1283. bytes:=$0;
  1284. { evaluate and set condition code }
  1285. { condition code allowed? }
  1286. { setup rest of the instruction }
  1287. case insentry^.code[0] of
  1288. #$08:
  1289. begin
  1290. { set instruction code }
  1291. bytes:=bytes or (ord(insentry^.code[1]) shl 26);
  1292. bytes:=bytes or (ord(insentry^.code[2]) shl 21);
  1293. { set destination }
  1294. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  1295. { create shifter op }
  1296. setshifterop(1);
  1297. { set i field }
  1298. bytes:=bytes or (i_field shl 25);
  1299. { set s if necessary }
  1300. if oppostfix=PF_S then
  1301. bytes:=bytes or (1 shl 20);
  1302. end;
  1303. #$ff:
  1304. internalerror(2005091101);
  1305. else
  1306. internalerror(2005091102);
  1307. end;
  1308. { we're finished, write code }
  1309. objdata.writebytes(bytes,sizeof(bytes));
  1310. end;
  1311. end.
  1312. {$ifdef dummy}
  1313. (*
  1314. static void gencode (long segment, long offset, int bits,
  1315. insn *ins, char *codes, long insn_end)
  1316. {
  1317. int has_S_code; /* S - setflag */
  1318. int has_B_code; /* B - setflag */
  1319. int has_T_code; /* T - setflag */
  1320. int has_W_code; /* ! => W flag */
  1321. int has_F_code; /* ^ => S flag */
  1322. int keep;
  1323. unsigned char c;
  1324. unsigned char bytes[4];
  1325. long data, size;
  1326. static int cc_code[] = /* bit pattern of cc */
  1327. { /* order as enum in */
  1328. 0x0E, 0x03, 0x02, 0x00, /* nasm.h */
  1329. 0x0A, 0x0C, 0x08, 0x0D,
  1330. 0x09, 0x0B, 0x04, 0x01,
  1331. 0x05, 0x07, 0x06,
  1332. };
  1333. (*
  1334. #ifdef DEBUG
  1335. static char *CC[] =
  1336. { /* condition code names */
  1337. "AL", "CC", "CS", "EQ",
  1338. "GE", "GT", "HI", "LE",
  1339. "LS", "LT", "MI", "NE",
  1340. "PL", "VC", "VS", "",
  1341. "S"
  1342. };
  1343. *)
  1344. has_S_code = (ins->condition & C_SSETFLAG);
  1345. has_B_code = (ins->condition & C_BSETFLAG);
  1346. has_T_code = (ins->condition & C_TSETFLAG);
  1347. has_W_code = (ins->condition & C_EXSETFLAG);
  1348. has_F_code = (ins->condition & C_FSETFLAG);
  1349. ins->condition = (ins->condition & 0x0F);
  1350. (*
  1351. if (rt_debug)
  1352. {
  1353. printf ("gencode: instruction: %s%s", insn_names[ins->opcode],
  1354. CC[ins->condition & 0x0F]);
  1355. if (has_S_code)
  1356. printf ("S");
  1357. if (has_B_code)
  1358. printf ("B");
  1359. if (has_T_code)
  1360. printf ("T");
  1361. if (has_W_code)
  1362. printf ("!");
  1363. if (has_F_code)
  1364. printf ("^");
  1365. printf ("\n");
  1366. c = *codes;
  1367. printf (" (%d) decode - '0x%02X'\n", ins->operands, c);
  1368. bytes[0] = 0xB;
  1369. bytes[1] = 0xE;
  1370. bytes[2] = 0xE;
  1371. bytes[3] = 0xF;
  1372. }
  1373. *)
  1374. // First condition code in upper nibble
  1375. if (ins->condition < C_NONE)
  1376. {
  1377. c = cc_code[ins->condition] << 4;
  1378. }
  1379. else
  1380. {
  1381. c = cc_code[C_AL] << 4; // is often ALWAYS but not always
  1382. }
  1383. switch (keep = *codes)
  1384. {
  1385. case 1:
  1386. // B, BL
  1387. ++codes;
  1388. c |= *codes++;
  1389. bytes[0] = c;
  1390. if (ins->oprs[0].segment != segment)
  1391. {
  1392. // fais une relocation
  1393. c = 1;
  1394. data = 0; // Let the linker locate ??
  1395. }
  1396. else
  1397. {
  1398. c = 0;
  1399. data = ins->oprs[0].offset - (offset + 8);
  1400. if (data % 4)
  1401. {
  1402. errfunc (ERR_NONFATAL, "offset not aligned on 4 bytes");
  1403. }
  1404. }
  1405. if (data >= 0x1000)
  1406. {
  1407. errfunc (ERR_NONFATAL, "too long offset");
  1408. }
  1409. data = data >> 2;
  1410. bytes[1] = (data >> 16) & 0xFF;
  1411. bytes[2] = (data >> 8) & 0xFF;
  1412. bytes[3] = (data ) & 0xFF;
  1413. if (c == 1)
  1414. {
  1415. // out (offset, segment, &bytes[0], OUT_RAWDATA+1, NO_SEG, NO_SEG);
  1416. out (offset, segment, &bytes[0], OUT_REL3ADR+4, ins->oprs[0].segment, NO_SEG);
  1417. }
  1418. else
  1419. {
  1420. out (offset, segment, &bytes[0], OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1421. }
  1422. return;
  1423. case 2:
  1424. // SWI
  1425. ++codes;
  1426. c |= *codes++;
  1427. bytes[0] = c;
  1428. data = ins->oprs[0].offset;
  1429. bytes[1] = (data >> 16) & 0xFF;
  1430. bytes[2] = (data >> 8) & 0xFF;
  1431. bytes[3] = (data) & 0xFF;
  1432. out (offset, segment, &bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1433. return;
  1434. case 3:
  1435. // BX
  1436. ++codes;
  1437. c |= *codes++;
  1438. bytes[0] = c;
  1439. bytes[1] = *codes++;
  1440. bytes[2] = *codes++;
  1441. bytes[3] = *codes++;
  1442. c = regval (&ins->oprs[0],1);
  1443. if (c == 15) // PC
  1444. {
  1445. errfunc (ERR_WARNING, "'BX' with R15 has undefined behaviour");
  1446. }
  1447. else if (c > 15)
  1448. {
  1449. errfunc (ERR_NONFATAL, "Illegal register specified for 'BX'");
  1450. }
  1451. bytes[3] |= (c & 0x0F);
  1452. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1453. return;
  1454. case 4: // AND Rd,Rn,Rm
  1455. case 5: // AND Rd,Rn,Rm,<shift>Rs
  1456. case 6: // AND Rd,Rn,Rm,<shift>imm
  1457. case 7: // AND Rd,Rn,<shift>imm
  1458. ++codes;
  1459. #ifdef DEBUG
  1460. if (rt_debug)
  1461. {
  1462. printf (" decode - '0x%02X'\n", keep);
  1463. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1464. }
  1465. #endif
  1466. bytes[0] = c | *codes;
  1467. ++codes;
  1468. bytes[1] = *codes;
  1469. if (has_S_code)
  1470. bytes[1] |= 0x10;
  1471. c = regval (&ins->oprs[1],1);
  1472. // Rn in low nibble
  1473. bytes[1] |= c;
  1474. // Rd in high nibble
  1475. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1476. if (keep != 7)
  1477. {
  1478. // Rm in low nibble
  1479. bytes[3] = regval (&ins->oprs[2],1);
  1480. }
  1481. // Shifts if any
  1482. if (keep == 5 || keep == 6)
  1483. {
  1484. // Shift in bytes 2 and 3
  1485. if (keep == 5)
  1486. {
  1487. // Rs
  1488. c = regval (&ins->oprs[3],1);
  1489. bytes[2] |= c;
  1490. c = 0x10; // Set bit 4 in byte[3]
  1491. }
  1492. if (keep == 6)
  1493. {
  1494. c = (ins->oprs[3].offset) & 0x1F;
  1495. // #imm
  1496. bytes[2] |= c >> 1;
  1497. if (c & 0x01)
  1498. {
  1499. bytes[3] |= 0x80;
  1500. }
  1501. c = 0; // Clr bit 4 in byte[3]
  1502. }
  1503. // <shift>
  1504. c |= shiftval (&ins->oprs[3]) << 5;
  1505. bytes[3] |= c;
  1506. }
  1507. // reg,reg,imm
  1508. if (keep == 7)
  1509. {
  1510. int shimm;
  1511. shimm = imm_shift (ins->oprs[2].offset);
  1512. if (shimm == -1)
  1513. {
  1514. errfunc (ERR_NONFATAL, "cannot create that constant");
  1515. }
  1516. bytes[3] = shimm & 0xFF;
  1517. bytes[2] |= (shimm & 0xF00) >> 8;
  1518. }
  1519. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1520. return;
  1521. case 8: // MOV Rd,Rm
  1522. case 9: // MOV Rd,Rm,<shift>Rs
  1523. case 0xA: // MOV Rd,Rm,<shift>imm
  1524. case 0xB: // MOV Rd,<shift>imm
  1525. ++codes;
  1526. #ifdef DEBUG
  1527. if (rt_debug)
  1528. {
  1529. printf (" decode - '0x%02X'\n", keep);
  1530. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1531. }
  1532. #endif
  1533. bytes[0] = c | *codes;
  1534. ++codes;
  1535. bytes[1] = *codes;
  1536. if (has_S_code)
  1537. bytes[1] |= 0x10;
  1538. // Rd in high nibble
  1539. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1540. if (keep != 0x0B)
  1541. {
  1542. // Rm in low nibble
  1543. bytes[3] = regval (&ins->oprs[1],1);
  1544. }
  1545. // Shifts if any
  1546. if (keep == 0x09 || keep == 0x0A)
  1547. {
  1548. // Shift in bytes 2 and 3
  1549. if (keep == 0x09)
  1550. {
  1551. // Rs
  1552. c = regval (&ins->oprs[2],1);
  1553. bytes[2] |= c;
  1554. c = 0x10; // Set bit 4 in byte[3]
  1555. }
  1556. if (keep == 0x0A)
  1557. {
  1558. c = (ins->oprs[2].offset) & 0x1F;
  1559. // #imm
  1560. bytes[2] |= c >> 1;
  1561. if (c & 0x01)
  1562. {
  1563. bytes[3] |= 0x80;
  1564. }
  1565. c = 0; // Clr bit 4 in byte[3]
  1566. }
  1567. // <shift>
  1568. c |= shiftval (&ins->oprs[2]) << 5;
  1569. bytes[3] |= c;
  1570. }
  1571. // reg,imm
  1572. if (keep == 0x0B)
  1573. {
  1574. int shimm;
  1575. shimm = imm_shift (ins->oprs[1].offset);
  1576. if (shimm == -1)
  1577. {
  1578. errfunc (ERR_NONFATAL, "cannot create that constant");
  1579. }
  1580. bytes[3] = shimm & 0xFF;
  1581. bytes[2] |= (shimm & 0xF00) >> 8;
  1582. }
  1583. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1584. return;
  1585. case 0xC: // CMP Rn,Rm
  1586. case 0xD: // CMP Rn,Rm,<shift>Rs
  1587. case 0xE: // CMP Rn,Rm,<shift>imm
  1588. case 0xF: // CMP Rn,<shift>imm
  1589. ++codes;
  1590. bytes[0] = c | *codes++;
  1591. bytes[1] = *codes;
  1592. // Implicit S code
  1593. bytes[1] |= 0x10;
  1594. c = regval (&ins->oprs[0],1);
  1595. // Rn in low nibble
  1596. bytes[1] |= c;
  1597. // No destination
  1598. bytes[2] = 0;
  1599. if (keep != 0x0B)
  1600. {
  1601. // Rm in low nibble
  1602. bytes[3] = regval (&ins->oprs[1],1);
  1603. }
  1604. // Shifts if any
  1605. if (keep == 0x0D || keep == 0x0E)
  1606. {
  1607. // Shift in bytes 2 and 3
  1608. if (keep == 0x0D)
  1609. {
  1610. // Rs
  1611. c = regval (&ins->oprs[2],1);
  1612. bytes[2] |= c;
  1613. c = 0x10; // Set bit 4 in byte[3]
  1614. }
  1615. if (keep == 0x0E)
  1616. {
  1617. c = (ins->oprs[2].offset) & 0x1F;
  1618. // #imm
  1619. bytes[2] |= c >> 1;
  1620. if (c & 0x01)
  1621. {
  1622. bytes[3] |= 0x80;
  1623. }
  1624. c = 0; // Clr bit 4 in byte[3]
  1625. }
  1626. // <shift>
  1627. c |= shiftval (&ins->oprs[2]) << 5;
  1628. bytes[3] |= c;
  1629. }
  1630. // reg,imm
  1631. if (keep == 0x0F)
  1632. {
  1633. int shimm;
  1634. shimm = imm_shift (ins->oprs[1].offset);
  1635. if (shimm == -1)
  1636. {
  1637. errfunc (ERR_NONFATAL, "cannot create that constant");
  1638. }
  1639. bytes[3] = shimm & 0xFF;
  1640. bytes[2] |= (shimm & 0xF00) >> 8;
  1641. }
  1642. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1643. return;
  1644. case 0x10: // MRS Rd,<psr>
  1645. ++codes;
  1646. bytes[0] = c | *codes++;
  1647. bytes[1] = *codes++;
  1648. // Rd
  1649. c = regval (&ins->oprs[0],1);
  1650. bytes[2] = c << 4;
  1651. bytes[3] = 0;
  1652. c = ins->oprs[1].basereg;
  1653. if (c == R_CPSR || c == R_SPSR)
  1654. {
  1655. if (c == R_SPSR)
  1656. {
  1657. bytes[1] |= 0x40;
  1658. }
  1659. }
  1660. else
  1661. {
  1662. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1663. }
  1664. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1665. return;
  1666. case 0x11: // MSR <psr>,Rm
  1667. case 0x12: // MSR <psrf>,Rm
  1668. case 0x13: // MSR <psrf>,#expression
  1669. ++codes;
  1670. bytes[0] = c | *codes++;
  1671. bytes[1] = *codes++;
  1672. bytes[2] = *codes;
  1673. if (keep == 0x11 || keep == 0x12)
  1674. {
  1675. // Rm
  1676. c = regval (&ins->oprs[1],1);
  1677. bytes[3] = c;
  1678. }
  1679. else
  1680. {
  1681. int shimm;
  1682. shimm = imm_shift (ins->oprs[1].offset);
  1683. if (shimm == -1)
  1684. {
  1685. errfunc (ERR_NONFATAL, "cannot create that constant");
  1686. }
  1687. bytes[3] = shimm & 0xFF;
  1688. bytes[2] |= (shimm & 0xF00) >> 8;
  1689. }
  1690. c = ins->oprs[0].basereg;
  1691. if ( keep == 0x11)
  1692. {
  1693. if ( c == R_CPSR || c == R_SPSR)
  1694. {
  1695. if ( c== R_SPSR)
  1696. {
  1697. bytes[1] |= 0x40;
  1698. }
  1699. }
  1700. else
  1701. {
  1702. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1703. }
  1704. }
  1705. else
  1706. {
  1707. if ( c == R_CPSR_FLG || c == R_SPSR_FLG)
  1708. {
  1709. if ( c== R_SPSR_FLG)
  1710. {
  1711. bytes[1] |= 0x40;
  1712. }
  1713. }
  1714. else
  1715. {
  1716. errfunc (ERR_NONFATAL, "CPSR_flg or SPSR_flg expected");
  1717. }
  1718. }
  1719. break;
  1720. case 0x14: // MUL Rd,Rm,Rs
  1721. case 0x15: // MULA Rd,Rm,Rs,Rn
  1722. ++codes;
  1723. bytes[0] = c | *codes++;
  1724. bytes[1] = *codes++;
  1725. bytes[3] = *codes;
  1726. // Rd
  1727. bytes[1] |= regval (&ins->oprs[0],1);
  1728. if (has_S_code)
  1729. bytes[1] |= 0x10;
  1730. // Rm
  1731. bytes[3] |= regval (&ins->oprs[1],1);
  1732. // Rs
  1733. bytes[2] = regval (&ins->oprs[2],1);
  1734. if (keep == 0x15)
  1735. {
  1736. bytes[2] |= regval (&ins->oprs[3],1) << 4;
  1737. }
  1738. break;
  1739. case 0x16: // SMLAL RdHi,RdLo,Rm,Rs
  1740. ++codes;
  1741. bytes[0] = c | *codes++;
  1742. bytes[1] = *codes++;
  1743. bytes[3] = *codes;
  1744. // RdHi
  1745. bytes[1] |= regval (&ins->oprs[1],1);
  1746. if (has_S_code)
  1747. bytes[1] |= 0x10;
  1748. // RdLo
  1749. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1750. // Rm
  1751. bytes[3] |= regval (&ins->oprs[2],1);
  1752. // Rs
  1753. bytes[2] |= regval (&ins->oprs[3],1);
  1754. break;
  1755. case 0x17: // LDR Rd, expression
  1756. ++codes;
  1757. bytes[0] = c | *codes++;
  1758. bytes[1] = *codes++;
  1759. // Rd
  1760. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1761. if (has_B_code)
  1762. bytes[1] |= 0x40;
  1763. if (has_T_code)
  1764. {
  1765. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1766. }
  1767. if (has_W_code)
  1768. {
  1769. errfunc (ERR_NONFATAL, "'!' not allowed");
  1770. }
  1771. // Rn - implicit R15
  1772. bytes[1] |= 0xF;
  1773. if (ins->oprs[1].segment != segment)
  1774. {
  1775. errfunc (ERR_NONFATAL, "label not in same segment");
  1776. }
  1777. data = ins->oprs[1].offset - (offset + 8);
  1778. if (data < 0)
  1779. {
  1780. data = -data;
  1781. }
  1782. else
  1783. {
  1784. bytes[1] |= 0x80;
  1785. }
  1786. if (data >= 0x1000)
  1787. {
  1788. errfunc (ERR_NONFATAL, "too long offset");
  1789. }
  1790. bytes[2] |= ((data & 0xF00) >> 8);
  1791. bytes[3] = data & 0xFF;
  1792. break;
  1793. case 0x18: // LDR Rd, [Rn]
  1794. ++codes;
  1795. bytes[0] = c | *codes++;
  1796. bytes[1] = *codes++;
  1797. // Rd
  1798. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1799. if (has_B_code)
  1800. bytes[1] |= 0x40;
  1801. if (has_T_code)
  1802. {
  1803. bytes[1] |= 0x20; // write-back
  1804. }
  1805. else
  1806. {
  1807. bytes[0] |= 0x01; // implicit pre-index mode
  1808. }
  1809. if (has_W_code)
  1810. {
  1811. bytes[1] |= 0x20; // write-back
  1812. }
  1813. // Rn
  1814. c = regval (&ins->oprs[1],1);
  1815. bytes[1] |= c;
  1816. if (c == 0x15) // R15
  1817. data = -8;
  1818. else
  1819. data = 0;
  1820. if (data < 0)
  1821. {
  1822. data = -data;
  1823. }
  1824. else
  1825. {
  1826. bytes[1] |= 0x80;
  1827. }
  1828. bytes[2] |= ((data & 0xF00) >> 8);
  1829. bytes[3] = data & 0xFF;
  1830. break;
  1831. case 0x19: // LDR Rd, [Rn,#expression]
  1832. case 0x20: // LDR Rd, [Rn,Rm]
  1833. case 0x21: // LDR Rd, [Rn,Rm,shift]
  1834. ++codes;
  1835. bytes[0] = c | *codes++;
  1836. bytes[1] = *codes++;
  1837. // Rd
  1838. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1839. if (has_B_code)
  1840. bytes[1] |= 0x40;
  1841. // Rn
  1842. c = regval (&ins->oprs[1],1);
  1843. bytes[1] |= c;
  1844. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1845. {
  1846. bytes[0] |= 0x01; // pre-index mode
  1847. if (has_W_code)
  1848. {
  1849. bytes[1] |= 0x20;
  1850. }
  1851. if (has_T_code)
  1852. {
  1853. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1854. }
  1855. }
  1856. else
  1857. {
  1858. if (has_T_code) // Forced write-back in post-index mode
  1859. {
  1860. bytes[1] |= 0x20;
  1861. }
  1862. if (has_W_code)
  1863. {
  1864. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1865. }
  1866. }
  1867. if (keep == 0x19)
  1868. {
  1869. data = ins->oprs[2].offset;
  1870. if (data < 0)
  1871. {
  1872. data = -data;
  1873. }
  1874. else
  1875. {
  1876. bytes[1] |= 0x80;
  1877. }
  1878. if (data >= 0x1000)
  1879. {
  1880. errfunc (ERR_NONFATAL, "too long offset");
  1881. }
  1882. bytes[2] |= ((data & 0xF00) >> 8);
  1883. bytes[3] = data & 0xFF;
  1884. }
  1885. else
  1886. {
  1887. if (ins->oprs[2].minus == 0)
  1888. {
  1889. bytes[1] |= 0x80;
  1890. }
  1891. c = regval (&ins->oprs[2],1);
  1892. bytes[3] = c;
  1893. if (keep == 0x21)
  1894. {
  1895. c = ins->oprs[3].offset;
  1896. if (c > 0x1F)
  1897. {
  1898. errfunc (ERR_NONFATAL, "too large shiftvalue");
  1899. c = c & 0x1F;
  1900. }
  1901. bytes[2] |= c >> 1;
  1902. if (c & 0x01)
  1903. {
  1904. bytes[3] |= 0x80;
  1905. }
  1906. bytes[3] |= shiftval (&ins->oprs[3]) << 5;
  1907. }
  1908. }
  1909. break;
  1910. case 0x22: // LDRH Rd, expression
  1911. ++codes;
  1912. bytes[0] = c | 0x01; // Implicit pre-index
  1913. bytes[1] = *codes++;
  1914. // Rd
  1915. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1916. // Rn - implicit R15
  1917. bytes[1] |= 0xF;
  1918. if (ins->oprs[1].segment != segment)
  1919. {
  1920. errfunc (ERR_NONFATAL, "label not in same segment");
  1921. }
  1922. data = ins->oprs[1].offset - (offset + 8);
  1923. if (data < 0)
  1924. {
  1925. data = -data;
  1926. }
  1927. else
  1928. {
  1929. bytes[1] |= 0x80;
  1930. }
  1931. if (data >= 0x100)
  1932. {
  1933. errfunc (ERR_NONFATAL, "too long offset");
  1934. }
  1935. bytes[3] = *codes++;
  1936. bytes[2] |= ((data & 0xF0) >> 4);
  1937. bytes[3] |= data & 0xF;
  1938. break;
  1939. case 0x23: // LDRH Rd, Rn
  1940. ++codes;
  1941. bytes[0] = c | 0x01; // Implicit pre-index
  1942. bytes[1] = *codes++;
  1943. // Rd
  1944. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1945. // Rn
  1946. c = regval (&ins->oprs[1],1);
  1947. bytes[1] |= c;
  1948. if (c == 0x15) // R15
  1949. data = -8;
  1950. else
  1951. data = 0;
  1952. if (data < 0)
  1953. {
  1954. data = -data;
  1955. }
  1956. else
  1957. {
  1958. bytes[1] |= 0x80;
  1959. }
  1960. if (data >= 0x100)
  1961. {
  1962. errfunc (ERR_NONFATAL, "too long offset");
  1963. }
  1964. bytes[3] = *codes++;
  1965. bytes[2] |= ((data & 0xF0) >> 4);
  1966. bytes[3] |= data & 0xF;
  1967. break;
  1968. case 0x24: // LDRH Rd, Rn, expression
  1969. case 0x25: // LDRH Rd, Rn, Rm
  1970. ++codes;
  1971. bytes[0] = c;
  1972. bytes[1] = *codes++;
  1973. // Rd
  1974. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1975. // Rn
  1976. c = regval (&ins->oprs[1],1);
  1977. bytes[1] |= c;
  1978. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1979. {
  1980. bytes[0] |= 0x01; // pre-index mode
  1981. if (has_W_code)
  1982. {
  1983. bytes[1] |= 0x20;
  1984. }
  1985. }
  1986. else
  1987. {
  1988. if (has_W_code)
  1989. {
  1990. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1991. }
  1992. }
  1993. bytes[3] = *codes++;
  1994. if (keep == 0x24)
  1995. {
  1996. data = ins->oprs[2].offset;
  1997. if (data < 0)
  1998. {
  1999. data = -data;
  2000. }
  2001. else
  2002. {
  2003. bytes[1] |= 0x80;
  2004. }
  2005. if (data >= 0x100)
  2006. {
  2007. errfunc (ERR_NONFATAL, "too long offset");
  2008. }
  2009. bytes[2] |= ((data & 0xF0) >> 4);
  2010. bytes[3] |= data & 0xF;
  2011. }
  2012. else
  2013. {
  2014. if (ins->oprs[2].minus == 0)
  2015. {
  2016. bytes[1] |= 0x80;
  2017. }
  2018. c = regval (&ins->oprs[2],1);
  2019. bytes[3] |= c;
  2020. }
  2021. break;
  2022. case 0x26: // LDM/STM Rn, {reg-list}
  2023. ++codes;
  2024. bytes[0] = c;
  2025. bytes[0] |= ( *codes >> 4) & 0xF;
  2026. bytes[1] = ( *codes << 4) & 0xF0;
  2027. ++codes;
  2028. if (has_W_code)
  2029. {
  2030. bytes[1] |= 0x20;
  2031. }
  2032. if (has_F_code)
  2033. {
  2034. bytes[1] |= 0x40;
  2035. }
  2036. // Rn
  2037. bytes[1] |= regval (&ins->oprs[0],1);
  2038. data = ins->oprs[1].basereg;
  2039. bytes[2] = ((data >> 8) & 0xFF);
  2040. bytes[3] = (data & 0xFF);
  2041. break;
  2042. case 0x27: // SWP Rd, Rm, [Rn]
  2043. ++codes;
  2044. bytes[0] = c;
  2045. bytes[0] |= *codes++;
  2046. bytes[1] = regval (&ins->oprs[2],1);
  2047. if (has_B_code)
  2048. {
  2049. bytes[1] |= 0x40;
  2050. }
  2051. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2052. bytes[3] = *codes++;
  2053. bytes[3] |= regval (&ins->oprs[1],1);
  2054. break;
  2055. default:
  2056. errfunc (ERR_FATAL, "unknown decoding of instruction");
  2057. bytes[0] = c;
  2058. // And a fix nibble
  2059. ++codes;
  2060. bytes[0] |= *codes++;
  2061. if ( *codes == 0x01) // An I bit
  2062. {
  2063. }
  2064. if ( *codes == 0x02) // An I bit
  2065. {
  2066. }
  2067. ++codes;
  2068. }
  2069. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2070. }
  2071. *)
  2072. {$endif dummy
  2073. }