aasmcpu.pas 74 KB

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