rautils.pas 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  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;out srsym:tsym;out srsymtable:TSymtable);
  164. Function GetRecordOffsetSize(s:string;out Offset: tcgint;out Size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean):boolean;
  165. Function SearchType(const hs:string;out 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;constsize:byte;isofs:boolean);
  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. tmpprocinfo: tprocinfo;
  733. Begin
  734. SetupVar:=false;
  735. asmsearchsym(s,sym,srsymtable);
  736. if sym = nil then
  737. exit;
  738. if sym.typ=absolutevarsym then
  739. begin
  740. case tabsolutevarsym(sym).abstyp of
  741. tovar:
  742. begin
  743. { Only support simple loads }
  744. plist:=tabsolutevarsym(sym).ref.firstsym;
  745. if assigned(plist) and
  746. (plist^.sltype=sl_load) then
  747. begin
  748. setvarsize(tabstractvarsym(sym));
  749. size_set_from_absolute:=true;
  750. sym:=plist^.sym;
  751. { resolve the chain of array indexes (if there are any) }
  752. harrdef:=nil;
  753. while assigned(plist^.next) do
  754. begin
  755. plist:=plist^.next;
  756. if (plist^.sltype=sl_vec) and (tabstractvarsym(sym).vardef.typ=arraydef) then
  757. begin
  758. if harrdef=nil then
  759. harrdef:=tarraydef(tabstractvarsym(sym).vardef)
  760. else if harrdef.elementdef.typ=arraydef then
  761. harrdef:=tarraydef(harrdef.elementdef)
  762. else
  763. begin
  764. Message(asmr_e_unsupported_symbol_type);
  765. exit;
  766. end;
  767. if is_special_array(harrdef) then
  768. begin
  769. Message(asmr_e_unsupported_symbol_type);
  770. exit;
  771. end;
  772. if not is_packed_array(harrdef) then
  773. Inc(absoffset,asizeint(Int64(plist^.value-harrdef.lowrange))*harrdef.elesize)
  774. else if (Int64(plist^.value-harrdef.lowrange)*harrdef.elepackedbitsize mod 8)=0 then
  775. Inc(absoffset,asizeint(Int64(plist^.value-harrdef.lowrange)*harrdef.elepackedbitsize div 8))
  776. else
  777. Message(asmr_e_packed_element);
  778. end
  779. else
  780. begin
  781. Message(asmr_e_unsupported_symbol_type);
  782. exit;
  783. end;
  784. end;
  785. end
  786. else
  787. begin
  788. Message(asmr_e_unsupported_symbol_type);
  789. exit;
  790. end;
  791. end;
  792. toaddr:
  793. begin
  794. initref;
  795. opr.ref.offset:=tabsolutevarsym(sym).addroffset;
  796. setvarsize(tabstractvarsym(sym));
  797. size_set_from_absolute:=true;
  798. hasvar:=true;
  799. Result:=true;
  800. exit;
  801. end;
  802. else
  803. begin
  804. Message(asmr_e_unsupported_symbol_type);
  805. exit;
  806. end;
  807. end;
  808. end;
  809. case sym.typ of
  810. fieldvarsym :
  811. begin
  812. if not tabstractrecordsymtable(sym.owner).is_packed then
  813. setconst(absoffset+tfieldvarsym(sym).fieldoffset)
  814. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  815. setconst(absoffset+tfieldvarsym(sym).fieldoffset div 8)
  816. else
  817. Message(asmr_e_packed_element);
  818. if not size_set_from_absolute then
  819. setvarsize(tabstractvarsym(sym));
  820. hasvar:=true;
  821. SetupVar:=true;
  822. end;
  823. staticvarsym,
  824. localvarsym,
  825. paravarsym :
  826. begin
  827. { we always assume in asm statements that }
  828. { that the variable is valid. }
  829. tabstractvarsym(sym).varstate:=vs_readwritten;
  830. inc(tabstractvarsym(sym).refs);
  831. { variable can't be placed in a register }
  832. tabstractvarsym(sym).varregable:=vr_none;
  833. { and anything may happen with its address }
  834. tabstractvarsym(sym).addr_taken:=true;
  835. case sym.typ of
  836. staticvarsym :
  837. begin
  838. initref;
  839. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(sym).mangledname,AT_DATA);
  840. Inc(opr.ref.offset,absoffset);
  841. end;
  842. paravarsym,
  843. localvarsym :
  844. begin
  845. tmpprocinfo:=current_procinfo;
  846. while assigned(tmpprocinfo) do
  847. begin
  848. if (sym.owner=tmpprocinfo.procdef.localst) or
  849. (sym.owner=tmpprocinfo.procdef.parast) then
  850. begin
  851. tmpprocinfo.procdef.init_paraloc_info(calleeside);
  852. break;
  853. end;
  854. tmpprocinfo:=tmpprocinfo.parent;
  855. end;
  856. if opr.typ=OPR_REFERENCE then
  857. begin
  858. indexreg:=opr.ref.base;
  859. if opr.ref.index<>NR_NO then
  860. begin
  861. if indexreg=NR_NO then
  862. indexreg:=opr.ref.index
  863. else
  864. Message(asmr_e_multiple_index);
  865. end;
  866. end
  867. else
  868. indexreg:=NR_NO;
  869. opr.typ:=OPR_LOCAL;
  870. if assigned(current_procinfo.parent) and
  871. not(po_inline in current_procinfo.procdef.procoptions) and
  872. (sym.owner<>current_procinfo.procdef.localst) and
  873. (sym.owner<>current_procinfo.procdef.parast) and
  874. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  875. symtable_has_localvarsyms(current_procinfo.procdef.localst) then
  876. message1(asmr_e_local_para_unreachable,s);
  877. opr.localsym:=tabstractnormalvarsym(sym);
  878. opr.localsymofs:=absoffset;
  879. opr.localindexreg:=indexreg;
  880. opr.localscale:=0;
  881. opr.localgetoffset:=GetOffset;
  882. if paramanager.push_addr_param(tabstractvarsym(sym).varspez,tabstractvarsym(sym).vardef,current_procinfo.procdef.proccalloption) then
  883. SetSize(sizeof(pint),false);
  884. end;
  885. end;
  886. if not size_set_from_absolute then
  887. setvarsize(tabstractvarsym(sym));
  888. hasvar:=true;
  889. SetupVar:=true;
  890. Exit;
  891. end;
  892. constsym :
  893. begin
  894. if tconstsym(sym).consttyp=constord then
  895. begin
  896. setconst(tconstsym(sym).value.valueord.svalue);
  897. SetupVar:=true;
  898. Exit;
  899. end;
  900. end;
  901. typesym :
  902. begin
  903. if ttypesym(sym).typedef.typ in [recorddef,objectdef] then
  904. begin
  905. setconst(0);
  906. SetupVar:=TRUE;
  907. Exit;
  908. end;
  909. end;
  910. procsym :
  911. begin
  912. if Tprocsym(sym).ProcdefList.Count>1 then
  913. Message(asmr_w_calling_overload_func);
  914. case opr.typ of
  915. OPR_REFERENCE:
  916. begin
  917. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION);
  918. Inc(opr.ref.offset,absoffset);
  919. {$ifdef i8086}
  920. opr.ref_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  921. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  922. {$endif i8086}
  923. end;
  924. OPR_NONE:
  925. begin
  926. opr.typ:=OPR_SYMBOL;
  927. opr.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION);
  928. {$ifdef i8086}
  929. opr.sym_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  930. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  931. {$endif i8086}
  932. opr.symofs:=absoffset;
  933. end;
  934. else
  935. Message(asmr_e_invalid_operand_type);
  936. end;
  937. hasproc:=true;
  938. hasvar:=true;
  939. SetupVar:=TRUE;
  940. Exit;
  941. end;
  942. {$ifdef i8086}
  943. labelsym :
  944. begin
  945. case opr.typ of
  946. OPR_REFERENCE:
  947. begin
  948. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tlabelsym(sym).mangledname,AT_FUNCTION);
  949. Inc(opr.ref.offset,absoffset);
  950. if opr.ref.segment=NR_NO then
  951. opr.ref.segment:=NR_CS;
  952. end;
  953. else
  954. begin
  955. Message(asmr_e_unsupported_symbol_type);
  956. exit;
  957. end;
  958. end;
  959. haslabelref:=true;
  960. hasvar:=true;
  961. SetupVar:=TRUE;
  962. Exit;
  963. end
  964. {$endif i8086}
  965. else
  966. begin
  967. Message(asmr_e_unsupported_symbol_type);
  968. exit;
  969. end;
  970. end;
  971. end;
  972. procedure TOperand.InitRef;
  973. {*********************************************************************}
  974. { Description: This routine first check if the opcode is of }
  975. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  976. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  977. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  978. { to point to the default segment. }
  979. {*********************************************************************}
  980. var
  981. l : aint;
  982. hsymofs : aint;
  983. hsymbol : tasmsymbol;
  984. reg : tregister;
  985. hsym_farprocentry: Boolean;
  986. Begin
  987. case opr.typ of
  988. OPR_REFERENCE :
  989. exit;
  990. OPR_CONSTANT :
  991. begin
  992. l:=opr.val;
  993. opr.typ:=OPR_REFERENCE;
  994. Fillchar(opr.ref,sizeof(treference),0);
  995. opr.Ref.Offset:=l;
  996. opr.varsize:=0;
  997. opr.constoffset:=0;
  998. opr.ref_farproc_entry:=false;
  999. end;
  1000. OPR_NONE :
  1001. begin
  1002. opr.typ:=OPR_REFERENCE;
  1003. opr.varsize:=0;
  1004. opr.constoffset:=0;
  1005. opr.ref_farproc_entry:=false;
  1006. Fillchar(opr.ref,sizeof(treference),0);
  1007. end;
  1008. OPR_REGISTER :
  1009. begin
  1010. reg:=opr.reg;
  1011. opr.typ:=OPR_REFERENCE;
  1012. opr.varsize:=0;
  1013. opr.constoffset:=0;
  1014. opr.ref_farproc_entry:=false;
  1015. Fillchar(opr.ref,sizeof(treference),0);
  1016. opr.Ref.base:=reg;
  1017. end;
  1018. OPR_SYMBOL :
  1019. begin
  1020. hsymbol:=opr.symbol;
  1021. hsymofs:=opr.symofs;
  1022. hsym_farprocentry:=opr.sym_farproc_entry;
  1023. opr.typ:=OPR_REFERENCE;
  1024. opr.varsize:=0;
  1025. opr.constoffset:=0;
  1026. Fillchar(opr.ref,sizeof(treference),0);
  1027. opr.ref.symbol:=hsymbol;
  1028. opr.ref.offset:=hsymofs;
  1029. opr.ref_farproc_entry:=hsym_farprocentry;
  1030. end;
  1031. else
  1032. InitRefError;
  1033. end;
  1034. end;
  1035. procedure TOperand.InitRefConvertLocal;
  1036. var
  1037. localvarsize,localconstoffset: asizeint;
  1038. localsym:tabstractnormalvarsym;
  1039. localsymofs:aint;
  1040. localindexreg:tregister;
  1041. localscale:byte;
  1042. begin
  1043. if opr.typ=OPR_LOCAL then
  1044. begin
  1045. if AsmRegisterPara(opr.localsym) and
  1046. not opr.localgetoffset then
  1047. begin
  1048. localvarsize:=opr.localvarsize;
  1049. localconstoffset:=opr.localconstoffset;
  1050. localsym:=opr.localsym;
  1051. localsymofs:=opr.localsymofs;
  1052. localindexreg:=opr.localindexreg;
  1053. localscale:=opr.localscale;;
  1054. opr.typ:=OPR_REFERENCE;
  1055. hasvar:=false;
  1056. Fillchar(opr.ref,sizeof(treference),0);
  1057. opr.varsize:=localvarsize;
  1058. opr.constoffset:=localconstoffset;
  1059. opr.ref_farproc_entry:=false;
  1060. opr.ref.base:=tparavarsym(localsym).paraloc[calleeside].Location^.register;
  1061. opr.ref.offset:=localsymofs;
  1062. opr.ref.index:=localindexreg;
  1063. opr.ref.scalefactor:=localscale;
  1064. end
  1065. else
  1066. InitRefError;
  1067. end
  1068. else
  1069. InitRef;
  1070. end;
  1071. procedure TOperand.InitRefError;
  1072. begin
  1073. Message(asmr_e_invalid_operand_type);
  1074. { Recover }
  1075. opr.typ:=OPR_REFERENCE;
  1076. opr.varsize:=0;
  1077. opr.constoffset:=0;
  1078. opr.ref_farproc_entry:=false;
  1079. Fillchar(opr.ref,sizeof(treference),0);
  1080. end;
  1081. Function TOperand.CheckOperand: boolean;
  1082. {*********************************************************************}
  1083. { Description: This routine checks if the operand is of }
  1084. { valid, and returns false if it isn't. Does nothing by default. }
  1085. {*********************************************************************}
  1086. begin
  1087. result:=true;
  1088. end;
  1089. {****************************************************************************
  1090. TInstruction
  1091. ****************************************************************************}
  1092. constructor TInstruction.create(optype : tcoperand);
  1093. var
  1094. i : longint;
  1095. Begin
  1096. { these field are set to 0 anyways by the constructor helper (FK)
  1097. Opcode:=A_NONE;
  1098. Condition:=C_NONE;
  1099. Ops:=0;
  1100. }
  1101. filepos:=current_filepos;
  1102. for i:=1 to max_operands do
  1103. Operands[i]:=optype.create;
  1104. Labeled:=false;
  1105. end;
  1106. destructor TInstruction.destroy;
  1107. var
  1108. i : longint;
  1109. Begin
  1110. for i:=1 to max_operands do
  1111. Operands[i].free;
  1112. end;
  1113. function TInstruction.ConcatInstruction(p:TAsmList) : tai;
  1114. var
  1115. ai : taicpu;
  1116. i : longint;
  1117. begin
  1118. for i:=1 to Ops do
  1119. operands[i].CheckOperand;
  1120. ai:=taicpu.op_none(opcode);
  1121. ai.fileinfo:=filepos;
  1122. ai.Ops:=Ops;
  1123. ai.Allocate_oper(Ops);
  1124. for i:=1 to Ops do
  1125. with operands[i].opr do
  1126. begin
  1127. case typ of
  1128. OPR_CONSTANT :
  1129. ai.loadconst(i-1,val);
  1130. OPR_REGISTER:
  1131. ai.loadreg(i-1,reg);
  1132. OPR_SYMBOL:
  1133. ai.loadsymbol(i-1,symbol,symofs);
  1134. OPR_LOCAL :
  1135. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1136. localscale,localgetoffset,localforceref);
  1137. OPR_REFERENCE:
  1138. ai.loadref(i-1,ref);
  1139. {$ifdef m68k}
  1140. OPR_REGSET:
  1141. ai.loadregset(i-1,regsetdata,regsetaddr,regsetfpu);
  1142. OPR_REGPAIR:
  1143. ai.loadregpair(i-1,reghi,reglo);
  1144. {$endif}
  1145. {$ifdef ARM}
  1146. OPR_REGSET:
  1147. ai.loadregset(i-1,regtype,subreg,regset,usermode);
  1148. OPR_MODEFLAGS:
  1149. ai.loadmodeflags(i-1,flags);
  1150. OPR_SPECIALREG:
  1151. ai.loadspecialreg(i-1,specialreg,specialregflags);
  1152. {$endif ARM}
  1153. {$if defined(arm) or defined(aarch64)}
  1154. OPR_SHIFTEROP:
  1155. ai.loadshifterop(i-1,shifterop);
  1156. OPR_COND:
  1157. ai.loadconditioncode(i-1,cc);
  1158. {$endif arm or aarch64}
  1159. { ignore wrong operand }
  1160. OPR_NONE:
  1161. ;
  1162. else
  1163. internalerror(200501051);
  1164. end;
  1165. end;
  1166. ai.SetCondition(condition);
  1167. { Concat the opcode or give an error }
  1168. if assigned(ai) then
  1169. p.concat(ai)
  1170. else
  1171. Message(asmr_e_invalid_opcode_and_operand);
  1172. result:=ai;
  1173. end;
  1174. {****************************************************************************
  1175. Symbol table helper routines
  1176. ****************************************************************************}
  1177. procedure AsmSearchSym(const s:string;out srsym:tsym;out srsymtable:TSymtable);
  1178. var
  1179. i : integer;
  1180. begin
  1181. i:=pos('.',s);
  1182. { allow unit.identifier }
  1183. if i>0 then
  1184. begin
  1185. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1186. if assigned(srsym) then
  1187. begin
  1188. if (srsym.typ=unitsym) and
  1189. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  1190. srsym.owner.iscurrentunit then
  1191. searchsym_in_module(tunitsym(srsym).module,Copy(s,i+1,255),srsym,srsymtable)
  1192. else
  1193. begin
  1194. srsym:=nil;
  1195. srsymtable:=nil;
  1196. end;
  1197. end;
  1198. end
  1199. else
  1200. searchsym(s,srsym,srsymtable);
  1201. end;
  1202. Function SearchType(const hs:string;out size:tcgint): Boolean;
  1203. var
  1204. srsym : tsym;
  1205. srsymtable : TSymtable;
  1206. begin
  1207. result:=false;
  1208. size:=0;
  1209. asmsearchsym(hs,srsym,srsymtable);
  1210. if assigned(srsym) and
  1211. (srsym.typ=typesym) then
  1212. begin
  1213. size:=ttypesym(srsym).typedef.size;
  1214. result:=true;
  1215. end;
  1216. end;
  1217. Function SearchRecordType(const s:string): boolean;
  1218. var
  1219. srsym : tsym;
  1220. srsymtable : TSymtable;
  1221. Begin
  1222. SearchRecordType:=false;
  1223. { Check the constants in symtable }
  1224. asmsearchsym(s,srsym,srsymtable);
  1225. if srsym <> nil then
  1226. Begin
  1227. case srsym.typ of
  1228. typesym :
  1229. begin
  1230. if ttypesym(srsym).typedef.typ in [recorddef,objectdef] then
  1231. begin
  1232. SearchRecordType:=true;
  1233. exit;
  1234. end;
  1235. end;
  1236. fieldvarsym :
  1237. begin
  1238. if (tfieldvarsym(srsym).vardef.typ in [recorddef,objectdef]) then
  1239. begin
  1240. SearchRecordType:=true;
  1241. exit;
  1242. end;
  1243. end;
  1244. end;
  1245. end;
  1246. end;
  1247. Function SearchIConstant(const s:string; var l:tcgint): boolean;
  1248. {**********************************************************************}
  1249. { Description: Searches for a CONSTANT of name s in either the local }
  1250. { symbol list, then in the global symbol list, and returns the value }
  1251. { of that constant in l. Returns TRUE if successfull, if not found, }
  1252. { or if the constant is not of correct type, then returns FALSE }
  1253. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1254. { respectively. }
  1255. {**********************************************************************}
  1256. var
  1257. srsym : tsym;
  1258. srsymtable : TSymtable;
  1259. Begin
  1260. SearchIConstant:=false;
  1261. { check for TRUE or FALSE reserved words first }
  1262. if s = 'TRUE' then
  1263. Begin
  1264. SearchIConstant:=TRUE;
  1265. l:=1;
  1266. exit;
  1267. end;
  1268. if s = 'FALSE' then
  1269. Begin
  1270. SearchIConstant:=TRUE;
  1271. l:=0;
  1272. exit;
  1273. end;
  1274. { Check the constants in symtable }
  1275. asmsearchsym(s,srsym,srsymtable);
  1276. if srsym <> nil then
  1277. Begin
  1278. case srsym.typ of
  1279. constsym :
  1280. begin
  1281. if tconstsym(srsym).consttyp=constord then
  1282. Begin
  1283. l:=tconstsym(srsym).value.valueord.svalue;
  1284. SearchIConstant:=TRUE;
  1285. exit;
  1286. end;
  1287. end;
  1288. enumsym:
  1289. Begin
  1290. l:=tenumsym(srsym).value;
  1291. SearchIConstant:=TRUE;
  1292. exit;
  1293. end;
  1294. end;
  1295. end;
  1296. end;
  1297. function AsmRegisterPara(sym: tabstractnormalvarsym): boolean;
  1298. begin
  1299. result:=
  1300. (po_assembler in current_procinfo.procdef.procoptions) and
  1301. (sym.typ=paravarsym) and
  1302. (tparavarsym(sym).paraloc[calleeside].Location^.Loc=LOC_REGISTER);
  1303. end;
  1304. Function GetRecordOffsetSize(s:string;out Offset: tcgint;out Size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean):boolean;
  1305. { search and returns the offset and size of records/objects of the base }
  1306. { with field name setup in field. }
  1307. { returns FALSE if not found. }
  1308. { used when base is a variable or a typed constant name. }
  1309. var
  1310. st : TSymtable;
  1311. harrdef : tarraydef;
  1312. sym : tsym;
  1313. srsymtable : TSymtable;
  1314. i : longint;
  1315. base : string;
  1316. procdef: tprocdef;
  1317. Begin
  1318. GetRecordOffsetSize:=FALSE;
  1319. Offset:=0;
  1320. Size:=0;
  1321. mangledname:='';
  1322. hastypecast:=false;
  1323. i:=pos('.',s);
  1324. if i=0 then
  1325. i:=255;
  1326. base:=Copy(s,1,i-1);
  1327. delete(s,1,i);
  1328. if base='SELF' then
  1329. st:=current_structdef.symtable
  1330. else
  1331. begin
  1332. asmsearchsym(base,sym,srsymtable);
  1333. st:=nil;
  1334. { we can start with a var,type,typedconst }
  1335. if assigned(sym) then
  1336. case sym.typ of
  1337. staticvarsym,
  1338. localvarsym,
  1339. paravarsym :
  1340. st:=Tabstractvarsym(sym).vardef.GetSymtable(gs_record);
  1341. typesym :
  1342. st:=Ttypesym(sym).typedef.GetSymtable(gs_record);
  1343. end
  1344. else
  1345. s:='';
  1346. end;
  1347. { now walk all recordsymtables }
  1348. while assigned(st) and (s<>'') do
  1349. begin
  1350. { load next field in base }
  1351. i:=pos('.',s);
  1352. if i=0 then
  1353. i:=255;
  1354. base:=Copy(s,1,i-1);
  1355. delete(s,1,i);
  1356. sym:=search_struct_member(tabstractrecorddef(st.defowner),base);
  1357. if not assigned(sym) then
  1358. begin
  1359. GetRecordOffsetSize:=false;
  1360. exit;
  1361. end;
  1362. st:=nil;
  1363. case sym.typ of
  1364. fieldvarsym :
  1365. with Tfieldvarsym(sym) do
  1366. begin
  1367. if not tabstractrecordsymtable(sym.owner).is_packed then
  1368. inc(Offset,fieldoffset)
  1369. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  1370. inc(Offset,fieldoffset div 8)
  1371. else
  1372. Message(asmr_e_packed_element);
  1373. size:=getsize;
  1374. case vardef.typ of
  1375. arraydef :
  1376. begin
  1377. { for arrays try to get the element size, take care of
  1378. multiple indexes }
  1379. harrdef:=tarraydef(vardef);
  1380. while assigned(harrdef.elementdef) and
  1381. (harrdef.elementdef.typ=arraydef) do
  1382. harrdef:=tarraydef(harrdef.elementdef);
  1383. if not is_packed_array(harrdef) then
  1384. size:=harrdef.elesize
  1385. else
  1386. begin
  1387. if (harrdef.elepackedbitsize mod 8) <> 0 then
  1388. Message(asmr_e_packed_element);
  1389. size := (harrdef.elepackedbitsize + 7) div 8;
  1390. end;
  1391. end;
  1392. recorddef :
  1393. st:=trecorddef(vardef).symtable;
  1394. objectdef :
  1395. st:=tobjectdef(vardef).symtable;
  1396. end;
  1397. end;
  1398. procsym:
  1399. begin
  1400. st:=nil;
  1401. if Tprocsym(sym).ProcdefList.Count>1 then
  1402. Message(asmr_w_calling_overload_func);
  1403. procdef:=tprocdef(tprocsym(sym).ProcdefList[0]);
  1404. if (not needvmtofs) then
  1405. begin
  1406. mangledname:=procdef.mangledname;
  1407. end
  1408. else
  1409. begin
  1410. { can only get the vmtoffset of virtual methods }
  1411. if not(po_virtualmethod in procdef.procoptions) or
  1412. is_objectpascal_helper(procdef.struct) then
  1413. Message1(asmr_e_no_vmtoffset_possible,FullTypeName(procdef,nil))
  1414. else
  1415. begin
  1416. { size = sizeof(target_system_pointer) }
  1417. size:=sizeof(pint);
  1418. offset:=tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber)
  1419. end;
  1420. end;
  1421. { if something comes after the procsym, it's invalid assembler syntax }
  1422. GetRecordOffsetSize:=(s='');
  1423. exit;
  1424. end;
  1425. end;
  1426. end;
  1427. { Support Field.Type as typecasting }
  1428. if (st=nil) and (s<>'') then
  1429. begin
  1430. asmsearchsym(s,sym,srsymtable);
  1431. if assigned(sym) and (sym.typ=typesym) then
  1432. begin
  1433. size:=ttypesym(sym).typedef.size;
  1434. s:='';
  1435. hastypecast:=true;
  1436. end;
  1437. end;
  1438. GetRecordOffsetSize:=(s='');
  1439. end;
  1440. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1441. var
  1442. sym : tsym;
  1443. srsymtable : TSymtable;
  1444. hs : string;
  1445. Begin
  1446. hl:=nil;
  1447. SearchLabel:=false;
  1448. { Check for pascal labels, which are case insensetive }
  1449. hs:=upper(s);
  1450. asmsearchsym(hs,sym,srsymtable);
  1451. if sym=nil then
  1452. exit;
  1453. case sym.typ of
  1454. labelsym :
  1455. begin
  1456. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1457. begin
  1458. Tlabelsym(sym).nonlocal:=true;
  1459. if emit then
  1460. exclude(current_procinfo.procdef.procoptions,po_inline);
  1461. end;
  1462. if not(assigned(tlabelsym(sym).asmblocklabel)) then
  1463. if Tlabelsym(sym).nonlocal then
  1464. current_asmdata.getglobaljumplabel(tlabelsym(sym).asmblocklabel)
  1465. else
  1466. current_asmdata.getjumplabel(tlabelsym(sym).asmblocklabel);
  1467. hl:=tlabelsym(sym).asmblocklabel;
  1468. if emit then
  1469. begin
  1470. if tlabelsym(sym).defined then
  1471. Message(sym_e_label_already_defined);
  1472. tlabelsym(sym).defined:=true
  1473. end
  1474. else
  1475. tlabelsym(sym).used:=true;
  1476. SearchLabel:=true;
  1477. end;
  1478. end;
  1479. end;
  1480. {*************************************************************************}
  1481. { Instruction Generation Utilities }
  1482. {*************************************************************************}
  1483. Procedure ConcatString(p : TAsmList;s:string);
  1484. {*********************************************************************}
  1485. { PROCEDURE ConcatString(s:string); }
  1486. { Description: This routine adds the character chain pointed to in }
  1487. { s to the instruction linked list. }
  1488. {*********************************************************************}
  1489. Begin
  1490. p.concat(Tai_string.Create(s));
  1491. end;
  1492. Procedure ConcatConstant(p: TAsmList; value: tcgint; constsize:byte);
  1493. {*********************************************************************}
  1494. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1495. { Description: This routine adds the value constant to the current }
  1496. { instruction linked list. }
  1497. { maxvalue -> indicates the size of the data to initialize: }
  1498. { $ff -> create a byte node. }
  1499. { $ffff -> create a word node. }
  1500. { $ffffffff -> create a dword node. }
  1501. {*********************************************************************}
  1502. var
  1503. rangelo,rangehi : int64;
  1504. Begin
  1505. case constsize of
  1506. 1 :
  1507. begin
  1508. p.concat(Tai_const.Create_8bit(byte(value)));
  1509. rangelo:=low(shortint);
  1510. rangehi:=high(byte);
  1511. end;
  1512. 2 :
  1513. begin
  1514. p.concat(Tai_const.Create_16bit(word(value)));
  1515. rangelo:=low(smallint);
  1516. rangehi:=high(word);
  1517. end;
  1518. 4 :
  1519. begin
  1520. p.concat(Tai_const.Create_32bit(longint(value)));
  1521. rangelo:=low(longint);
  1522. rangehi:=high(cardinal);
  1523. end;
  1524. 8 :
  1525. begin
  1526. p.concat(Tai_const.Create_64bit(int64(value)));
  1527. rangelo:=0;
  1528. rangehi:=0;
  1529. end;
  1530. else
  1531. internalerror(200405011);
  1532. end;
  1533. { check for out of bounds }
  1534. if (rangelo<>0) and
  1535. ((value>rangehi) or (value<rangelo)) then
  1536. Message(asmr_e_constant_out_of_bounds);
  1537. end;
  1538. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:tcgint;constsize:byte;isofs:boolean);
  1539. begin
  1540. {$ifdef i8086}
  1541. { 'DW xx' as well as 'DW OFFSET xx' are just near pointers }
  1542. if constsize=2 then
  1543. p.concat(Tai_const.Createname_near(sym,l))
  1544. else if constsize=4 then
  1545. begin
  1546. if isofs then
  1547. begin
  1548. { 'DD OFFSET xx' is a 32-bit offset; since we don't produce 32-bit
  1549. relocations yet, just do a 16-bit one and set the high word to 0 }
  1550. p.concat(Tai_const.Createname_near(sym,l));
  1551. p.concat(Tai_const.Create_16bit(0));
  1552. end
  1553. else
  1554. { 'DD xx' is a far pointer }
  1555. p.concat(Tai_const.Createname_far(sym,l));
  1556. end
  1557. else
  1558. internalerror(2018020701);
  1559. {$else i8086}
  1560. p.concat(Tai_const.Createname(sym,l));
  1561. {$endif i8086}
  1562. end;
  1563. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  1564. {***********************************************************************}
  1565. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1566. { Description: This routine adds the value constant to the current }
  1567. { instruction linked list. }
  1568. { real_typ -> indicates the type of the real data to initialize: }
  1569. { s32real -> create a single node. }
  1570. { s64real -> create a double node. }
  1571. { s80real -> create an extended node. }
  1572. { s64bit -> create a comp node. }
  1573. { f32bit -> create a fixed node. (not used normally) }
  1574. {***********************************************************************}
  1575. Begin
  1576. case real_typ of
  1577. s32real : p.concat(tai_realconst.create_s32real(value));
  1578. s64real :
  1579. {$ifdef ARM}
  1580. if is_double_hilo_swapped then
  1581. p.concat(tai_realconst.create_s64real_hiloswapped(value))
  1582. else
  1583. {$endif ARM}
  1584. p.concat(tai_realconst.create_s64real(value));
  1585. s80real : p.concat(tai_realconst.create_s80real(value,s80floattype.size));
  1586. sc80real : p.concat(tai_realconst.create_s80real(value,sc80floattype.size));
  1587. s64comp : p.concat(tai_realconst.create_s64compreal(trunc(value)));
  1588. else
  1589. internalerror(2014050608);
  1590. end;
  1591. end;
  1592. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  1593. {*********************************************************************}
  1594. { PROCEDURE ConcatLabel }
  1595. { Description: This routine either emits a label or a labeled }
  1596. { instruction to the linked list of instructions. }
  1597. {*********************************************************************}
  1598. begin
  1599. p.concat(Tai_label.Create(l));
  1600. end;
  1601. procedure ConcatAlign(p:TAsmList;l:tcgint);
  1602. {*********************************************************************}
  1603. { PROCEDURE ConcatPublic }
  1604. { Description: This routine emits an global definition to the }
  1605. { linked list of instructions.(used by AT&T styled asm) }
  1606. {*********************************************************************}
  1607. begin
  1608. p.concat(Tai_align.Create(l));
  1609. end;
  1610. procedure ConcatPublic(p:TAsmList;const s : string);
  1611. {*********************************************************************}
  1612. { PROCEDURE ConcatPublic }
  1613. { Description: This routine emits an global definition to the }
  1614. { linked list of instructions.(used by AT&T styled asm) }
  1615. {*********************************************************************}
  1616. begin
  1617. p.concat(Tai_symbol.Createname_global(s,AT_LABEL,0,voidcodepointertype));
  1618. end;
  1619. procedure ConcatLocal(p:TAsmList;const s : string);
  1620. {*********************************************************************}
  1621. { PROCEDURE ConcatLocal }
  1622. { Description: This routine emits an local definition to the }
  1623. { linked list of instructions. }
  1624. {*********************************************************************}
  1625. begin
  1626. p.concat(Tai_symbol.Createname(s,AT_LABEL,0,voidcodepointertype));
  1627. end;
  1628. end.