rautils.pas 46 KB

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