aasmcpu.pas 76 KB

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