rautils.pas 47 KB

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