rautils.pas 48 KB

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