rautils.pas 51 KB

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