aasmcpu.pas 74 KB

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