aasmcpu.pas 74 KB

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