aasmcpu.pas 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414
  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 : tai;
  515. curdata : TAsmList;
  516. l : tasmlabel;
  517. begin
  518. curdata:=TAsmList.create;
  519. lastpos:=-1;
  520. curpos:=0;
  521. curtai:=tai(list.first);
  522. while assigned(curtai) do
  523. begin
  524. { instruction? }
  525. if curtai.typ=ait_instruction then
  526. begin
  527. { walk through all operand of the instruction }
  528. for curop:=0 to taicpu(curtai).ops-1 do
  529. begin
  530. { reference? }
  531. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  532. begin
  533. { pc relative symbol? }
  534. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  535. if assigned(curdatatai) then
  536. begin
  537. { if yes, insert till next symbol }
  538. repeat
  539. hp:=tai(curdatatai.next);
  540. listtoinsert.remove(curdatatai);
  541. curdata.concat(curdatatai);
  542. curdatatai:=hp;
  543. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  544. if lastpos=-1 then
  545. lastpos:=curpos;
  546. end;
  547. end;
  548. end;
  549. inc(curpos);
  550. end;
  551. { split only at real instructions else the test below fails }
  552. if ((curpos-lastpos)>1016) and (curtai.typ=ait_instruction) and
  553. (
  554. { don't split loads of pc to lr and the following move }
  555. not(
  556. (taicpu(curtai).opcode=A_MOV) and
  557. (taicpu(curtai).oper[0]^.typ=top_reg) and
  558. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  559. (taicpu(curtai).oper[1]^.typ=top_reg) and
  560. (taicpu(curtai).oper[1]^.reg=NR_PC)
  561. )
  562. ) then
  563. begin
  564. lastpos:=curpos;
  565. hp:=tai(curtai.next);
  566. current_asmdata.getjumplabel(l);
  567. curdata.insert(taicpu.op_sym(A_B,l));
  568. curdata.concat(tai_label.create(l));
  569. list.insertlistafter(curtai,curdata);
  570. curtai:=hp;
  571. end
  572. else
  573. curtai:=tai(curtai.next);
  574. end;
  575. list.concatlist(curdata);
  576. curdata.free;
  577. end;
  578. (*
  579. Floating point instruction format information, taken from the linux kernel
  580. ARM Floating Point Instruction Classes
  581. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  582. |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
  583. |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)
  584. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  585. |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
  586. |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
  587. |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
  588. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  589. CPDT data transfer instructions
  590. LDF, STF, LFM (copro 2), SFM (copro 2)
  591. CPDO dyadic arithmetic instructions
  592. ADF, MUF, SUF, RSF, DVF, RDF,
  593. POW, RPW, RMF, FML, FDV, FRD, POL
  594. CPDO monadic arithmetic instructions
  595. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  596. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  597. CPRT joint arithmetic/data transfer instructions
  598. FIX (arithmetic followed by load/store)
  599. FLT (load/store followed by arithmetic)
  600. CMF, CNF CMFE, CNFE (comparisons)
  601. WFS, RFS (write/read floating point status register)
  602. WFC, RFC (write/read floating point control register)
  603. cond condition codes
  604. P pre/post index bit: 0 = postindex, 1 = preindex
  605. U up/down bit: 0 = stack grows down, 1 = stack grows up
  606. W write back bit: 1 = update base register (Rn)
  607. L load/store bit: 0 = store, 1 = load
  608. Rn base register
  609. Rd destination/source register
  610. Fd floating point destination register
  611. Fn floating point source register
  612. Fm floating point source register or floating point constant
  613. uv transfer length (TABLE 1)
  614. wx register count (TABLE 2)
  615. abcd arithmetic opcode (TABLES 3 & 4)
  616. ef destination size (rounding precision) (TABLE 5)
  617. gh rounding mode (TABLE 6)
  618. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  619. i constant bit: 1 = constant (TABLE 6)
  620. */
  621. /*
  622. TABLE 1
  623. +-------------------------+---+---+---------+---------+
  624. | Precision | u | v | FPSR.EP | length |
  625. +-------------------------+---+---+---------+---------+
  626. | Single | 0 | 0 | x | 1 words |
  627. | Double | 1 | 1 | x | 2 words |
  628. | Extended | 1 | 1 | x | 3 words |
  629. | Packed decimal | 1 | 1 | 0 | 3 words |
  630. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  631. +-------------------------+---+---+---------+---------+
  632. Note: x = don't care
  633. */
  634. /*
  635. TABLE 2
  636. +---+---+---------------------------------+
  637. | w | x | Number of registers to transfer |
  638. +---+---+---------------------------------+
  639. | 0 | 1 | 1 |
  640. | 1 | 0 | 2 |
  641. | 1 | 1 | 3 |
  642. | 0 | 0 | 4 |
  643. +---+---+---------------------------------+
  644. */
  645. /*
  646. TABLE 3: Dyadic Floating Point Opcodes
  647. +---+---+---+---+----------+-----------------------+-----------------------+
  648. | a | b | c | d | Mnemonic | Description | Operation |
  649. +---+---+---+---+----------+-----------------------+-----------------------+
  650. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  651. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  652. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  653. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  654. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  655. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  656. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  657. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  658. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  659. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  660. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  661. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  662. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  663. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  664. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  665. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  666. +---+---+---+---+----------+-----------------------+-----------------------+
  667. Note: POW, RPW, POL are deprecated, and are available for backwards
  668. compatibility only.
  669. */
  670. /*
  671. TABLE 4: Monadic Floating Point Opcodes
  672. +---+---+---+---+----------+-----------------------+-----------------------+
  673. | a | b | c | d | Mnemonic | Description | Operation |
  674. +---+---+---+---+----------+-----------------------+-----------------------+
  675. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  676. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  677. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  678. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  679. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  680. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  681. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  682. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  683. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  684. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  685. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  686. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  687. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  688. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  689. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  690. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  691. +---+---+---+---+----------+-----------------------+-----------------------+
  692. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  693. available for backwards compatibility only.
  694. */
  695. /*
  696. TABLE 5
  697. +-------------------------+---+---+
  698. | Rounding Precision | e | f |
  699. +-------------------------+---+---+
  700. | IEEE Single precision | 0 | 0 |
  701. | IEEE Double precision | 0 | 1 |
  702. | IEEE Extended precision | 1 | 0 |
  703. | undefined (trap) | 1 | 1 |
  704. +-------------------------+---+---+
  705. */
  706. /*
  707. TABLE 5
  708. +---------------------------------+---+---+
  709. | Rounding Mode | g | h |
  710. +---------------------------------+---+---+
  711. | Round to nearest (default) | 0 | 0 |
  712. | Round toward plus infinity | 0 | 1 |
  713. | Round toward negative infinity | 1 | 0 |
  714. | Round toward zero | 1 | 1 |
  715. +---------------------------------+---+---+
  716. *)
  717. function taicpu.GetString:string;
  718. var
  719. i : longint;
  720. s : string;
  721. addsize : boolean;
  722. begin
  723. s:='['+gas_op2str[opcode];
  724. for i:=0 to ops-1 do
  725. begin
  726. with oper[i]^ do
  727. begin
  728. if i=0 then
  729. s:=s+' '
  730. else
  731. s:=s+',';
  732. { type }
  733. addsize:=false;
  734. if (ot and OT_VREG)=OT_VREG then
  735. s:=s+'vreg'
  736. else
  737. if (ot and OT_FPUREG)=OT_FPUREG then
  738. s:=s+'fpureg'
  739. else
  740. if (ot and OT_REGISTER)=OT_REGISTER then
  741. begin
  742. s:=s+'reg';
  743. addsize:=true;
  744. end
  745. else
  746. if (ot and OT_REGLIST)=OT_REGLIST then
  747. begin
  748. s:=s+'reglist';
  749. addsize:=false;
  750. end
  751. else
  752. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  753. begin
  754. s:=s+'imm';
  755. addsize:=true;
  756. end
  757. else
  758. if (ot and OT_MEMORY)=OT_MEMORY then
  759. begin
  760. s:=s+'mem';
  761. addsize:=true;
  762. if (ot and OT_AM2)<>0 then
  763. s:=s+' am2 ';
  764. end
  765. else
  766. s:=s+'???';
  767. { size }
  768. if addsize then
  769. begin
  770. if (ot and OT_BITS8)<>0 then
  771. s:=s+'8'
  772. else
  773. if (ot and OT_BITS16)<>0 then
  774. s:=s+'24'
  775. else
  776. if (ot and OT_BITS32)<>0 then
  777. s:=s+'32'
  778. else
  779. if (ot and OT_BITSSHIFTER)<>0 then
  780. s:=s+'shifter'
  781. else
  782. s:=s+'??';
  783. { signed }
  784. if (ot and OT_SIGNED)<>0 then
  785. s:=s+'s';
  786. end;
  787. end;
  788. end;
  789. GetString:=s+']';
  790. end;
  791. procedure taicpu.ResetPass1;
  792. begin
  793. { we need to reset everything here, because the choosen insentry
  794. can be invalid for a new situation where the previously optimized
  795. insentry is not correct }
  796. InsEntry:=nil;
  797. InsSize:=0;
  798. LastInsOffset:=-1;
  799. end;
  800. procedure taicpu.ResetPass2;
  801. begin
  802. { we are here in a second pass, check if the instruction can be optimized }
  803. if assigned(InsEntry) and
  804. ((InsEntry^.flags and IF_PASS2)<>0) then
  805. begin
  806. InsEntry:=nil;
  807. InsSize:=0;
  808. end;
  809. LastInsOffset:=-1;
  810. end;
  811. function taicpu.CheckIfValid:boolean;
  812. begin
  813. end;
  814. function taicpu.Pass1(objdata:TObjData):longint;
  815. var
  816. ldr2op : array[PF_B..PF_T] of tasmop = (
  817. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  818. str2op : array[PF_B..PF_T] of tasmop = (
  819. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  820. begin
  821. Pass1:=0;
  822. { Save the old offset and set the new offset }
  823. InsOffset:=ObjData.CurrObjSec.Size;
  824. { Error? }
  825. if (Insentry=nil) and (InsSize=-1) then
  826. exit;
  827. { set the file postion }
  828. aktfilepos:=fileinfo;
  829. { tranlate LDR+postfix to complete opcode }
  830. if (opcode=A_LDR) and (oppostfix<>PF_None) then
  831. begin
  832. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  833. opcode:=ldr2op[oppostfix]
  834. else
  835. internalerror(2005091001);
  836. if opcode=A_None then
  837. internalerror(2005091004);
  838. { postfix has been added to opcode }
  839. oppostfix:=PF_None;
  840. end
  841. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  842. begin
  843. if (oppostfix in [low(str2op)..high(str2op)]) then
  844. opcode:=str2op[oppostfix]
  845. else
  846. internalerror(2005091002);
  847. if opcode=A_None then
  848. internalerror(2005091003);
  849. { postfix has been added to opcode }
  850. oppostfix:=PF_None;
  851. end;
  852. { Get InsEntry }
  853. if FindInsEntry(objdata) then
  854. begin
  855. InsSize:=4;
  856. LastInsOffset:=InsOffset;
  857. Pass1:=InsSize;
  858. exit;
  859. end;
  860. LastInsOffset:=-1;
  861. end;
  862. procedure taicpu.Pass2(objdata:TObjData);
  863. begin
  864. { error in pass1 ? }
  865. if insentry=nil then
  866. exit;
  867. aktfilepos:=fileinfo;
  868. { Generate the instruction }
  869. GenCode(objdata);
  870. end;
  871. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  872. begin
  873. end;
  874. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  875. begin
  876. end;
  877. procedure taicpu.ppubuildderefimploper(var o:toper);
  878. begin
  879. end;
  880. procedure taicpu.ppuderefoper(var o:toper);
  881. begin
  882. end;
  883. function taicpu.InsEnd:longint;
  884. begin
  885. end;
  886. procedure taicpu.create_ot(objdata:TObjData);
  887. var
  888. i,l,relsize : longint;
  889. dummy : byte;
  890. currsym : TObjSymbol;
  891. begin
  892. if ops=0 then
  893. exit;
  894. { update oper[].ot field }
  895. for i:=0 to ops-1 do
  896. with oper[i]^ do
  897. begin
  898. case typ of
  899. top_regset:
  900. begin
  901. ot:=OT_REGLIST;
  902. end;
  903. top_reg :
  904. begin
  905. case getregtype(reg) of
  906. R_INTREGISTER:
  907. ot:=OT_REG32 or OT_SHIFTEROP;
  908. R_FPUREGISTER:
  909. ot:=OT_FPUREG;
  910. else
  911. internalerror(2005090901);
  912. end;
  913. end;
  914. top_ref :
  915. begin
  916. if ref^.refaddr=addr_no then
  917. begin
  918. { create ot field }
  919. { we should get the size here dependend on the
  920. instruction }
  921. if (ot and OT_SIZE_MASK)=0 then
  922. ot:=OT_MEMORY or OT_BITS32
  923. else
  924. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  925. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  926. ot:=ot or OT_MEM_OFFS;
  927. { if we need to fix a reference, we do it here }
  928. { pc relative addressing }
  929. if (ref^.base=NR_NO) and
  930. (ref^.index=NR_NO) and
  931. (ref^.shiftmode=SM_None)
  932. { at least we should check if the destination symbol
  933. is in a text section }
  934. { and
  935. (ref^.symbol^.owner="text") } then
  936. ref^.base:=NR_PC;
  937. { determine possible address modes }
  938. if (ref^.base<>NR_NO) and
  939. (
  940. (
  941. (ref^.index=NR_NO) and
  942. (ref^.shiftmode=SM_None) and
  943. (ref^.offset>=-4097) and
  944. (ref^.offset<=4097)
  945. ) or
  946. (
  947. (ref^.shiftmode=SM_None) and
  948. (ref^.offset=0)
  949. ) or
  950. (
  951. (ref^.index<>NR_NO) and
  952. (ref^.shiftmode<>SM_None) and
  953. (ref^.shiftimm<=31) and
  954. (ref^.offset=0)
  955. )
  956. ) then
  957. ot:=ot or OT_AM2;
  958. if (ref^.index<>NR_NO) and
  959. (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  960. (
  961. (ref^.base=NR_NO) and
  962. (ref^.shiftmode=SM_None) and
  963. (ref^.offset=0)
  964. ) then
  965. ot:=ot or OT_AM4;
  966. end
  967. else
  968. begin
  969. l:=ref^.offset;
  970. currsym:=ObjData.symbolref(ref^.symbol);
  971. if assigned(currsym) then
  972. inc(l,currsym.address);
  973. relsize:=(InsOffset+2)-l;
  974. if (relsize<-33554428) or (relsize>33554428) then
  975. ot:=OT_IMM32
  976. else
  977. ot:=OT_IMM24;
  978. end;
  979. end;
  980. top_local :
  981. begin
  982. { we should get the size here dependend on the
  983. instruction }
  984. if (ot and OT_SIZE_MASK)=0 then
  985. ot:=OT_MEMORY or OT_BITS32
  986. else
  987. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  988. end;
  989. top_const :
  990. begin
  991. ot:=OT_IMMEDIATE;
  992. if is_shifter_const(val,dummy) then
  993. ot:=OT_IMMSHIFTER
  994. else
  995. ot:=OT_IMM32
  996. end;
  997. top_none :
  998. begin
  999. { generated when there was an error in the
  1000. assembler reader. It never happends when generating
  1001. assembler }
  1002. end;
  1003. top_shifterop:
  1004. begin
  1005. ot:=OT_SHIFTEROP;
  1006. end;
  1007. else
  1008. internalerror(200402261);
  1009. end;
  1010. end;
  1011. end;
  1012. function taicpu.Matches(p:PInsEntry):longint;
  1013. { * IF_SM stands for Size Match: any operand whose size is not
  1014. * explicitly specified by the template is `really' intended to be
  1015. * the same size as the first size-specified operand.
  1016. * Non-specification is tolerated in the input instruction, but
  1017. * _wrong_ specification is not.
  1018. *
  1019. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1020. * three-operand instructions such as SHLD: it implies that the
  1021. * first two operands must match in size, but that the third is
  1022. * required to be _unspecified_.
  1023. *
  1024. * IF_SB invokes Size Byte: operands with unspecified size in the
  1025. * template are really bytes, and so no non-byte specification in
  1026. * the input instruction will be tolerated. IF_SW similarly invokes
  1027. * Size Word, and IF_SD invokes Size Doubleword.
  1028. *
  1029. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1030. * that any operand with unspecified size in the template is
  1031. * required to have unspecified size in the instruction too...)
  1032. }
  1033. var
  1034. i,j,asize,oprs : longint;
  1035. siz : array[0..3] of longint;
  1036. begin
  1037. Matches:=100;
  1038. writeln(getstring,'---');
  1039. { Check the opcode and operands }
  1040. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1041. begin
  1042. Matches:=0;
  1043. exit;
  1044. end;
  1045. { Check that no spurious colons or TOs are present }
  1046. for i:=0 to p^.ops-1 do
  1047. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  1048. begin
  1049. Matches:=0;
  1050. exit;
  1051. end;
  1052. { Check that the operand flags all match up }
  1053. for i:=0 to p^.ops-1 do
  1054. begin
  1055. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  1056. ((p^.optypes[i] and OT_SIZE_MASK) and
  1057. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  1058. begin
  1059. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  1060. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  1061. begin
  1062. Matches:=0;
  1063. exit;
  1064. end
  1065. else
  1066. Matches:=1;
  1067. end;
  1068. end;
  1069. { check postfixes:
  1070. the existance of a certain postfix requires a
  1071. particular code }
  1072. { update condition flags
  1073. or floating point single }
  1074. if (oppostfix=PF_S) and
  1075. not(p^.code[0] in [#$04]) then
  1076. begin
  1077. Matches:=0;
  1078. exit;
  1079. end;
  1080. { floating point size }
  1081. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  1082. not(p^.code[0] in []) then
  1083. begin
  1084. Matches:=0;
  1085. exit;
  1086. end;
  1087. { multiple load/store address modes }
  1088. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1089. not(p^.code[0] in [
  1090. // ldr,str,ldrb,strb
  1091. #$17,
  1092. // stm,ldm
  1093. #$26
  1094. ]) then
  1095. begin
  1096. Matches:=0;
  1097. exit;
  1098. end;
  1099. { we shouldn't see any opsize prefixes here }
  1100. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  1101. begin
  1102. Matches:=0;
  1103. exit;
  1104. end;
  1105. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  1106. begin
  1107. Matches:=0;
  1108. exit;
  1109. end;
  1110. { Check operand sizes }
  1111. { as default an untyped size can get all the sizes, this is different
  1112. from nasm, but else we need to do a lot checking which opcodes want
  1113. size or not with the automatic size generation }
  1114. asize:=longint($ffffffff);
  1115. (*
  1116. if (p^.flags and IF_SB)<>0 then
  1117. asize:=OT_BITS8
  1118. else if (p^.flags and IF_SW)<>0 then
  1119. asize:=OT_BITS16
  1120. else if (p^.flags and IF_SD)<>0 then
  1121. asize:=OT_BITS32;
  1122. if (p^.flags and IF_ARMASK)<>0 then
  1123. begin
  1124. siz[0]:=0;
  1125. siz[1]:=0;
  1126. siz[2]:=0;
  1127. if (p^.flags and IF_AR0)<>0 then
  1128. siz[0]:=asize
  1129. else if (p^.flags and IF_AR1)<>0 then
  1130. siz[1]:=asize
  1131. else if (p^.flags and IF_AR2)<>0 then
  1132. siz[2]:=asize;
  1133. end
  1134. else
  1135. begin
  1136. { we can leave because the size for all operands is forced to be
  1137. the same
  1138. but not if IF_SB IF_SW or IF_SD is set PM }
  1139. if asize=-1 then
  1140. exit;
  1141. siz[0]:=asize;
  1142. siz[1]:=asize;
  1143. siz[2]:=asize;
  1144. end;
  1145. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1146. begin
  1147. if (p^.flags and IF_SM2)<>0 then
  1148. oprs:=2
  1149. else
  1150. oprs:=p^.ops;
  1151. for i:=0 to oprs-1 do
  1152. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1153. begin
  1154. for j:=0 to oprs-1 do
  1155. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1156. break;
  1157. end;
  1158. end
  1159. else
  1160. oprs:=2;
  1161. { Check operand sizes }
  1162. for i:=0 to p^.ops-1 do
  1163. begin
  1164. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1165. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1166. { Immediates can always include smaller size }
  1167. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1168. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1169. Matches:=2;
  1170. end;
  1171. *)
  1172. end;
  1173. function taicpu.calcsize(p:PInsEntry):shortint;
  1174. begin
  1175. result:=4;
  1176. end;
  1177. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1178. begin
  1179. end;
  1180. procedure taicpu.Swapoperands;
  1181. begin
  1182. end;
  1183. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1184. var
  1185. i : longint;
  1186. begin
  1187. result:=false;
  1188. { Things which may only be done once, not when a second pass is done to
  1189. optimize }
  1190. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1191. begin
  1192. { create the .ot fields }
  1193. create_ot(objdata);
  1194. { set the file postion }
  1195. aktfilepos:=fileinfo;
  1196. end
  1197. else
  1198. begin
  1199. { we've already an insentry so it's valid }
  1200. result:=true;
  1201. exit;
  1202. end;
  1203. { Lookup opcode in the table }
  1204. InsSize:=-1;
  1205. i:=instabcache^[opcode];
  1206. if i=-1 then
  1207. begin
  1208. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1209. exit;
  1210. end;
  1211. insentry:=@instab[i];
  1212. while (insentry^.opcode=opcode) do
  1213. begin
  1214. if matches(insentry)=100 then
  1215. begin
  1216. result:=true;
  1217. exit;
  1218. end;
  1219. inc(i);
  1220. insentry:=@instab[i];
  1221. end;
  1222. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1223. { No instruction found, set insentry to nil and inssize to -1 }
  1224. insentry:=nil;
  1225. inssize:=-1;
  1226. end;
  1227. procedure taicpu.gencode(objdata:TObjData);
  1228. var
  1229. bytes : dword;
  1230. i_field : byte;
  1231. procedure setshifterop(op : byte);
  1232. begin
  1233. case oper[op]^.typ of
  1234. top_const:
  1235. begin
  1236. i_field:=1;
  1237. bytes:=bytes or (oper[op]^.val and $fff);
  1238. end;
  1239. top_reg:
  1240. begin
  1241. i_field:=0;
  1242. bytes:=bytes or (getsupreg(oper[op]^.reg) shl 16);
  1243. { does a real shifter op follow? }
  1244. if (op+1<=op) and (oper[op+1]^.typ=top_shifterop) then
  1245. begin
  1246. end;
  1247. end;
  1248. else
  1249. internalerror(2005091103);
  1250. end;
  1251. end;
  1252. begin
  1253. bytes:=$0;
  1254. { evaluate and set condition code }
  1255. { condition code allowed? }
  1256. { setup rest of the instruction }
  1257. case insentry^.code[0] of
  1258. #$08:
  1259. begin
  1260. { set instruction code }
  1261. bytes:=bytes or (ord(insentry^.code[1]) shl 26);
  1262. bytes:=bytes or (ord(insentry^.code[2]) shl 21);
  1263. { set destination }
  1264. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  1265. { create shifter op }
  1266. setshifterop(1);
  1267. { set i field }
  1268. bytes:=bytes or (i_field shl 25);
  1269. { set s if necessary }
  1270. if oppostfix=PF_S then
  1271. bytes:=bytes or (1 shl 20);
  1272. end;
  1273. #$ff:
  1274. internalerror(2005091101);
  1275. else
  1276. internalerror(2005091102);
  1277. end;
  1278. { we're finished, write code }
  1279. objdata.writebytes(bytes,sizeof(bytes));
  1280. end;
  1281. end.
  1282. {$ifdef dummy}
  1283. (*
  1284. static void gencode (long segment, long offset, int bits,
  1285. insn *ins, char *codes, long insn_end)
  1286. {
  1287. int has_S_code; /* S - setflag */
  1288. int has_B_code; /* B - setflag */
  1289. int has_T_code; /* T - setflag */
  1290. int has_W_code; /* ! => W flag */
  1291. int has_F_code; /* ^ => S flag */
  1292. int keep;
  1293. unsigned char c;
  1294. unsigned char bytes[4];
  1295. long data, size;
  1296. static int cc_code[] = /* bit pattern of cc */
  1297. { /* order as enum in */
  1298. 0x0E, 0x03, 0x02, 0x00, /* nasm.h */
  1299. 0x0A, 0x0C, 0x08, 0x0D,
  1300. 0x09, 0x0B, 0x04, 0x01,
  1301. 0x05, 0x07, 0x06,
  1302. };
  1303. (*
  1304. #ifdef DEBUG
  1305. static char *CC[] =
  1306. { /* condition code names */
  1307. "AL", "CC", "CS", "EQ",
  1308. "GE", "GT", "HI", "LE",
  1309. "LS", "LT", "MI", "NE",
  1310. "PL", "VC", "VS", "",
  1311. "S"
  1312. };
  1313. *)
  1314. has_S_code = (ins->condition & C_SSETFLAG);
  1315. has_B_code = (ins->condition & C_BSETFLAG);
  1316. has_T_code = (ins->condition & C_TSETFLAG);
  1317. has_W_code = (ins->condition & C_EXSETFLAG);
  1318. has_F_code = (ins->condition & C_FSETFLAG);
  1319. ins->condition = (ins->condition & 0x0F);
  1320. (*
  1321. if (rt_debug)
  1322. {
  1323. printf ("gencode: instruction: %s%s", insn_names[ins->opcode],
  1324. CC[ins->condition & 0x0F]);
  1325. if (has_S_code)
  1326. printf ("S");
  1327. if (has_B_code)
  1328. printf ("B");
  1329. if (has_T_code)
  1330. printf ("T");
  1331. if (has_W_code)
  1332. printf ("!");
  1333. if (has_F_code)
  1334. printf ("^");
  1335. printf ("\n");
  1336. c = *codes;
  1337. printf (" (%d) decode - '0x%02X'\n", ins->operands, c);
  1338. bytes[0] = 0xB;
  1339. bytes[1] = 0xE;
  1340. bytes[2] = 0xE;
  1341. bytes[3] = 0xF;
  1342. }
  1343. *)
  1344. // First condition code in upper nibble
  1345. if (ins->condition < C_NONE)
  1346. {
  1347. c = cc_code[ins->condition] << 4;
  1348. }
  1349. else
  1350. {
  1351. c = cc_code[C_AL] << 4; // is often ALWAYS but not always
  1352. }
  1353. switch (keep = *codes)
  1354. {
  1355. case 1:
  1356. // B, BL
  1357. ++codes;
  1358. c |= *codes++;
  1359. bytes[0] = c;
  1360. if (ins->oprs[0].segment != segment)
  1361. {
  1362. // fais une relocation
  1363. c = 1;
  1364. data = 0; // Let the linker locate ??
  1365. }
  1366. else
  1367. {
  1368. c = 0;
  1369. data = ins->oprs[0].offset - (offset + 8);
  1370. if (data % 4)
  1371. {
  1372. errfunc (ERR_NONFATAL, "offset not aligned on 4 bytes");
  1373. }
  1374. }
  1375. if (data >= 0x1000)
  1376. {
  1377. errfunc (ERR_NONFATAL, "too long offset");
  1378. }
  1379. data = data >> 2;
  1380. bytes[1] = (data >> 16) & 0xFF;
  1381. bytes[2] = (data >> 8) & 0xFF;
  1382. bytes[3] = (data ) & 0xFF;
  1383. if (c == 1)
  1384. {
  1385. // out (offset, segment, &bytes[0], OUT_RAWDATA+1, NO_SEG, NO_SEG);
  1386. out (offset, segment, &bytes[0], OUT_REL3ADR+4, ins->oprs[0].segment, NO_SEG);
  1387. }
  1388. else
  1389. {
  1390. out (offset, segment, &bytes[0], OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1391. }
  1392. return;
  1393. case 2:
  1394. // SWI
  1395. ++codes;
  1396. c |= *codes++;
  1397. bytes[0] = c;
  1398. data = ins->oprs[0].offset;
  1399. bytes[1] = (data >> 16) & 0xFF;
  1400. bytes[2] = (data >> 8) & 0xFF;
  1401. bytes[3] = (data) & 0xFF;
  1402. out (offset, segment, &bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1403. return;
  1404. case 3:
  1405. // BX
  1406. ++codes;
  1407. c |= *codes++;
  1408. bytes[0] = c;
  1409. bytes[1] = *codes++;
  1410. bytes[2] = *codes++;
  1411. bytes[3] = *codes++;
  1412. c = regval (&ins->oprs[0],1);
  1413. if (c == 15) // PC
  1414. {
  1415. errfunc (ERR_WARNING, "'BX' with R15 has undefined behaviour");
  1416. }
  1417. else if (c > 15)
  1418. {
  1419. errfunc (ERR_NONFATAL, "Illegal register specified for 'BX'");
  1420. }
  1421. bytes[3] |= (c & 0x0F);
  1422. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1423. return;
  1424. case 4: // AND Rd,Rn,Rm
  1425. case 5: // AND Rd,Rn,Rm,<shift>Rs
  1426. case 6: // AND Rd,Rn,Rm,<shift>imm
  1427. case 7: // AND Rd,Rn,<shift>imm
  1428. ++codes;
  1429. #ifdef DEBUG
  1430. if (rt_debug)
  1431. {
  1432. printf (" decode - '0x%02X'\n", keep);
  1433. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1434. }
  1435. #endif
  1436. bytes[0] = c | *codes;
  1437. ++codes;
  1438. bytes[1] = *codes;
  1439. if (has_S_code)
  1440. bytes[1] |= 0x10;
  1441. c = regval (&ins->oprs[1],1);
  1442. // Rn in low nibble
  1443. bytes[1] |= c;
  1444. // Rd in high nibble
  1445. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1446. if (keep != 7)
  1447. {
  1448. // Rm in low nibble
  1449. bytes[3] = regval (&ins->oprs[2],1);
  1450. }
  1451. // Shifts if any
  1452. if (keep == 5 || keep == 6)
  1453. {
  1454. // Shift in bytes 2 and 3
  1455. if (keep == 5)
  1456. {
  1457. // Rs
  1458. c = regval (&ins->oprs[3],1);
  1459. bytes[2] |= c;
  1460. c = 0x10; // Set bit 4 in byte[3]
  1461. }
  1462. if (keep == 6)
  1463. {
  1464. c = (ins->oprs[3].offset) & 0x1F;
  1465. // #imm
  1466. bytes[2] |= c >> 1;
  1467. if (c & 0x01)
  1468. {
  1469. bytes[3] |= 0x80;
  1470. }
  1471. c = 0; // Clr bit 4 in byte[3]
  1472. }
  1473. // <shift>
  1474. c |= shiftval (&ins->oprs[3]) << 5;
  1475. bytes[3] |= c;
  1476. }
  1477. // reg,reg,imm
  1478. if (keep == 7)
  1479. {
  1480. int shimm;
  1481. shimm = imm_shift (ins->oprs[2].offset);
  1482. if (shimm == -1)
  1483. {
  1484. errfunc (ERR_NONFATAL, "cannot create that constant");
  1485. }
  1486. bytes[3] = shimm & 0xFF;
  1487. bytes[2] |= (shimm & 0xF00) >> 8;
  1488. }
  1489. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1490. return;
  1491. case 8: // MOV Rd,Rm
  1492. case 9: // MOV Rd,Rm,<shift>Rs
  1493. case 0xA: // MOV Rd,Rm,<shift>imm
  1494. case 0xB: // MOV Rd,<shift>imm
  1495. ++codes;
  1496. #ifdef DEBUG
  1497. if (rt_debug)
  1498. {
  1499. printf (" decode - '0x%02X'\n", keep);
  1500. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1501. }
  1502. #endif
  1503. bytes[0] = c | *codes;
  1504. ++codes;
  1505. bytes[1] = *codes;
  1506. if (has_S_code)
  1507. bytes[1] |= 0x10;
  1508. // Rd in high nibble
  1509. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1510. if (keep != 0x0B)
  1511. {
  1512. // Rm in low nibble
  1513. bytes[3] = regval (&ins->oprs[1],1);
  1514. }
  1515. // Shifts if any
  1516. if (keep == 0x09 || keep == 0x0A)
  1517. {
  1518. // Shift in bytes 2 and 3
  1519. if (keep == 0x09)
  1520. {
  1521. // Rs
  1522. c = regval (&ins->oprs[2],1);
  1523. bytes[2] |= c;
  1524. c = 0x10; // Set bit 4 in byte[3]
  1525. }
  1526. if (keep == 0x0A)
  1527. {
  1528. c = (ins->oprs[2].offset) & 0x1F;
  1529. // #imm
  1530. bytes[2] |= c >> 1;
  1531. if (c & 0x01)
  1532. {
  1533. bytes[3] |= 0x80;
  1534. }
  1535. c = 0; // Clr bit 4 in byte[3]
  1536. }
  1537. // <shift>
  1538. c |= shiftval (&ins->oprs[2]) << 5;
  1539. bytes[3] |= c;
  1540. }
  1541. // reg,imm
  1542. if (keep == 0x0B)
  1543. {
  1544. int shimm;
  1545. shimm = imm_shift (ins->oprs[1].offset);
  1546. if (shimm == -1)
  1547. {
  1548. errfunc (ERR_NONFATAL, "cannot create that constant");
  1549. }
  1550. bytes[3] = shimm & 0xFF;
  1551. bytes[2] |= (shimm & 0xF00) >> 8;
  1552. }
  1553. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1554. return;
  1555. case 0xC: // CMP Rn,Rm
  1556. case 0xD: // CMP Rn,Rm,<shift>Rs
  1557. case 0xE: // CMP Rn,Rm,<shift>imm
  1558. case 0xF: // CMP Rn,<shift>imm
  1559. ++codes;
  1560. bytes[0] = c | *codes++;
  1561. bytes[1] = *codes;
  1562. // Implicit S code
  1563. bytes[1] |= 0x10;
  1564. c = regval (&ins->oprs[0],1);
  1565. // Rn in low nibble
  1566. bytes[1] |= c;
  1567. // No destination
  1568. bytes[2] = 0;
  1569. if (keep != 0x0B)
  1570. {
  1571. // Rm in low nibble
  1572. bytes[3] = regval (&ins->oprs[1],1);
  1573. }
  1574. // Shifts if any
  1575. if (keep == 0x0D || keep == 0x0E)
  1576. {
  1577. // Shift in bytes 2 and 3
  1578. if (keep == 0x0D)
  1579. {
  1580. // Rs
  1581. c = regval (&ins->oprs[2],1);
  1582. bytes[2] |= c;
  1583. c = 0x10; // Set bit 4 in byte[3]
  1584. }
  1585. if (keep == 0x0E)
  1586. {
  1587. c = (ins->oprs[2].offset) & 0x1F;
  1588. // #imm
  1589. bytes[2] |= c >> 1;
  1590. if (c & 0x01)
  1591. {
  1592. bytes[3] |= 0x80;
  1593. }
  1594. c = 0; // Clr bit 4 in byte[3]
  1595. }
  1596. // <shift>
  1597. c |= shiftval (&ins->oprs[2]) << 5;
  1598. bytes[3] |= c;
  1599. }
  1600. // reg,imm
  1601. if (keep == 0x0F)
  1602. {
  1603. int shimm;
  1604. shimm = imm_shift (ins->oprs[1].offset);
  1605. if (shimm == -1)
  1606. {
  1607. errfunc (ERR_NONFATAL, "cannot create that constant");
  1608. }
  1609. bytes[3] = shimm & 0xFF;
  1610. bytes[2] |= (shimm & 0xF00) >> 8;
  1611. }
  1612. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1613. return;
  1614. case 0x10: // MRS Rd,<psr>
  1615. ++codes;
  1616. bytes[0] = c | *codes++;
  1617. bytes[1] = *codes++;
  1618. // Rd
  1619. c = regval (&ins->oprs[0],1);
  1620. bytes[2] = c << 4;
  1621. bytes[3] = 0;
  1622. c = ins->oprs[1].basereg;
  1623. if (c == R_CPSR || c == R_SPSR)
  1624. {
  1625. if (c == R_SPSR)
  1626. {
  1627. bytes[1] |= 0x40;
  1628. }
  1629. }
  1630. else
  1631. {
  1632. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1633. }
  1634. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1635. return;
  1636. case 0x11: // MSR <psr>,Rm
  1637. case 0x12: // MSR <psrf>,Rm
  1638. case 0x13: // MSR <psrf>,#expression
  1639. ++codes;
  1640. bytes[0] = c | *codes++;
  1641. bytes[1] = *codes++;
  1642. bytes[2] = *codes;
  1643. if (keep == 0x11 || keep == 0x12)
  1644. {
  1645. // Rm
  1646. c = regval (&ins->oprs[1],1);
  1647. bytes[3] = c;
  1648. }
  1649. else
  1650. {
  1651. int shimm;
  1652. shimm = imm_shift (ins->oprs[1].offset);
  1653. if (shimm == -1)
  1654. {
  1655. errfunc (ERR_NONFATAL, "cannot create that constant");
  1656. }
  1657. bytes[3] = shimm & 0xFF;
  1658. bytes[2] |= (shimm & 0xF00) >> 8;
  1659. }
  1660. c = ins->oprs[0].basereg;
  1661. if ( keep == 0x11)
  1662. {
  1663. if ( c == R_CPSR || c == R_SPSR)
  1664. {
  1665. if ( c== R_SPSR)
  1666. {
  1667. bytes[1] |= 0x40;
  1668. }
  1669. }
  1670. else
  1671. {
  1672. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1673. }
  1674. }
  1675. else
  1676. {
  1677. if ( c == R_CPSR_FLG || c == R_SPSR_FLG)
  1678. {
  1679. if ( c== R_SPSR_FLG)
  1680. {
  1681. bytes[1] |= 0x40;
  1682. }
  1683. }
  1684. else
  1685. {
  1686. errfunc (ERR_NONFATAL, "CPSR_flg or SPSR_flg expected");
  1687. }
  1688. }
  1689. break;
  1690. case 0x14: // MUL Rd,Rm,Rs
  1691. case 0x15: // MULA Rd,Rm,Rs,Rn
  1692. ++codes;
  1693. bytes[0] = c | *codes++;
  1694. bytes[1] = *codes++;
  1695. bytes[3] = *codes;
  1696. // Rd
  1697. bytes[1] |= regval (&ins->oprs[0],1);
  1698. if (has_S_code)
  1699. bytes[1] |= 0x10;
  1700. // Rm
  1701. bytes[3] |= regval (&ins->oprs[1],1);
  1702. // Rs
  1703. bytes[2] = regval (&ins->oprs[2],1);
  1704. if (keep == 0x15)
  1705. {
  1706. bytes[2] |= regval (&ins->oprs[3],1) << 4;
  1707. }
  1708. break;
  1709. case 0x16: // SMLAL RdHi,RdLo,Rm,Rs
  1710. ++codes;
  1711. bytes[0] = c | *codes++;
  1712. bytes[1] = *codes++;
  1713. bytes[3] = *codes;
  1714. // RdHi
  1715. bytes[1] |= regval (&ins->oprs[1],1);
  1716. if (has_S_code)
  1717. bytes[1] |= 0x10;
  1718. // RdLo
  1719. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1720. // Rm
  1721. bytes[3] |= regval (&ins->oprs[2],1);
  1722. // Rs
  1723. bytes[2] |= regval (&ins->oprs[3],1);
  1724. break;
  1725. case 0x17: // LDR Rd, expression
  1726. ++codes;
  1727. bytes[0] = c | *codes++;
  1728. bytes[1] = *codes++;
  1729. // Rd
  1730. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1731. if (has_B_code)
  1732. bytes[1] |= 0x40;
  1733. if (has_T_code)
  1734. {
  1735. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1736. }
  1737. if (has_W_code)
  1738. {
  1739. errfunc (ERR_NONFATAL, "'!' not allowed");
  1740. }
  1741. // Rn - implicit R15
  1742. bytes[1] |= 0xF;
  1743. if (ins->oprs[1].segment != segment)
  1744. {
  1745. errfunc (ERR_NONFATAL, "label not in same segment");
  1746. }
  1747. data = ins->oprs[1].offset - (offset + 8);
  1748. if (data < 0)
  1749. {
  1750. data = -data;
  1751. }
  1752. else
  1753. {
  1754. bytes[1] |= 0x80;
  1755. }
  1756. if (data >= 0x1000)
  1757. {
  1758. errfunc (ERR_NONFATAL, "too long offset");
  1759. }
  1760. bytes[2] |= ((data & 0xF00) >> 8);
  1761. bytes[3] = data & 0xFF;
  1762. break;
  1763. case 0x18: // LDR Rd, [Rn]
  1764. ++codes;
  1765. bytes[0] = c | *codes++;
  1766. bytes[1] = *codes++;
  1767. // Rd
  1768. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1769. if (has_B_code)
  1770. bytes[1] |= 0x40;
  1771. if (has_T_code)
  1772. {
  1773. bytes[1] |= 0x20; // write-back
  1774. }
  1775. else
  1776. {
  1777. bytes[0] |= 0x01; // implicit pre-index mode
  1778. }
  1779. if (has_W_code)
  1780. {
  1781. bytes[1] |= 0x20; // write-back
  1782. }
  1783. // Rn
  1784. c = regval (&ins->oprs[1],1);
  1785. bytes[1] |= c;
  1786. if (c == 0x15) // R15
  1787. data = -8;
  1788. else
  1789. data = 0;
  1790. if (data < 0)
  1791. {
  1792. data = -data;
  1793. }
  1794. else
  1795. {
  1796. bytes[1] |= 0x80;
  1797. }
  1798. bytes[2] |= ((data & 0xF00) >> 8);
  1799. bytes[3] = data & 0xFF;
  1800. break;
  1801. case 0x19: // LDR Rd, [Rn,#expression]
  1802. case 0x20: // LDR Rd, [Rn,Rm]
  1803. case 0x21: // LDR Rd, [Rn,Rm,shift]
  1804. ++codes;
  1805. bytes[0] = c | *codes++;
  1806. bytes[1] = *codes++;
  1807. // Rd
  1808. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1809. if (has_B_code)
  1810. bytes[1] |= 0x40;
  1811. // Rn
  1812. c = regval (&ins->oprs[1],1);
  1813. bytes[1] |= c;
  1814. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1815. {
  1816. bytes[0] |= 0x01; // pre-index mode
  1817. if (has_W_code)
  1818. {
  1819. bytes[1] |= 0x20;
  1820. }
  1821. if (has_T_code)
  1822. {
  1823. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1824. }
  1825. }
  1826. else
  1827. {
  1828. if (has_T_code) // Forced write-back in post-index mode
  1829. {
  1830. bytes[1] |= 0x20;
  1831. }
  1832. if (has_W_code)
  1833. {
  1834. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1835. }
  1836. }
  1837. if (keep == 0x19)
  1838. {
  1839. data = ins->oprs[2].offset;
  1840. if (data < 0)
  1841. {
  1842. data = -data;
  1843. }
  1844. else
  1845. {
  1846. bytes[1] |= 0x80;
  1847. }
  1848. if (data >= 0x1000)
  1849. {
  1850. errfunc (ERR_NONFATAL, "too long offset");
  1851. }
  1852. bytes[2] |= ((data & 0xF00) >> 8);
  1853. bytes[3] = data & 0xFF;
  1854. }
  1855. else
  1856. {
  1857. if (ins->oprs[2].minus == 0)
  1858. {
  1859. bytes[1] |= 0x80;
  1860. }
  1861. c = regval (&ins->oprs[2],1);
  1862. bytes[3] = c;
  1863. if (keep == 0x21)
  1864. {
  1865. c = ins->oprs[3].offset;
  1866. if (c > 0x1F)
  1867. {
  1868. errfunc (ERR_NONFATAL, "too large shiftvalue");
  1869. c = c & 0x1F;
  1870. }
  1871. bytes[2] |= c >> 1;
  1872. if (c & 0x01)
  1873. {
  1874. bytes[3] |= 0x80;
  1875. }
  1876. bytes[3] |= shiftval (&ins->oprs[3]) << 5;
  1877. }
  1878. }
  1879. break;
  1880. case 0x22: // LDRH Rd, expression
  1881. ++codes;
  1882. bytes[0] = c | 0x01; // Implicit pre-index
  1883. bytes[1] = *codes++;
  1884. // Rd
  1885. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1886. // Rn - implicit R15
  1887. bytes[1] |= 0xF;
  1888. if (ins->oprs[1].segment != segment)
  1889. {
  1890. errfunc (ERR_NONFATAL, "label not in same segment");
  1891. }
  1892. data = ins->oprs[1].offset - (offset + 8);
  1893. if (data < 0)
  1894. {
  1895. data = -data;
  1896. }
  1897. else
  1898. {
  1899. bytes[1] |= 0x80;
  1900. }
  1901. if (data >= 0x100)
  1902. {
  1903. errfunc (ERR_NONFATAL, "too long offset");
  1904. }
  1905. bytes[3] = *codes++;
  1906. bytes[2] |= ((data & 0xF0) >> 4);
  1907. bytes[3] |= data & 0xF;
  1908. break;
  1909. case 0x23: // LDRH Rd, Rn
  1910. ++codes;
  1911. bytes[0] = c | 0x01; // Implicit pre-index
  1912. bytes[1] = *codes++;
  1913. // Rd
  1914. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1915. // Rn
  1916. c = regval (&ins->oprs[1],1);
  1917. bytes[1] |= c;
  1918. if (c == 0x15) // R15
  1919. data = -8;
  1920. else
  1921. data = 0;
  1922. if (data < 0)
  1923. {
  1924. data = -data;
  1925. }
  1926. else
  1927. {
  1928. bytes[1] |= 0x80;
  1929. }
  1930. if (data >= 0x100)
  1931. {
  1932. errfunc (ERR_NONFATAL, "too long offset");
  1933. }
  1934. bytes[3] = *codes++;
  1935. bytes[2] |= ((data & 0xF0) >> 4);
  1936. bytes[3] |= data & 0xF;
  1937. break;
  1938. case 0x24: // LDRH Rd, Rn, expression
  1939. case 0x25: // LDRH Rd, Rn, Rm
  1940. ++codes;
  1941. bytes[0] = c;
  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 (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1949. {
  1950. bytes[0] |= 0x01; // pre-index mode
  1951. if (has_W_code)
  1952. {
  1953. bytes[1] |= 0x20;
  1954. }
  1955. }
  1956. else
  1957. {
  1958. if (has_W_code)
  1959. {
  1960. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1961. }
  1962. }
  1963. bytes[3] = *codes++;
  1964. if (keep == 0x24)
  1965. {
  1966. data = ins->oprs[2].offset;
  1967. if (data < 0)
  1968. {
  1969. data = -data;
  1970. }
  1971. else
  1972. {
  1973. bytes[1] |= 0x80;
  1974. }
  1975. if (data >= 0x100)
  1976. {
  1977. errfunc (ERR_NONFATAL, "too long offset");
  1978. }
  1979. bytes[2] |= ((data & 0xF0) >> 4);
  1980. bytes[3] |= data & 0xF;
  1981. }
  1982. else
  1983. {
  1984. if (ins->oprs[2].minus == 0)
  1985. {
  1986. bytes[1] |= 0x80;
  1987. }
  1988. c = regval (&ins->oprs[2],1);
  1989. bytes[3] |= c;
  1990. }
  1991. break;
  1992. case 0x26: // LDM/STM Rn, {reg-list}
  1993. ++codes;
  1994. bytes[0] = c;
  1995. bytes[0] |= ( *codes >> 4) & 0xF;
  1996. bytes[1] = ( *codes << 4) & 0xF0;
  1997. ++codes;
  1998. if (has_W_code)
  1999. {
  2000. bytes[1] |= 0x20;
  2001. }
  2002. if (has_F_code)
  2003. {
  2004. bytes[1] |= 0x40;
  2005. }
  2006. // Rn
  2007. bytes[1] |= regval (&ins->oprs[0],1);
  2008. data = ins->oprs[1].basereg;
  2009. bytes[2] = ((data >> 8) & 0xFF);
  2010. bytes[3] = (data & 0xFF);
  2011. break;
  2012. case 0x27: // SWP Rd, Rm, [Rn]
  2013. ++codes;
  2014. bytes[0] = c;
  2015. bytes[0] |= *codes++;
  2016. bytes[1] = regval (&ins->oprs[2],1);
  2017. if (has_B_code)
  2018. {
  2019. bytes[1] |= 0x40;
  2020. }
  2021. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2022. bytes[3] = *codes++;
  2023. bytes[3] |= regval (&ins->oprs[1],1);
  2024. break;
  2025. default:
  2026. errfunc (ERR_FATAL, "unknown decoding of instruction");
  2027. bytes[0] = c;
  2028. // And a fix nibble
  2029. ++codes;
  2030. bytes[0] |= *codes++;
  2031. if ( *codes == 0x01) // An I bit
  2032. {
  2033. }
  2034. if ( *codes == 0x02) // An I bit
  2035. {
  2036. }
  2037. ++codes;
  2038. }
  2039. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2040. }
  2041. *)
  2042. {$endif dummy
  2043. }