rautils.pas 47 KB

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