rautils.pas 46 KB

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