rautils.pas 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. This unit implements some support routines for assembler parsing
  4. independent of the processor
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. **********************************************************************}
  17. Unit RAUtils;
  18. {$i fpcdefs.inc}
  19. Interface
  20. Uses
  21. cutils,cclasses,
  22. globtype,aasmbase,aasmtai,aasmdata,cpubase,cpuinfo,cgbase,cgutils,
  23. symconst,symbase,symtype,symdef,symsym,constexp,symcpu;
  24. Const
  25. RPNMax = 10; { I think you only need 4, but just to be safe }
  26. OpMax = 25;
  27. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  28. {---------------------------------------------------------------------
  29. Instruction management
  30. ---------------------------------------------------------------------}
  31. type
  32. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  33. OPR_REFERENCE,OPR_REGISTER,OPR_COND,OPR_REGSET,
  34. OPR_SHIFTEROP,OPR_MODEFLAGS,OPR_SPECIALREG,
  35. OPR_REGPAIR,OPR_FENCEFLAGS);
  36. TOprRec = record
  37. case typ:TOprType of
  38. OPR_NONE : ();
  39. {$if defined(AVR)}
  40. OPR_CONSTANT : (val:longint);
  41. {$elseif defined(i8086)}
  42. OPR_CONSTANT : (val:longint);
  43. {$elseif defined(Z80)}
  44. OPR_CONSTANT : (val:longint);
  45. {$else}
  46. OPR_CONSTANT : (val:aint);
  47. {$endif}
  48. OPR_SYMBOL : (symbol:tasmsymbol;symofs:aint;symseg:boolean;sym_farproc_entry:boolean);
  49. OPR_REFERENCE : (varsize:asizeint; constoffset: asizeint;ref_farproc_entry:boolean;ref:treference);
  50. OPR_LOCAL : (localvarsize, localconstoffset: asizeint;localsym:tabstractnormalvarsym;localsymofs:aint;localsegment,localindexreg:tregister;localscale:byte;localgetoffset,localforceref:boolean);
  51. OPR_REGISTER : (reg:tregister);
  52. {$ifdef m68k}
  53. OPR_REGSET : (regsetdata,regsetaddr,regsetfpu : tcpuregisterset);
  54. OPR_REGPAIR : (reghi,reglo: tregister);
  55. {$endif m68k}
  56. {$ifdef powerpc}
  57. OPR_COND : (cond : tasmcond);
  58. {$endif powerpc}
  59. {$ifdef POWERPC64}
  60. OPR_COND : (cond : tasmcond);
  61. {$endif POWERPC64}
  62. {$ifdef arm}
  63. OPR_REGSET : (regset : tcpuregisterset; regtype: tregistertype; subreg: tsubregister; usermode: boolean);
  64. OPR_SHIFTEROP : (shifterop : tshifterop);
  65. OPR_COND : (cc : tasmcond);
  66. OPR_MODEFLAGS : (flags : tcpumodeflags);
  67. OPR_SPECIALREG: (specialreg : tregister; specialregflags : tspecialregflags);
  68. {$endif arm}
  69. {$ifdef aarch64}
  70. OPR_SHIFTEROP : (shifterop : tshifterop);
  71. OPR_COND : (cc : tasmcond);
  72. {$endif aarch64}
  73. {$if defined(riscv32) or defined(riscv64)}
  74. OPR_FENCEFLAGS: (fenceflags : TFenceFlags);
  75. {$endif aarch64}
  76. end;
  77. TOperand = class
  78. opr : TOprRec;
  79. typesize : byte;
  80. haslabelref, { if the operand has a label, used in a reference like a
  81. var (e.g. 'mov ax, word ptr [label+5]', but *not*
  82. e.g. 'jmp label') }
  83. hasproc, { if the operand has a procedure/function reference }
  84. hastype, { if the operand has typecasted variable }
  85. hasvar : boolean; { if the operand is loaded with a variable }
  86. size : TCGSize;
  87. constructor create;virtual;
  88. destructor destroy;override;
  89. Procedure SetSize(_size:longint;force:boolean);virtual;
  90. Procedure SetCorrectSize(opcode:tasmop);virtual;
  91. Function SetupResult:boolean;virtual;
  92. Function SetupSelf:boolean;
  93. Function SetupOldEBP:boolean;
  94. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  95. Function CheckOperand: boolean; virtual;
  96. Procedure InitRef;
  97. Procedure InitRefConvertLocal;
  98. protected
  99. Procedure InitRefError;
  100. end;
  101. TCOperand = class of TOperand;
  102. TInstruction = class
  103. operands : array[1..max_operands] of toperand;
  104. opcode : tasmop;
  105. condition : tasmcond;
  106. ops : byte;
  107. labeled : boolean;
  108. filepos : tfileposinfo;
  109. constructor create(optype : tcoperand);virtual;
  110. destructor destroy;override;
  111. { converts the instruction to an instruction how it's used by the assembler writer
  112. and concats it to the passed list. The newly created item is returned if the
  113. instruction was valid, otherwise nil is returned }
  114. function ConcatInstruction(p:TAsmList) : tai;virtual;
  115. end;
  116. {---------------------------------------------------------------------}
  117. { Expression parser types }
  118. {---------------------------------------------------------------------}
  119. TExprOperator = record
  120. ch: char; { operator }
  121. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  122. end;
  123. {**********************************************************************}
  124. { The following operators are supported: }
  125. { '+' : addition }
  126. { '-' : subtraction }
  127. { '*' : multiplication }
  128. { '/' : modulo division }
  129. { '^' : exclusive or }
  130. { '<' : shift left }
  131. { '>' : shift right }
  132. { '&' : bitwise and }
  133. { '|' : bitwise or }
  134. { '~' : bitwise complement }
  135. { '%' : modulo division }
  136. { nnn: longint numbers }
  137. { ( and ) parenthesis }
  138. { [ and ] another kind of parenthesis }
  139. {**********************************************************************}
  140. TExprParse = class
  141. public
  142. Constructor create;
  143. Destructor Destroy;override;
  144. Function Evaluate(Expr: String): tcgint;
  145. Function Priority(_Operator: Char): aint;
  146. private
  147. RPNStack : Array[1..RPNMax] of tcgint; { Stack For RPN calculator }
  148. RPNTop : tcgint;
  149. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  150. OpTop : tcgint;
  151. Procedure RPNPush(Num: tcgint);
  152. Function RPNPop: tcgint;
  153. Procedure RPNCalc(const token: String; prefix: boolean);
  154. Procedure OpPush(_Operator: char; prefix: boolean);
  155. { In reality returns TExprOperaotr }
  156. Procedure OpPop(var _Operator:TExprOperator);
  157. end;
  158. { Evaluate an expression string to a tcgint }
  159. Function CalculateExpression(const expression: string): tcgint;
  160. {---------------------------------------------------------------------}
  161. { String routines }
  162. {---------------------------------------------------------------------}
  163. Function ParseVal(const S:String;base:byte):tcgint;
  164. Function PadZero(Var s: String; n: byte): Boolean;
  165. Function EscapeToPascal(const s:string): string;
  166. {---------------------------------------------------------------------
  167. Symbol helper routines
  168. ---------------------------------------------------------------------}
  169. procedure AsmSearchSym(const s:string;out srsym:tsym;out srsymtable:TSymtable);
  170. Function GetRecordOffsetSize(s:string;out Offset: tcgint;out Size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean):boolean;
  171. Function SearchType(const hs:string;out size:tcgint): Boolean;
  172. Function SearchRecordType(const s:string): boolean;
  173. Function SearchIConstant(const s:string; var l:tcgint): boolean;
  174. Function AsmRegisterPara(sym: tabstractnormalvarsym): boolean;
  175. {---------------------------------------------------------------------
  176. Instruction generation routines
  177. ---------------------------------------------------------------------}
  178. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  179. Procedure ConcatConstant(p : TAsmList;value: tcgint; constsize:byte);
  180. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:tcgint;constsize:byte;isofs:boolean);
  181. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  182. Procedure ConcatString(p : TAsmList;s:string);
  183. procedure ConcatAlign(p:TAsmList;l:tcgint);
  184. Procedure ConcatPublic(p:TAsmList;const s : string);
  185. Procedure ConcatLocal(p:TAsmList;const s : string);
  186. Implementation
  187. uses
  188. SysUtils,
  189. defutil,systems,verbose,globals,
  190. symtable,paramgr,
  191. aasmcpu,
  192. procinfo,ngenutil;
  193. {*************************************************************************
  194. TExprParse
  195. *************************************************************************}
  196. Constructor TExprParse.create;
  197. Begin
  198. end;
  199. Procedure TExprParse.RPNPush(Num : tcgint);
  200. { Add an operand to the top of the RPN stack }
  201. begin
  202. if RPNTop < RPNMax then
  203. begin
  204. Inc(RPNTop);
  205. RPNStack[RPNTop]:=Num;
  206. end
  207. else
  208. Message(asmr_e_expr_illegal);
  209. end;
  210. Function TExprParse.RPNPop : tcgint;
  211. { Get the operand at the top of the RPN stack }
  212. begin
  213. RPNPop:=0;
  214. if RPNTop > 0 then
  215. begin
  216. RPNPop:=RPNStack[RPNTop];
  217. Dec(RPNTop);
  218. end
  219. else
  220. Message(asmr_e_expr_illegal);
  221. end;
  222. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  223. Var
  224. Temp : tcgint;
  225. n1,n2 : tcgint;
  226. LocalError : Integer;
  227. begin
  228. { Handle operators }
  229. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  230. Case Token[1] of
  231. '+' :
  232. Begin
  233. if not prefix then
  234. RPNPush(RPNPop + RPNPop);
  235. end;
  236. '-' :
  237. Begin
  238. if prefix then
  239. RPNPush(-(RPNPop))
  240. else
  241. begin
  242. n1:=RPNPop;
  243. n2:=RPNPop;
  244. RPNPush(n2 - n1);
  245. end;
  246. end;
  247. '*' : RPNPush(RPNPop * RPNPop);
  248. '&' :
  249. begin
  250. n1:=RPNPop;
  251. n2:=RPNPop;
  252. RPNPush(n2 and n1);
  253. end;
  254. '|' :
  255. begin
  256. n1:=RPNPop;
  257. n2:=RPNPop;
  258. RPNPush(n2 or n1);
  259. end;
  260. '~' : RPNPush(NOT RPNPop);
  261. '<' :
  262. begin
  263. n1:=RPNPop;
  264. n2:=RPNPop;
  265. RPNPush(n2 SHL n1);
  266. end;
  267. '>' :
  268. begin
  269. n1:=RPNPop;
  270. n2:=RPNPop;
  271. RPNPush(n2 SHR n1);
  272. end;
  273. '%' :
  274. begin
  275. Temp:=RPNPop;
  276. if Temp <> 0 then
  277. RPNPush(RPNPop mod Temp)
  278. else
  279. begin
  280. Message(asmr_e_expr_zero_divide);
  281. { push 1 for error recovery }
  282. RPNPush(1);
  283. end;
  284. end;
  285. '^' : RPNPush(RPNPop XOR RPNPop);
  286. '/' :
  287. begin
  288. Temp:=RPNPop;
  289. if Temp <> 0 then
  290. RPNPush(RPNPop div Temp)
  291. else
  292. begin
  293. Message(asmr_e_expr_zero_divide);
  294. { push 1 for error recovery }
  295. RPNPush(1);
  296. end;
  297. end;
  298. end
  299. else
  300. begin
  301. { Convert String to number and add to stack }
  302. Val(Token, Temp, LocalError);
  303. if LocalError = 0 then
  304. RPNPush(Temp)
  305. else
  306. begin
  307. Message(asmr_e_expr_illegal);
  308. { push 1 for error recovery }
  309. RPNPush(1);
  310. end;
  311. end;
  312. end;
  313. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  314. { Add an operator onto top of the stack }
  315. begin
  316. if OpTop < OpMax then
  317. begin
  318. Inc(OpTop);
  319. OpStack[OpTop].ch:=_Operator;
  320. OpStack[OpTop].is_prefix:=prefix;
  321. end
  322. else
  323. Message(asmr_e_expr_illegal);
  324. end;
  325. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  326. { Get operator at the top of the stack }
  327. begin
  328. if OpTop > 0 then
  329. begin
  330. _Operator:=OpStack[OpTop];
  331. Dec(OpTop);
  332. end
  333. else
  334. Message(asmr_e_expr_illegal);
  335. end;
  336. Function TExprParse.Priority(_Operator : Char) : aint;
  337. { Return priority of operator }
  338. { The greater the priority, the higher the precedence }
  339. begin
  340. Priority:=0;
  341. Case _Operator OF
  342. '(','[' :
  343. Priority:=0;
  344. '|','^','~' : // the lowest priority: OR, XOR, NOT
  345. Priority:=0;
  346. '&' : // bigger priority: AND
  347. Priority:=1;
  348. '+', '-' : // bigger priority: +, -
  349. Priority:=2;
  350. '*', '/','%','<','>' : // the highest priority: *, /, MOD, SHL, SHR
  351. Priority:=3;
  352. else
  353. Message(asmr_e_expr_illegal);
  354. end;
  355. end;
  356. Function TExprParse.Evaluate(Expr : String):tcgint;
  357. Var
  358. I : longint;
  359. Token : String;
  360. opr : TExprOperator;
  361. begin
  362. Evaluate:=0;
  363. { Reset stacks }
  364. OpTop :=0;
  365. RPNTop:=0;
  366. Token :='';
  367. { nothing to do ? }
  368. if Expr='' then
  369. exit;
  370. For I:=1 to Length(Expr) DO
  371. begin
  372. if Expr[I] in ['0'..'9'] then
  373. begin { Build multi-digit numbers }
  374. Token:=Token + Expr[I];
  375. if I = Length(Expr) then { Send last one to calculator }
  376. RPNCalc(Token,false);
  377. end
  378. else
  379. if Expr[I] in ['+', '-', '*', '/', '(', ')','[',']','^','&','|','%','~','<','>'] then
  380. begin
  381. if Token <> '' then
  382. begin { Send last built number to calc. }
  383. RPNCalc(Token,false);
  384. Token:='';
  385. end;
  386. Case Expr[I] OF
  387. '[' : OpPush('[',false);
  388. ']' : begin
  389. While (OpTop>0) and (OpStack[OpTop].ch <> '[') DO
  390. Begin
  391. OpPop(opr);
  392. RPNCalc(opr.ch,opr.is_prefix);
  393. end;
  394. OpPop(opr); { Pop off and ignore the '[' }
  395. end;
  396. '(' : OpPush('(',false);
  397. ')' : begin
  398. While (OpTop>0) and (OpStack[OpTop].ch <> '(') DO
  399. Begin
  400. OpPop(opr);
  401. RPNCalc(opr.ch,opr.is_prefix);
  402. end;
  403. OpPop(opr); { Pop off and ignore the '(' }
  404. end;
  405. '+','-','~' : Begin
  406. { workaround for -2147483648 }
  407. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  408. begin
  409. token:='-';
  410. expr[i]:='+';
  411. end;
  412. { if start of expression then surely a prefix }
  413. { or if previous char was also an operator }
  414. if (I = 1) or (not (Expr[I-1] in ['0'..'9',')'])) then
  415. OpPush(Expr[I],true)
  416. else
  417. Begin
  418. { Evaluate all higher priority operators }
  419. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  420. Begin
  421. OpPop(opr);
  422. RPNCalc(opr.ch,opr.is_prefix);
  423. end;
  424. OpPush(Expr[I],false);
  425. End;
  426. end;
  427. '*', '/',
  428. '^','|','&',
  429. '%','<','>' : begin
  430. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  431. Begin
  432. OpPop(opr);
  433. RPNCalc(opr.ch,opr.is_prefix);
  434. end;
  435. OpPush(Expr[I],false);
  436. end;
  437. end; { Case }
  438. end
  439. else
  440. Message(asmr_e_expr_illegal); { Handle bad input error }
  441. end;
  442. { Pop off the remaining operators }
  443. While OpTop > 0 do
  444. Begin
  445. OpPop(opr);
  446. RPNCalc(opr.ch,opr.is_prefix);
  447. end;
  448. { The result is stored on the top of the stack }
  449. Evaluate:=RPNPop;
  450. end;
  451. Destructor TExprParse.Destroy;
  452. Begin
  453. end;
  454. Function CalculateExpression(const expression: string): tcgint;
  455. var
  456. expr: TExprParse;
  457. Begin
  458. expr:=TExprParse.create;
  459. CalculateExpression:=expr.Evaluate(expression);
  460. expr.Free;
  461. end;
  462. {*************************************************************************}
  463. { String conversions/utils }
  464. {*************************************************************************}
  465. Function EscapeToPascal(const s:string): string;
  466. { converts a C styled string - which contains escape }
  467. { characters to a pascal style string. }
  468. var
  469. i,len : asizeint;
  470. hs : string;
  471. temp : string;
  472. c : char;
  473. Begin
  474. hs:='';
  475. len:=0;
  476. i:=0;
  477. while (i<length(s)) and (len<255) do
  478. begin
  479. Inc(i);
  480. if (s[i]='\') and (i<length(s)) then
  481. Begin
  482. inc(i);
  483. case s[i] of
  484. '\' :
  485. c:='\';
  486. 'b':
  487. c:=#8;
  488. 'f':
  489. c:=#12;
  490. 'n':
  491. c:=#10;
  492. 'r':
  493. c:=#13;
  494. 't':
  495. c:=#9;
  496. '"':
  497. c:='"';
  498. '0'..'7':
  499. Begin
  500. temp:=s[i];
  501. temp:=temp+s[i+1];
  502. temp:=temp+s[i+2];
  503. inc(i,2);
  504. c:=chr(ParseVal(temp,8));
  505. end;
  506. 'x':
  507. Begin
  508. temp:=s[i+1];
  509. temp:=temp+s[i+2];
  510. inc(i,2);
  511. c:=chr(ParseVal(temp,16));
  512. end;
  513. else
  514. Begin
  515. Message1(asmr_e_escape_seq_ignored,s[i]);
  516. c:=s[i];
  517. end;
  518. end;
  519. end
  520. else
  521. c:=s[i];
  522. inc(len);
  523. hs[len]:=c;
  524. end;
  525. hs[0]:=chr(len);
  526. EscapeToPascal:=hs;
  527. end;
  528. Function ParseVal(const S:String;base:byte):tcgint;
  529. { Converts a decimal string to tcgint }
  530. var
  531. code : integer;
  532. errmsg : word;
  533. prefix : string[2];
  534. Begin
  535. case base of
  536. 2 :
  537. begin
  538. errmsg:=asmr_e_error_converting_binary;
  539. prefix:='%';
  540. end;
  541. 8 :
  542. begin
  543. errmsg:=asmr_e_error_converting_octal;
  544. prefix:='&';
  545. end;
  546. 10 :
  547. begin
  548. errmsg:=asmr_e_error_converting_decimal;
  549. prefix:='';
  550. end;
  551. 16 :
  552. begin
  553. errmsg:=asmr_e_error_converting_hexadecimal;
  554. prefix:='$';
  555. end;
  556. else
  557. internalerror(200501202);
  558. end;
  559. val(prefix+s,result,code);
  560. if code<>0 then
  561. begin
  562. val(prefix+s,result,code);
  563. if code<>0 then
  564. begin
  565. Message1(errmsg,s);
  566. result:=0;
  567. end;
  568. end;
  569. end;
  570. Function PadZero(Var s: String; n: byte): Boolean;
  571. Begin
  572. PadZero:=TRUE;
  573. { Do some error checking first }
  574. if Length(s) = n then
  575. exit
  576. else
  577. if Length(s) > n then
  578. Begin
  579. PadZero:=FALSE;
  580. delete(s,n+1,length(s));
  581. exit;
  582. end
  583. else
  584. PadZero:=TRUE;
  585. { Fill it up with the specified character }
  586. fillchar(s[length(s)+1],n-1,#0);
  587. s[0]:=chr(n);
  588. end;
  589. {****************************************************************************
  590. TOperand
  591. ****************************************************************************}
  592. constructor TOperand.Create;
  593. begin
  594. size:=OS_NO;
  595. hasproc:=false;
  596. hastype:=false;
  597. hasvar:=false;
  598. FillChar(Opr,sizeof(Opr),0);
  599. end;
  600. destructor TOperand.destroy;
  601. begin
  602. end;
  603. Procedure TOperand.SetSize(_size:longint;force:boolean);
  604. begin
  605. if force or
  606. ((size = OS_NO) and (_size<=16)) then
  607. Begin
  608. case _size of
  609. 1 : size:=OS_8;
  610. 2 : size:=OS_16{ could be S_IS};
  611. 4 : size:=OS_32{ could be S_IL or S_FS};
  612. 8 : size:=OS_64{ could be S_D or S_FL};
  613. 10 : size:=OS_F80;
  614. 16 : size:=OS_128;
  615. end;
  616. end;
  617. end;
  618. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  619. begin
  620. end;
  621. function TOperand.SetupResult:boolean;
  622. begin
  623. SetupResult:=false;
  624. { replace by correct offset. }
  625. with current_procinfo.procdef do
  626. if (not is_void(returndef)) then
  627. begin
  628. if (m_tp7 in current_settings.modeswitches) and
  629. not (df_generic in defoptions) and
  630. (po_assembler in procoptions) and
  631. (not paramanager.ret_in_param(returndef,current_procinfo.procdef)) then
  632. begin
  633. message(asmr_e_cannot_use_RESULT_here);
  634. exit;
  635. end;
  636. SetupResult:=setupvar('result',false)
  637. end
  638. else
  639. message(asmr_e_void_function);
  640. end;
  641. Function TOperand.SetupSelf:boolean;
  642. Begin
  643. SetupSelf:=false;
  644. if assigned(current_structdef) then
  645. SetupSelf:=setupvar('self',false)
  646. else
  647. Message(asmr_e_cannot_use_SELF_outside_a_method);
  648. end;
  649. Function TOperand.SetupOldEBP:boolean;
  650. Begin
  651. SetupOldEBP:=false;
  652. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  653. SetupOldEBP:=setupvar('parentframe',false)
  654. else
  655. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  656. end;
  657. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  658. function symtable_has_localvarsyms(st:TSymtable):boolean;
  659. var
  660. sym : tsym;
  661. i : longint;
  662. begin
  663. result:=false;
  664. for i:=0 to st.SymList.Count-1 do
  665. begin
  666. sym:=tsym(st.SymList[i]);
  667. if sym.typ=localvarsym then
  668. begin
  669. result:=true;
  670. exit;
  671. end;
  672. end;
  673. end;
  674. procedure setconst(l:aint);
  675. begin
  676. { We return the address of the field, just like Delphi/TP }
  677. case opr.typ of
  678. OPR_NONE :
  679. begin
  680. opr.typ:=OPR_CONSTANT;
  681. opr.val:=l;
  682. end;
  683. OPR_CONSTANT :
  684. inc(opr.val,l);
  685. OPR_REFERENCE :
  686. inc(opr.ref.offset,l);
  687. OPR_LOCAL :
  688. inc(opr.localsymofs,l);
  689. else
  690. Message(asmr_e_invalid_operand_type);
  691. end;
  692. end;
  693. procedure setvarsize(sym: tabstractvarsym);
  694. var
  695. harrdef: tarraydef;
  696. l: asizeint;
  697. begin
  698. case sym.vardef.typ of
  699. orddef,
  700. enumdef,
  701. pointerdef,
  702. procvardef,
  703. floatdef :
  704. SetSize(sym.getsize,false);
  705. arraydef :
  706. begin
  707. { for arrays try to get the element size, take care of
  708. multiple indexes }
  709. harrdef:=tarraydef(sym.vardef);
  710. { calc array size }
  711. if is_special_array(harrdef) then
  712. l := -1
  713. else
  714. l := harrdef.size;
  715. case opr.typ of
  716. OPR_REFERENCE: opr.varsize := l;
  717. OPR_LOCAL: opr.localvarsize := l;
  718. else
  719. ;
  720. end;
  721. while assigned(harrdef.elementdef) and
  722. (harrdef.elementdef.typ=arraydef) do
  723. harrdef:=tarraydef(harrdef.elementdef);
  724. if not is_packed_array(harrdef) then
  725. SetSize(harrdef.elesize,false)
  726. else
  727. if (harrdef.elepackedbitsize mod 8) = 0 then
  728. SetSize(harrdef.elepackedbitsize div 8,false);
  729. end;
  730. recorddef:
  731. case opr.typ of
  732. OPR_REFERENCE: opr.varsize := sym.getsize;
  733. OPR_LOCAL: opr.localvarsize := sym.getsize;
  734. else
  735. ;
  736. end;
  737. else
  738. ;
  739. end;
  740. end;
  741. { search and sets up the correct fields in the Instr record }
  742. { for the NON-constant identifier passed to the routine. }
  743. { if not found returns FALSE. }
  744. var
  745. sym : tsym;
  746. srsymtable : TSymtable;
  747. {$ifdef x86}
  748. segreg,
  749. {$endif x86}
  750. indexreg : tregister;
  751. plist : ppropaccesslistitem;
  752. size_set_from_absolute : boolean = false;
  753. { offset fixup (in bytes), coming from an absolute declaration with an index
  754. (e.g. var tralala: word absolute moo[5]; ) }
  755. absoffset: asizeint=0;
  756. harrdef: tarraydef;
  757. tmpprocinfo: tprocinfo;
  758. Begin
  759. SetupVar:=false;
  760. asmsearchsym(s,sym,srsymtable);
  761. if sym = nil then
  762. exit;
  763. if sym.typ=absolutevarsym then
  764. begin
  765. case tabsolutevarsym(sym).abstyp of
  766. tovar:
  767. begin
  768. { Only support simple loads }
  769. plist:=tabsolutevarsym(sym).ref.firstsym;
  770. if assigned(plist) and
  771. (plist^.sltype=sl_load) then
  772. begin
  773. setvarsize(tabstractvarsym(sym));
  774. size_set_from_absolute:=true;
  775. sym:=plist^.sym;
  776. { resolve the chain of array indexes (if there are any) }
  777. harrdef:=nil;
  778. while assigned(plist^.next) do
  779. begin
  780. plist:=plist^.next;
  781. if (plist^.sltype=sl_vec) and (tabstractvarsym(sym).vardef.typ=arraydef) then
  782. begin
  783. if harrdef=nil then
  784. harrdef:=tarraydef(tabstractvarsym(sym).vardef)
  785. else if harrdef.elementdef.typ=arraydef then
  786. harrdef:=tarraydef(harrdef.elementdef)
  787. else
  788. begin
  789. Message(asmr_e_unsupported_symbol_type);
  790. exit;
  791. end;
  792. if is_special_array(harrdef) then
  793. begin
  794. Message(asmr_e_unsupported_symbol_type);
  795. exit;
  796. end;
  797. if not is_packed_array(harrdef) then
  798. Inc(absoffset,asizeint(Int64(plist^.value-harrdef.lowrange))*harrdef.elesize)
  799. else if (Int64(plist^.value-harrdef.lowrange)*harrdef.elepackedbitsize mod 8)=0 then
  800. Inc(absoffset,asizeint(Int64(plist^.value-harrdef.lowrange)*harrdef.elepackedbitsize div 8))
  801. else
  802. Message(asmr_e_packed_element);
  803. end
  804. else
  805. begin
  806. Message(asmr_e_unsupported_symbol_type);
  807. exit;
  808. end;
  809. end;
  810. end
  811. else
  812. begin
  813. Message(asmr_e_unsupported_symbol_type);
  814. exit;
  815. end;
  816. end;
  817. toaddr:
  818. begin
  819. initref;
  820. opr.ref.offset:=tabsolutevarsym(sym).addroffset;
  821. setvarsize(tabstractvarsym(sym));
  822. size_set_from_absolute:=true;
  823. hasvar:=true;
  824. Result:=true;
  825. exit;
  826. end;
  827. else
  828. begin
  829. Message(asmr_e_unsupported_symbol_type);
  830. exit;
  831. end;
  832. end;
  833. end;
  834. case sym.typ of
  835. fieldvarsym :
  836. begin
  837. if not tabstractrecordsymtable(sym.owner).is_packed then
  838. setconst(absoffset+tfieldvarsym(sym).fieldoffset)
  839. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  840. setconst(absoffset+tfieldvarsym(sym).fieldoffset div 8)
  841. else
  842. Message(asmr_e_packed_element);
  843. if not size_set_from_absolute then
  844. setvarsize(tabstractvarsym(sym));
  845. hasvar:=true;
  846. SetupVar:=true;
  847. end;
  848. staticvarsym,
  849. localvarsym,
  850. paravarsym :
  851. begin
  852. { we always assume in asm statements that }
  853. { that the variable is valid. }
  854. tabstractvarsym(sym).varstate:=vs_readwritten;
  855. inc(tabstractvarsym(sym).refs);
  856. { variable can't be placed in a register }
  857. tabstractvarsym(sym).varregable:=vr_none;
  858. { and anything may happen with its address }
  859. tabstractvarsym(sym).addr_taken:=true;
  860. case sym.typ of
  861. staticvarsym :
  862. begin
  863. initref;
  864. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(sym).mangledname,AT_DATA);
  865. Inc(opr.ref.offset,absoffset);
  866. end;
  867. paravarsym,
  868. localvarsym :
  869. begin
  870. tmpprocinfo:=current_procinfo;
  871. while assigned(tmpprocinfo) do
  872. begin
  873. if (sym.owner=tmpprocinfo.procdef.localst) or
  874. (sym.owner=tmpprocinfo.procdef.parast) then
  875. begin
  876. tmpprocinfo.procdef.init_paraloc_info(calleeside);
  877. break;
  878. end;
  879. tmpprocinfo:=tmpprocinfo.parent;
  880. end;
  881. if opr.typ=OPR_REFERENCE then
  882. begin
  883. {$ifdef x86}
  884. segreg:=opr.ref.segment;
  885. {$endif x86}
  886. indexreg:=opr.ref.base;
  887. if opr.ref.index<>NR_NO then
  888. begin
  889. if indexreg=NR_NO then
  890. indexreg:=opr.ref.index
  891. else
  892. Message(asmr_e_multiple_index);
  893. end;
  894. end
  895. else
  896. begin
  897. {$ifdef x86}
  898. segreg:=NR_NO;
  899. {$endif x86}
  900. indexreg:=NR_NO;
  901. end;
  902. opr.typ:=OPR_LOCAL;
  903. if assigned(current_procinfo.parent) and
  904. not(po_inline in current_procinfo.procdef.procoptions) and
  905. (sym.owner<>current_procinfo.procdef.localst) and
  906. (sym.owner<>current_procinfo.procdef.parast) and
  907. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  908. symtable_has_localvarsyms(current_procinfo.procdef.localst) then
  909. message1(asmr_e_local_para_unreachable,s);
  910. opr.localsym:=tabstractnormalvarsym(sym);
  911. opr.localsymofs:=absoffset;
  912. {$ifdef x86}
  913. opr.localsegment:=segreg;
  914. {$endif x86}
  915. opr.localindexreg:=indexreg;
  916. opr.localscale:=0;
  917. opr.localgetoffset:=GetOffset;
  918. if paramanager.push_addr_param(tabstractvarsym(sym).varspez,tabstractvarsym(sym).vardef,current_procinfo.procdef.proccalloption) then
  919. SetSize(sizeof(pint),false);
  920. end;
  921. else
  922. ;
  923. end;
  924. if not size_set_from_absolute then
  925. setvarsize(tabstractvarsym(sym));
  926. hasvar:=true;
  927. SetupVar:=true;
  928. Exit;
  929. end;
  930. constsym :
  931. begin
  932. if tconstsym(sym).consttyp=constord then
  933. begin
  934. setconst(tconstsym(sym).value.valueord.svalue);
  935. SetupVar:=true;
  936. Exit;
  937. end;
  938. end;
  939. typesym :
  940. begin
  941. if ttypesym(sym).typedef.typ in [recorddef,objectdef] then
  942. begin
  943. setconst(0);
  944. SetupVar:=TRUE;
  945. Exit;
  946. end;
  947. end;
  948. procsym :
  949. begin
  950. if Tprocsym(sym).ProcdefList.Count>1 then
  951. Message(asmr_w_calling_overload_func);
  952. case opr.typ of
  953. OPR_REFERENCE:
  954. begin
  955. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION);
  956. Inc(opr.ref.offset,absoffset);
  957. {$ifdef i8086}
  958. opr.ref_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  959. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  960. {$endif i8086}
  961. end;
  962. OPR_NONE:
  963. begin
  964. opr.typ:=OPR_SYMBOL;
  965. opr.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION);
  966. {$ifdef i8086}
  967. opr.sym_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  968. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  969. {$endif i8086}
  970. opr.symofs:=absoffset;
  971. end;
  972. else
  973. Message(asmr_e_invalid_operand_type);
  974. end;
  975. hasproc:=true;
  976. hasvar:=true;
  977. SetupVar:=TRUE;
  978. Exit;
  979. end;
  980. {$ifdef i8086}
  981. labelsym :
  982. begin
  983. case opr.typ of
  984. OPR_REFERENCE:
  985. begin
  986. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tlabelsym(sym).mangledname,AT_FUNCTION);
  987. Inc(opr.ref.offset,absoffset);
  988. if opr.ref.segment=NR_NO then
  989. opr.ref.segment:=NR_CS;
  990. end;
  991. else
  992. begin
  993. Message(asmr_e_unsupported_symbol_type);
  994. exit;
  995. end;
  996. end;
  997. haslabelref:=true;
  998. hasvar:=true;
  999. SetupVar:=TRUE;
  1000. Exit;
  1001. end
  1002. {$endif i8086}
  1003. else
  1004. begin
  1005. Message(asmr_e_unsupported_symbol_type);
  1006. exit;
  1007. end;
  1008. end;
  1009. end;
  1010. procedure TOperand.InitRef;
  1011. {*********************************************************************}
  1012. { Description: This routine first check if the opcode is of }
  1013. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  1014. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  1015. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  1016. { to point to the default segment. }
  1017. {*********************************************************************}
  1018. var
  1019. l : aint;
  1020. hsymofs : aint;
  1021. hsymbol : tasmsymbol;
  1022. reg : tregister;
  1023. hsym_farprocentry: Boolean;
  1024. Begin
  1025. case opr.typ of
  1026. OPR_REFERENCE :
  1027. exit;
  1028. OPR_CONSTANT :
  1029. begin
  1030. l:=opr.val;
  1031. opr.typ:=OPR_REFERENCE;
  1032. Fillchar(opr.ref,sizeof(treference),0);
  1033. opr.Ref.Offset:=l;
  1034. opr.varsize:=0;
  1035. opr.constoffset:=0;
  1036. opr.ref_farproc_entry:=false;
  1037. end;
  1038. OPR_NONE :
  1039. begin
  1040. opr.typ:=OPR_REFERENCE;
  1041. opr.varsize:=0;
  1042. opr.constoffset:=0;
  1043. opr.ref_farproc_entry:=false;
  1044. Fillchar(opr.ref,sizeof(treference),0);
  1045. end;
  1046. OPR_REGISTER :
  1047. begin
  1048. reg:=opr.reg;
  1049. opr.typ:=OPR_REFERENCE;
  1050. opr.varsize:=0;
  1051. opr.constoffset:=0;
  1052. opr.ref_farproc_entry:=false;
  1053. Fillchar(opr.ref,sizeof(treference),0);
  1054. opr.Ref.base:=reg;
  1055. end;
  1056. OPR_SYMBOL :
  1057. begin
  1058. hsymbol:=opr.symbol;
  1059. hsymofs:=opr.symofs;
  1060. hsym_farprocentry:=opr.sym_farproc_entry;
  1061. opr.typ:=OPR_REFERENCE;
  1062. opr.varsize:=0;
  1063. opr.constoffset:=0;
  1064. Fillchar(opr.ref,sizeof(treference),0);
  1065. opr.ref.symbol:=hsymbol;
  1066. opr.ref.offset:=hsymofs;
  1067. opr.ref_farproc_entry:=hsym_farprocentry;
  1068. end;
  1069. else
  1070. InitRefError;
  1071. end;
  1072. end;
  1073. procedure TOperand.InitRefConvertLocal;
  1074. var
  1075. localvarsize,localconstoffset: asizeint;
  1076. localsym:tabstractnormalvarsym;
  1077. localsymofs:aint;
  1078. {$ifdef x86}
  1079. localsegment,
  1080. {$endif x86}
  1081. localindexreg:tregister;
  1082. localscale:byte;
  1083. begin
  1084. if opr.typ=OPR_LOCAL then
  1085. begin
  1086. if AsmRegisterPara(opr.localsym) and
  1087. not opr.localgetoffset then
  1088. begin
  1089. localvarsize:=opr.localvarsize;
  1090. localconstoffset:=opr.localconstoffset;
  1091. localsym:=opr.localsym;
  1092. localsymofs:=opr.localsymofs;
  1093. {$ifdef x86}
  1094. localsegment:=opr.localsegment;
  1095. {$endif x86}
  1096. localindexreg:=opr.localindexreg;
  1097. localscale:=opr.localscale;
  1098. opr.typ:=OPR_REFERENCE;
  1099. hasvar:=false;
  1100. Fillchar(opr.ref,sizeof(treference),0);
  1101. opr.varsize:=localvarsize;
  1102. opr.constoffset:=localconstoffset;
  1103. opr.ref_farproc_entry:=false;
  1104. opr.ref.base:=tparavarsym(localsym).paraloc[calleeside].Location^.register;
  1105. opr.ref.offset:=localsymofs;
  1106. {$ifdef x86}
  1107. opr.ref.segment:=localsegment;
  1108. {$endif x86}
  1109. opr.ref.index:=localindexreg;
  1110. opr.ref.scalefactor:=localscale;
  1111. end
  1112. else
  1113. InitRefError;
  1114. end
  1115. else
  1116. InitRef;
  1117. end;
  1118. procedure TOperand.InitRefError;
  1119. begin
  1120. Message(asmr_e_invalid_operand_type);
  1121. { Recover }
  1122. opr.typ:=OPR_REFERENCE;
  1123. opr.varsize:=0;
  1124. opr.constoffset:=0;
  1125. opr.ref_farproc_entry:=false;
  1126. Fillchar(opr.ref,sizeof(treference),0);
  1127. end;
  1128. Function TOperand.CheckOperand: boolean;
  1129. {*********************************************************************}
  1130. { Description: This routine checks if the operand is of }
  1131. { valid, and returns false if it isn't. Does nothing by default. }
  1132. {*********************************************************************}
  1133. begin
  1134. result:=true;
  1135. end;
  1136. {****************************************************************************
  1137. TInstruction
  1138. ****************************************************************************}
  1139. constructor TInstruction.create(optype : tcoperand);
  1140. var
  1141. i : longint;
  1142. Begin
  1143. { these field are set to 0 anyways by the constructor helper (FK)
  1144. Opcode:=A_NONE;
  1145. Condition:=C_NONE;
  1146. Ops:=0;
  1147. }
  1148. filepos:=current_filepos;
  1149. for i:=1 to max_operands do
  1150. Operands[i]:=optype.create;
  1151. Labeled:=false;
  1152. end;
  1153. destructor TInstruction.destroy;
  1154. var
  1155. i : longint;
  1156. Begin
  1157. for i:=1 to max_operands do
  1158. Operands[i].free;
  1159. end;
  1160. function TInstruction.ConcatInstruction(p:TAsmList) : tai;
  1161. var
  1162. ai : taicpu;
  1163. i : longint;
  1164. begin
  1165. for i:=1 to Ops do
  1166. operands[i].CheckOperand;
  1167. ai:=taicpu.op_none(opcode);
  1168. ai.fileinfo:=filepos;
  1169. ai.Ops:=Ops;
  1170. ai.Allocate_oper(Ops);
  1171. for i:=1 to Ops do
  1172. with operands[i].opr do
  1173. begin
  1174. case typ of
  1175. OPR_CONSTANT :
  1176. ai.loadconst(i-1,val);
  1177. OPR_REGISTER:
  1178. ai.loadreg(i-1,reg);
  1179. OPR_SYMBOL:
  1180. ai.loadsymbol(i-1,symbol,symofs);
  1181. OPR_LOCAL :
  1182. begin
  1183. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1184. localscale,localgetoffset,localforceref);
  1185. {$ifdef x86}
  1186. ai.oper[i-1]^.localoper^.localsegment:=localsegment;
  1187. {$endif x86}
  1188. end;
  1189. OPR_REFERENCE:
  1190. ai.loadref(i-1,ref);
  1191. {$ifdef m68k}
  1192. OPR_REGSET:
  1193. ai.loadregset(i-1,regsetdata,regsetaddr,regsetfpu);
  1194. OPR_REGPAIR:
  1195. ai.loadregpair(i-1,reghi,reglo);
  1196. {$endif}
  1197. {$ifdef ARM}
  1198. OPR_REGSET:
  1199. ai.loadregset(i-1,regtype,subreg,regset,usermode);
  1200. OPR_MODEFLAGS:
  1201. ai.loadmodeflags(i-1,flags);
  1202. OPR_SPECIALREG:
  1203. ai.loadspecialreg(i-1,specialreg,specialregflags);
  1204. {$endif ARM}
  1205. {$if defined(arm) or defined(aarch64)}
  1206. OPR_SHIFTEROP:
  1207. ai.loadshifterop(i-1,shifterop);
  1208. OPR_COND:
  1209. ai.loadconditioncode(i-1,cc);
  1210. {$endif arm or aarch64}
  1211. {$if defined(riscv32) or defined(riscv64)}
  1212. OPR_FENCEFLAGS:
  1213. ai.loadfenceflags(i-1,fenceflags);
  1214. {$endif riscv32 or riscv64}
  1215. { ignore wrong operand }
  1216. OPR_NONE:
  1217. ;
  1218. else
  1219. internalerror(200501051);
  1220. end;
  1221. end;
  1222. ai.SetCondition(condition);
  1223. { Concat the opcode or give an error }
  1224. if assigned(ai) then
  1225. p.concat(ai)
  1226. else
  1227. Message(asmr_e_invalid_opcode_and_operand);
  1228. result:=ai;
  1229. end;
  1230. {****************************************************************************
  1231. Symbol table helper routines
  1232. ****************************************************************************}
  1233. procedure AddAbsoluteSymRefs(sym: tabsolutevarsym); forward;
  1234. procedure MaybeAddSymRef(sym: tsym);
  1235. begin
  1236. case sym.typ of
  1237. absolutevarsym:
  1238. AddAbsoluteSymRefs(tabsolutevarsym(sym));
  1239. staticvarsym:
  1240. if not(vo_is_external in tstaticvarsym(sym).varoptions) then
  1241. cnodeutils.RegisterUsedAsmSym(current_asmdata.RefAsmSymbol(sym.mangledname,AT_DATA),tstaticvarsym(sym).vardef,true);
  1242. procsym:
  1243. begin
  1244. { if it's a pure assembler routine, the definition of the symbol will also
  1245. be in assembler and it can't be removed by the compiler (and if we mark
  1246. it as used anyway, clang will get into trouble) }
  1247. if not(po_assembler in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions) and
  1248. not(po_external in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions) then
  1249. cnodeutils.RegisterUsedAsmSym(current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION),tprocdef(tprocsym(sym).ProcdefList[0]),true);
  1250. end;
  1251. else
  1252. ;
  1253. end;
  1254. end;
  1255. procedure AddAbsoluteSymRefs(sym: tabsolutevarsym);
  1256. var
  1257. symlist: ppropaccesslistitem;
  1258. begin
  1259. case sym.abstyp of
  1260. toaddr:
  1261. ;
  1262. toasm:
  1263. begin
  1264. cnodeutils.RegisterUsedAsmSym(current_asmdata.RefAsmSymbol(sym.mangledname,AT_DATA),sym.vardef,true);
  1265. end;
  1266. tovar:
  1267. begin
  1268. symlist:=tabsolutevarsym(sym).ref.firstsym;
  1269. repeat
  1270. case symlist^.sltype of
  1271. sl_load:
  1272. MaybeAddSymRef(symlist^.sym);
  1273. sl_subscript,
  1274. sl_absolutetype,
  1275. sl_typeconv,
  1276. sl_vec:
  1277. ;
  1278. else
  1279. internalerror(2009031401);
  1280. end;
  1281. symlist:=symlist^.next;
  1282. until not assigned(symlist);
  1283. end;
  1284. end;
  1285. end;
  1286. procedure AsmSearchSym(const s:string;out srsym:tsym;out srsymtable:TSymtable);
  1287. var
  1288. i : integer;
  1289. begin
  1290. i:=pos('.',s);
  1291. { allow unit.identifier }
  1292. if i>0 then
  1293. begin
  1294. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1295. if assigned(srsym) then
  1296. begin
  1297. if (srsym.typ=unitsym) and
  1298. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  1299. srsym.owner.iscurrentunit then
  1300. searchsym_in_module(tunitsym(srsym).module,Copy(s,i+1,255),srsym,srsymtable)
  1301. else
  1302. begin
  1303. srsym:=nil;
  1304. srsymtable:=nil;
  1305. end;
  1306. end;
  1307. end
  1308. else
  1309. searchsym(s,srsym,srsymtable);
  1310. { in asm routines, the function result variable, that matches the function
  1311. name should be avoided, because:
  1312. 1) there's already a @Result directive (even in TP7) that can be used, if
  1313. you want to access the function result
  1314. 2) there's no other way to disambiguate between the function result variable
  1315. and the function's address (using asm syntax only)
  1316. This fixes code, such as:
  1317. function test1: word;
  1318. begin
  1319. asm
  1320. mov ax, offset test1
  1321. end;
  1322. end;
  1323. and makes it work in a consistent manner as this code:
  1324. procedure test2;
  1325. begin
  1326. asm
  1327. mov ax, offset test2
  1328. end;
  1329. end; }
  1330. if assigned(srsym) and
  1331. assigned(srsymtable) and
  1332. (srsym.typ=absolutevarsym) and
  1333. (vo_is_funcret in tabsolutevarsym(srsym).varoptions) and
  1334. (srsymtable.symtabletype=localsymtable) and
  1335. assigned(srsymtable.defowner) and
  1336. (srsymtable.defowner.typ=procdef) and
  1337. (tprocdef(srsymtable.defowner).procsym.name=tabsolutevarsym(srsym).Name) then
  1338. begin
  1339. srsym:=tprocdef(srsymtable.defowner).procsym;
  1340. srsymtable:=srsym.Owner;
  1341. end;
  1342. { llvm can't catch symbol references from inline assembler blocks }
  1343. if assigned(srsym) then
  1344. MaybeAddSymRef(srsym);
  1345. end;
  1346. Function SearchType(const hs:string;out size:tcgint): Boolean;
  1347. var
  1348. srsym : tsym;
  1349. srsymtable : TSymtable;
  1350. begin
  1351. result:=false;
  1352. size:=0;
  1353. asmsearchsym(hs,srsym,srsymtable);
  1354. if assigned(srsym) and
  1355. (srsym.typ=typesym) then
  1356. begin
  1357. size:=ttypesym(srsym).typedef.size;
  1358. result:=true;
  1359. end;
  1360. end;
  1361. Function SearchRecordType(const s:string): boolean;
  1362. var
  1363. srsym : tsym;
  1364. srsymtable : TSymtable;
  1365. Begin
  1366. SearchRecordType:=false;
  1367. { Check the constants in symtable }
  1368. asmsearchsym(s,srsym,srsymtable);
  1369. if srsym <> nil then
  1370. Begin
  1371. case srsym.typ of
  1372. typesym :
  1373. begin
  1374. if ttypesym(srsym).typedef.typ in [recorddef,objectdef] then
  1375. begin
  1376. SearchRecordType:=true;
  1377. exit;
  1378. end;
  1379. end;
  1380. fieldvarsym :
  1381. begin
  1382. if (tfieldvarsym(srsym).vardef.typ in [recorddef,objectdef]) then
  1383. begin
  1384. SearchRecordType:=true;
  1385. exit;
  1386. end;
  1387. end;
  1388. else
  1389. ;
  1390. end;
  1391. end;
  1392. end;
  1393. Function SearchIConstant(const s:string; var l:tcgint): boolean;
  1394. {**********************************************************************}
  1395. { Description: Searches for a CONSTANT of name s in either the local }
  1396. { symbol list, then in the global symbol list, and returns the value }
  1397. { of that constant in l. Returns TRUE if successfull, if not found, }
  1398. { or if the constant is not of correct type, then returns FALSE }
  1399. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1400. { respectively. }
  1401. {**********************************************************************}
  1402. var
  1403. srsym : tsym;
  1404. srsymtable : TSymtable;
  1405. Begin
  1406. SearchIConstant:=false;
  1407. { check for TRUE or FALSE reserved words first }
  1408. if s = 'TRUE' then
  1409. Begin
  1410. SearchIConstant:=TRUE;
  1411. l:=1;
  1412. exit;
  1413. end;
  1414. if s = 'FALSE' then
  1415. Begin
  1416. SearchIConstant:=TRUE;
  1417. l:=0;
  1418. exit;
  1419. end;
  1420. { Check the constants in symtable }
  1421. asmsearchsym(s,srsym,srsymtable);
  1422. if srsym <> nil then
  1423. Begin
  1424. case srsym.typ of
  1425. constsym :
  1426. begin
  1427. if tconstsym(srsym).consttyp=constord then
  1428. Begin
  1429. l:=tconstsym(srsym).value.valueord.svalue;
  1430. SearchIConstant:=TRUE;
  1431. exit;
  1432. end;
  1433. end;
  1434. enumsym:
  1435. Begin
  1436. l:=tenumsym(srsym).value;
  1437. SearchIConstant:=TRUE;
  1438. exit;
  1439. end;
  1440. else
  1441. ;
  1442. end;
  1443. end;
  1444. end;
  1445. function AsmRegisterPara(sym: tabstractnormalvarsym): boolean;
  1446. begin
  1447. result:=
  1448. (po_assembler in current_procinfo.procdef.procoptions) and
  1449. (sym.typ=paravarsym) and
  1450. (tparavarsym(sym).paraloc[calleeside].Location^.Loc=LOC_REGISTER);
  1451. end;
  1452. Function GetRecordOffsetSize(s:string;out Offset: tcgint;out Size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean):boolean;
  1453. { search and returns the offset and size of records/objects of the base }
  1454. { with field name setup in field. }
  1455. { returns FALSE if not found. }
  1456. { used when base is a variable or a typed constant name. }
  1457. var
  1458. st : TSymtable;
  1459. harrdef : tarraydef;
  1460. sym : tsym;
  1461. srsymtable : TSymtable;
  1462. i : longint;
  1463. base : string;
  1464. procdef: tprocdef;
  1465. Begin
  1466. GetRecordOffsetSize:=FALSE;
  1467. Offset:=0;
  1468. Size:=0;
  1469. mangledname:='';
  1470. hastypecast:=false;
  1471. i:=pos('.',s);
  1472. if i=0 then
  1473. i:=255;
  1474. base:=Copy(s,1,i-1);
  1475. delete(s,1,i);
  1476. if base='SELF' then
  1477. st:=current_structdef.symtable
  1478. else
  1479. begin
  1480. asmsearchsym(base,sym,srsymtable);
  1481. { allow unitname.identifier }
  1482. if assigned(sym) and (sym.typ=unitsym) then
  1483. begin
  1484. i:=pos('.',s);
  1485. if i=0 then
  1486. i:=255;
  1487. base:=base+'.'+Copy(s,1,i-1);
  1488. delete(s,1,i);
  1489. asmsearchsym(base,sym,srsymtable);
  1490. end;
  1491. st:=nil;
  1492. { we can start with a var,type,typedconst }
  1493. if assigned(sym) then
  1494. case sym.typ of
  1495. staticvarsym,
  1496. localvarsym,
  1497. paravarsym :
  1498. st:=Tabstractvarsym(sym).vardef.GetSymtable(gs_record);
  1499. typesym :
  1500. st:=Ttypesym(sym).typedef.GetSymtable(gs_record);
  1501. else
  1502. ;
  1503. end
  1504. else
  1505. s:='';
  1506. end;
  1507. { now walk all recordsymtables }
  1508. while assigned(st) and (s<>'') do
  1509. begin
  1510. { load next field in base }
  1511. i:=pos('.',s);
  1512. if i=0 then
  1513. i:=255;
  1514. base:=Copy(s,1,i-1);
  1515. delete(s,1,i);
  1516. sym:=search_struct_member(tabstractrecorddef(st.defowner),base);
  1517. if not assigned(sym) then
  1518. begin
  1519. GetRecordOffsetSize:=false;
  1520. exit;
  1521. end;
  1522. st:=nil;
  1523. case sym.typ of
  1524. fieldvarsym :
  1525. with Tfieldvarsym(sym) do
  1526. begin
  1527. if not tabstractrecordsymtable(sym.owner).is_packed then
  1528. inc(Offset,fieldoffset)
  1529. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  1530. inc(Offset,fieldoffset div 8)
  1531. else
  1532. Message(asmr_e_packed_element);
  1533. size:=getsize;
  1534. case vardef.typ of
  1535. arraydef :
  1536. begin
  1537. { for arrays try to get the element size, take care of
  1538. multiple indexes }
  1539. harrdef:=tarraydef(vardef);
  1540. while assigned(harrdef.elementdef) and
  1541. (harrdef.elementdef.typ=arraydef) do
  1542. harrdef:=tarraydef(harrdef.elementdef);
  1543. if not is_packed_array(harrdef) then
  1544. size:=harrdef.elesize
  1545. else
  1546. begin
  1547. if (harrdef.elepackedbitsize mod 8) <> 0 then
  1548. Message(asmr_e_packed_element);
  1549. size := (harrdef.elepackedbitsize + 7) div 8;
  1550. end;
  1551. end;
  1552. recorddef :
  1553. st:=trecorddef(vardef).symtable;
  1554. objectdef :
  1555. st:=tobjectdef(vardef).symtable;
  1556. else
  1557. ;
  1558. end;
  1559. end;
  1560. procsym:
  1561. begin
  1562. st:=nil;
  1563. if Tprocsym(sym).ProcdefList.Count>1 then
  1564. Message(asmr_w_calling_overload_func);
  1565. procdef:=tprocdef(tprocsym(sym).ProcdefList[0]);
  1566. if (not needvmtofs) then
  1567. begin
  1568. mangledname:=procdef.mangledname;
  1569. end
  1570. else
  1571. begin
  1572. { can only get the vmtoffset of virtual methods }
  1573. if not(po_virtualmethod in procdef.procoptions) or
  1574. is_objectpascal_helper(procdef.struct) then
  1575. Message1(asmr_e_no_vmtoffset_possible,FullTypeName(procdef,nil))
  1576. else
  1577. begin
  1578. { size = sizeof(target_system_pointer) }
  1579. size:=sizeof(pint);
  1580. offset:=tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber)
  1581. end;
  1582. end;
  1583. { if something comes after the procsym, it's invalid assembler syntax }
  1584. GetRecordOffsetSize:=(s='');
  1585. exit;
  1586. end;
  1587. else
  1588. ;
  1589. end;
  1590. end;
  1591. { Support Field.Type as typecasting }
  1592. if (st=nil) and (s<>'') then
  1593. begin
  1594. asmsearchsym(s,sym,srsymtable);
  1595. if assigned(sym) and (sym.typ=typesym) then
  1596. begin
  1597. size:=ttypesym(sym).typedef.size;
  1598. s:='';
  1599. hastypecast:=true;
  1600. end;
  1601. end;
  1602. GetRecordOffsetSize:=(s='');
  1603. end;
  1604. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1605. var
  1606. sym : tsym;
  1607. srsymtable : TSymtable;
  1608. hs : string;
  1609. Begin
  1610. hl:=nil;
  1611. SearchLabel:=false;
  1612. { Check for pascal labels, which are case insensetive }
  1613. hs:=upper(s);
  1614. asmsearchsym(hs,sym,srsymtable);
  1615. if sym=nil then
  1616. exit;
  1617. case sym.typ of
  1618. labelsym :
  1619. begin
  1620. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1621. begin
  1622. Tlabelsym(sym).nonlocal:=true;
  1623. if emit then
  1624. include(current_procinfo.flags,pi_has_interproclabel);
  1625. end;
  1626. if not(assigned(tlabelsym(sym).asmblocklabel)) then
  1627. if Tlabelsym(sym).nonlocal then
  1628. current_asmdata.getglobaljumplabel(tlabelsym(sym).asmblocklabel)
  1629. else
  1630. current_asmdata.getjumplabel(tlabelsym(sym).asmblocklabel);
  1631. hl:=tlabelsym(sym).asmblocklabel;
  1632. if emit then
  1633. begin
  1634. if tlabelsym(sym).defined then
  1635. Message(sym_e_label_already_defined);
  1636. tlabelsym(sym).defined:=true;
  1637. hl.defined_in_asmstatement:=true
  1638. end
  1639. else
  1640. tlabelsym(sym).used:=true;
  1641. SearchLabel:=true;
  1642. end;
  1643. else
  1644. ;
  1645. end;
  1646. end;
  1647. {*************************************************************************}
  1648. { Instruction Generation Utilities }
  1649. {*************************************************************************}
  1650. Procedure ConcatString(p : TAsmList;s:string);
  1651. {*********************************************************************}
  1652. { PROCEDURE ConcatString(s:string); }
  1653. { Description: This routine adds the character chain pointed to in }
  1654. { s to the instruction linked list. }
  1655. {*********************************************************************}
  1656. Begin
  1657. p.concat(Tai_string.Create(s));
  1658. end;
  1659. Procedure ConcatConstant(p: TAsmList; value: tcgint; constsize:byte);
  1660. {*********************************************************************}
  1661. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1662. { Description: This routine adds the value constant to the current }
  1663. { instruction linked list. }
  1664. { maxvalue -> indicates the size of the data to initialize: }
  1665. { $ff -> create a byte node. }
  1666. { $ffff -> create a word node. }
  1667. { $ffffffff -> create a dword node. }
  1668. {*********************************************************************}
  1669. var
  1670. rangelo,rangehi : int64;
  1671. Begin
  1672. case constsize of
  1673. 1 :
  1674. begin
  1675. p.concat(Tai_const.Create_8bit(byte(value)));
  1676. rangelo:=low(shortint);
  1677. rangehi:=high(byte);
  1678. end;
  1679. 2 :
  1680. begin
  1681. p.concat(Tai_const.Create_16bit(word(value)));
  1682. rangelo:=low(smallint);
  1683. rangehi:=high(word);
  1684. end;
  1685. 4 :
  1686. begin
  1687. p.concat(Tai_const.Create_32bit(longint(value)));
  1688. rangelo:=low(longint);
  1689. rangehi:=high(cardinal);
  1690. end;
  1691. 8 :
  1692. begin
  1693. p.concat(Tai_const.Create_64bit(int64(value)));
  1694. rangelo:=0;
  1695. rangehi:=0;
  1696. end;
  1697. else
  1698. internalerror(200405011);
  1699. end;
  1700. { check for out of bounds }
  1701. if (rangelo<>0) and
  1702. ((value>rangehi) or (value<rangelo)) then
  1703. Message(asmr_e_constant_out_of_bounds);
  1704. end;
  1705. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:tcgint;constsize:byte;isofs:boolean);
  1706. begin
  1707. {$ifdef i8086}
  1708. { 'DW xx' as well as 'DW OFFSET xx' are just near pointers }
  1709. if constsize=2 then
  1710. p.concat(Tai_const.Createname_near(sym,l))
  1711. else if constsize=4 then
  1712. begin
  1713. if isofs then
  1714. begin
  1715. { 'DD OFFSET xx' is a 32-bit offset; since we don't produce 32-bit
  1716. relocations yet, just do a 16-bit one and set the high word to 0 }
  1717. p.concat(Tai_const.Createname_near(sym,l));
  1718. p.concat(Tai_const.Create_16bit(0));
  1719. end
  1720. else
  1721. { 'DD xx' is a far pointer }
  1722. p.concat(Tai_const.Createname_far(sym,l));
  1723. end
  1724. else
  1725. internalerror(2018020701);
  1726. {$else i8086}
  1727. p.concat(Tai_const.Createname(sym,l));
  1728. {$endif i8086}
  1729. end;
  1730. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  1731. {***********************************************************************}
  1732. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1733. { Description: This routine adds the value constant to the current }
  1734. { instruction linked list. }
  1735. { real_typ -> indicates the type of the real data to initialize: }
  1736. { s32real -> create a single node. }
  1737. { s64real -> create a double node. }
  1738. { s80real -> create an extended node. }
  1739. { s64bit -> create a comp node. }
  1740. { f32bit -> create a fixed node. (not used normally) }
  1741. {***********************************************************************}
  1742. Begin
  1743. case real_typ of
  1744. s32real : p.concat(tai_realconst.create_s32real(value));
  1745. s64real :
  1746. {$ifdef ARM}
  1747. if is_double_hilo_swapped then
  1748. p.concat(tai_realconst.create_s64real_hiloswapped(value))
  1749. else
  1750. {$endif ARM}
  1751. p.concat(tai_realconst.create_s64real(value));
  1752. s80real : p.concat(tai_realconst.create_s80real(value,s80floattype.size));
  1753. sc80real : p.concat(tai_realconst.create_s80real(value,sc80floattype.size));
  1754. s64comp : p.concat(tai_realconst.create_s64compreal(trunc(value)));
  1755. else
  1756. internalerror(2014050608);
  1757. end;
  1758. end;
  1759. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  1760. {*********************************************************************}
  1761. { PROCEDURE ConcatLabel }
  1762. { Description: This routine either emits a label or a labeled }
  1763. { instruction to the linked list of instructions. }
  1764. {*********************************************************************}
  1765. begin
  1766. p.concat(Tai_label.Create(l));
  1767. end;
  1768. procedure ConcatAlign(p:TAsmList;l:tcgint);
  1769. {*********************************************************************}
  1770. { PROCEDURE ConcatPublic }
  1771. { Description: This routine emits an global definition to the }
  1772. { linked list of instructions.(used by AT&T styled asm) }
  1773. {*********************************************************************}
  1774. begin
  1775. p.concat(Tai_align.Create(l));
  1776. end;
  1777. procedure ConcatPublic(p:TAsmList;const s : string);
  1778. {*********************************************************************}
  1779. { PROCEDURE ConcatPublic }
  1780. { Description: This routine emits an global definition to the }
  1781. { linked list of instructions.(used by AT&T styled asm) }
  1782. {*********************************************************************}
  1783. begin
  1784. p.concat(Tai_symbol.Createname_global(s,AT_LABEL,0,voidcodepointertype));
  1785. end;
  1786. procedure ConcatLocal(p:TAsmList;const s : string);
  1787. {*********************************************************************}
  1788. { PROCEDURE ConcatLocal }
  1789. { Description: This routine emits an local definition to the }
  1790. { linked list of instructions. }
  1791. {*********************************************************************}
  1792. begin
  1793. p.concat(Tai_symbol.Createname(s,AT_LABEL,0,voidcodepointertype));
  1794. end;
  1795. end.