aasmcpu.pas 94 KB

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