2
0

rautils.pas 49 KB

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