rautils.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. Interface
  20. Uses
  21. strings,
  22. cobjects,
  23. globtype,types,systems,verbose,globals,files,
  24. symconst,symtable,aasm,cpubase,cpuasm
  25. {$ifdef NEWCG}
  26. ,cgbase
  27. {$else}
  28. ,hcodegen
  29. {$endif}
  30. ;
  31. Const
  32. RPNMax = 10; { I think you only need 4, but just to be safe }
  33. OpMax = 25;
  34. maxoperands = 3; { Maximum operands for assembler instructions }
  35. {---------------------------------------------------------------------
  36. Local Label Management
  37. ---------------------------------------------------------------------}
  38. Type
  39. { Each local label has this structure associated with it }
  40. PLocalLabel = ^TLocalLabel;
  41. TLocalLabel = object(TNamedIndexObject)
  42. Emitted : boolean;
  43. constructor Init(const n:string);
  44. function Getpasmlabel:pasmlabel;
  45. private
  46. lab : pasmlabel;
  47. end;
  48. PLocalLabelList = ^TLocalLabelList;
  49. TLocalLabelList = Object(TDictionary)
  50. procedure CheckEmitted;
  51. end;
  52. var
  53. LocalLabelList : PLocalLabelList;
  54. function CreateLocalLabel(const s: string; var hl: pasmlabel; emit:boolean):boolean;
  55. Function SearchLabel(const s: string; var hl: pasmlabel;emit:boolean): boolean;
  56. {---------------------------------------------------------------------
  57. Instruction management
  58. ---------------------------------------------------------------------}
  59. type
  60. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_REFERENCE,OPR_REGISTER);
  61. TOprRec = record
  62. case typ:TOprType of
  63. OPR_NONE : ();
  64. OPR_CONSTANT : (val:longint);
  65. OPR_SYMBOL : (symbol:PAsmSymbol;symofs:longint);
  66. OPR_REFERENCE : (ref:treference);
  67. OPR_REGISTER : (reg:tregister);
  68. end;
  69. POperand = ^TOperand;
  70. TOperand = object
  71. size : topsize;
  72. hastype, { if the operand has typecasted variable }
  73. hasvar : boolean; { if the operand is loaded with a variable }
  74. opr : TOprRec;
  75. constructor init;
  76. destructor done;virtual;
  77. Procedure BuildOperand;virtual;
  78. Procedure SetSize(_size:longint;force:boolean);
  79. Procedure SetCorrectSize(opcode:tasmop);virtual;
  80. Function SetupResult:boolean;
  81. Function SetupSelf:boolean;
  82. Function SetupOldEBP:boolean;
  83. Function SetupVar(const hs:string;GetOffset : boolean): Boolean;
  84. Function SetupDirectVar(const hs:string): Boolean;
  85. Procedure InitRef;
  86. end;
  87. PInstruction = ^TInstruction;
  88. TInstruction = object
  89. opcode : tasmop;
  90. opsize : topsize;
  91. condition : tasmcond;
  92. ops : byte;
  93. operands : array[1..maxoperands] of POperand;
  94. constructor init;
  95. destructor done;virtual;
  96. Procedure InitOperands;virtual;
  97. Procedure BuildOpcode;virtual;
  98. procedure ConcatInstruction(p:PAasmoutput);virtual;
  99. Procedure SwapOperands;
  100. end;
  101. {---------------------------------------------------------------------}
  102. { Expression parser types }
  103. {---------------------------------------------------------------------}
  104. TExprOperator = record
  105. ch: char; { operator }
  106. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  107. end;
  108. String15 = String[15];
  109. {**********************************************************************}
  110. { The following operators are supported: }
  111. { '+' : addition }
  112. { '-' : subtraction }
  113. { '*' : multiplication }
  114. { '/' : modulo division }
  115. { '^' : exclusive or }
  116. { '<' : shift left }
  117. { '>' : shift right }
  118. { '&' : bitwise and }
  119. { '|' : bitwise or }
  120. { '~' : bitwise complement }
  121. { '%' : modulo division }
  122. { nnn: longint numbers }
  123. { ( and ) parenthesis }
  124. {**********************************************************************}
  125. TExprParse = Object
  126. public
  127. Constructor Init;
  128. Destructor Done;
  129. Function Evaluate(Expr: String): longint;
  130. Function Priority(_Operator: Char): Integer; virtual;
  131. private
  132. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  133. RPNTop : Integer;
  134. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  135. OpTop : Integer;
  136. Procedure RPNPush(Num: Longint);
  137. Function RPNPop: Longint;
  138. Procedure RPNCalc(token: String15; prefix: boolean);
  139. Procedure OpPush(_Operator: char; prefix: boolean);
  140. { In reality returns TExprOperaotr }
  141. Procedure OpPop(var _Operator:TExprOperator);
  142. end;
  143. { Evaluate an expression string to a longint }
  144. Function CalculateExpression(const expression: string): longint;
  145. {---------------------------------------------------------------------}
  146. { String routines }
  147. {---------------------------------------------------------------------}
  148. Function ValDecimal(const S:String):longint;
  149. Function ValOctal(const S:String):longint;
  150. Function ValBinary(const S:String):longint;
  151. Function ValHexaDecimal(const S:String):longint;
  152. Function PadZero(Var s: String; n: byte): Boolean;
  153. Function EscapeToPascal(const s:string): string;
  154. {---------------------------------------------------------------------
  155. Symbol helper routines
  156. ---------------------------------------------------------------------}
  157. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  158. Function SearchType(const hs:string): Boolean;
  159. Function SearchRecordType(const s:string): boolean;
  160. Function SearchIConstant(const s:string; var l:longint): boolean;
  161. {---------------------------------------------------------------------
  162. Instruction generation routines
  163. ---------------------------------------------------------------------}
  164. Procedure ConcatPasString(p : paasmoutput;s:string);
  165. Procedure ConcatDirect(p : paasmoutput;s:string);
  166. Procedure ConcatLabel(p: paasmoutput;var l : pasmlabel);
  167. Procedure ConcatConstant(p : paasmoutput;value: longint; maxvalue: longint);
  168. Procedure ConcatConstSymbol(p : paasmoutput;const sym:string;l:longint);
  169. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  170. Procedure ConcatString(p : paasmoutput;s:string);
  171. procedure ConcatAlign(p:paasmoutput;l:longint);
  172. Procedure ConcatPublic(p:paasmoutput;const s : string);
  173. Procedure ConcatLocal(p:paasmoutput;const s : string);
  174. Procedure ConcatGlobalBss(const s : string;size : longint);
  175. Procedure ConcatLocalBss(const s : string;size : longint);
  176. Implementation
  177. {*************************************************************************
  178. TExprParse
  179. *************************************************************************}
  180. Constructor TExprParse.Init;
  181. Begin
  182. end;
  183. Procedure TExprParse.RPNPush(Num : longint);
  184. { Add an operand to the top of the RPN stack }
  185. begin
  186. if RPNTop < RPNMax then
  187. begin
  188. Inc(RPNTop);
  189. RPNStack[RPNTop]:=Num;
  190. end
  191. else
  192. Message(asmr_e_expr_illegal);
  193. end;
  194. Function TExprParse.RPNPop : longint;
  195. { Get the operand at the top of the RPN stack }
  196. begin
  197. if RPNTop > 0 then
  198. begin
  199. RPNPop:=RPNStack[RPNTop];
  200. Dec(RPNTop);
  201. end
  202. else
  203. Message(asmr_e_expr_illegal);
  204. end;
  205. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  206. Var
  207. Temp : longint;
  208. LocalError : Integer;
  209. begin
  210. { Handle operators }
  211. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  212. Case Token[1] of
  213. '+' :
  214. Begin
  215. if not prefix then
  216. RPNPush(RPNPop + RPNPop);
  217. end;
  218. '-' :
  219. Begin
  220. if prefix then
  221. RPNPush(-(RPNPop))
  222. else
  223. RPNPush(RPNPop - RPNPop);
  224. end;
  225. '*' : RPNPush(RPNPop * RPNPop);
  226. '&' : RPNPush(RPNPop AND RPNPop);
  227. '|' : RPNPush(RPNPop OR RPNPop);
  228. '~' : RPNPush(NOT RPNPop);
  229. '<' : RPNPush(RPNPop SHL RPNPop);
  230. '>' : RPNPush(RPNPop SHR RPNPop);
  231. '%' :
  232. begin
  233. Temp:=RPNPop;
  234. if Temp <> 0 then
  235. RPNPush(RPNPop mod Temp)
  236. else
  237. begin
  238. Message(asmr_e_expr_zero_divide);
  239. { push 1 for error recovery }
  240. RPNPush(1);
  241. end;
  242. end;
  243. '^' : RPNPush(RPNPop XOR RPNPop);
  244. '/' :
  245. begin
  246. Temp:=RPNPop;
  247. if Temp <> 0 then
  248. RPNPush(RPNPop div Temp)
  249. else
  250. begin
  251. Message(asmr_e_expr_zero_divide);
  252. { push 1 for error recovery }
  253. RPNPush(1);
  254. end;
  255. end;
  256. end
  257. else
  258. begin
  259. { Convert String to number and add to stack }
  260. if token='-2147483648' then
  261. begin
  262. temp:=$80000000;
  263. localerror:=0;
  264. end
  265. else
  266. Val(Token, Temp, LocalError);
  267. if LocalError = 0 then
  268. RPNPush(Temp)
  269. else
  270. begin
  271. Message(asmr_e_expr_illegal);
  272. { push 1 for error recovery }
  273. RPNPush(1);
  274. end;
  275. end;
  276. end;
  277. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  278. { Add an operator onto top of the stack }
  279. begin
  280. if OpTop < OpMax then
  281. begin
  282. Inc(OpTop);
  283. OpStack[OpTop].ch:=_Operator;
  284. OpStack[OpTop].is_prefix:=prefix;
  285. end
  286. else
  287. Message(asmr_e_expr_illegal);
  288. end;
  289. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  290. { Get operator at the top of the stack }
  291. begin
  292. if OpTop > 0 then
  293. begin
  294. _Operator:=OpStack[OpTop];
  295. Dec(OpTop);
  296. end
  297. else
  298. Message(asmr_e_expr_illegal);
  299. end;
  300. Function TExprParse.Priority(_Operator : Char) : Integer;
  301. { Return priority of operator }
  302. { The greater the priority, the higher the precedence }
  303. begin
  304. Case _Operator OF
  305. '(' :
  306. Priority:=0;
  307. '+', '-' :
  308. Priority:=1;
  309. '*', '/','%','<','>' :
  310. Priority:=2;
  311. '|','&','^','~' :
  312. Priority:=0;
  313. else
  314. Message(asmr_e_expr_illegal);
  315. end;
  316. end;
  317. Function TExprParse.Evaluate(Expr : String):longint;
  318. Var
  319. I : Integer;
  320. Token : String15;
  321. opr : TExprOperator;
  322. begin
  323. Evaluate:=0;
  324. { Reset stacks }
  325. OpTop :=0;
  326. RPNTop:=0;
  327. Token :='';
  328. { nothing to do ? }
  329. if Expr='' then
  330. exit;
  331. For I:=1 to Length(Expr) DO
  332. begin
  333. if Expr[I] in ['0'..'9'] then
  334. begin { Build multi-digit numbers }
  335. Token:=Token + Expr[I];
  336. if I = Length(Expr) then { Send last one to calculator }
  337. RPNCalc(Token,false);
  338. end
  339. else
  340. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  341. begin
  342. if Token <> '' then
  343. begin { Send last built number to calc. }
  344. RPNCalc(Token,false);
  345. Token:='';
  346. end;
  347. Case Expr[I] OF
  348. '(' : OpPush('(',false);
  349. ')' : begin
  350. While OpStack[OpTop].ch <> '(' DO
  351. Begin
  352. OpPop(opr);
  353. RPNCalc(opr.ch,opr.is_prefix);
  354. end;
  355. OpPop(opr); { Pop off and ignore the '(' }
  356. end;
  357. '+','-','~' : Begin
  358. { workaround for -2147483648 }
  359. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  360. begin
  361. token:='-';
  362. expr[i]:='+';
  363. end;
  364. { if start of expression then surely a prefix }
  365. { or if previous char was also an operator }
  366. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  367. OpPush(Expr[I],true)
  368. else
  369. Begin
  370. { Evaluate all higher priority operators }
  371. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  372. Begin
  373. OpPop(opr);
  374. RPNCalc(opr.ch,opr.is_prefix);
  375. end;
  376. OpPush(Expr[I],false);
  377. End;
  378. end;
  379. '*', '/',
  380. '^','|','&',
  381. '%','<','>' : begin
  382. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  383. Begin
  384. OpPop(opr);
  385. RPNCalc(opr.ch,opr.is_prefix);
  386. end;
  387. OpPush(Expr[I],false);
  388. end;
  389. end; { Case }
  390. end
  391. else
  392. Message(asmr_e_expr_illegal); { Handle bad input error }
  393. end;
  394. { Pop off the remaining operators }
  395. While OpTop > 0 do
  396. Begin
  397. OpPop(opr);
  398. RPNCalc(opr.ch,opr.is_prefix);
  399. end;
  400. { The result is stored on the top of the stack }
  401. Evaluate:=RPNPop;
  402. end;
  403. Destructor TExprParse.Done;
  404. Begin
  405. end;
  406. Function CalculateExpression(const expression: string): longint;
  407. var
  408. expr: TExprParse;
  409. Begin
  410. expr.Init;
  411. CalculateExpression:=expr.Evaluate(expression);
  412. expr.Done;
  413. end;
  414. {*************************************************************************}
  415. { String conversions/utils }
  416. {*************************************************************************}
  417. Function EscapeToPascal(const s:string): string;
  418. { converts a C styled string - which contains escape }
  419. { characters to a pascal style string. }
  420. var
  421. i,len : longint;
  422. hs : string;
  423. temp : string;
  424. c : char;
  425. Begin
  426. hs:='';
  427. len:=0;
  428. i:=0;
  429. while (i<length(s)) and (len<255) do
  430. begin
  431. Inc(i);
  432. if (s[i]='\') and (i<length(s)) then
  433. Begin
  434. inc(i);
  435. case s[i] of
  436. '\' :
  437. c:='\';
  438. 'b':
  439. c:=#8;
  440. 'f':
  441. c:=#12;
  442. 'n':
  443. c:=#10;
  444. 'r':
  445. c:=#13;
  446. 't':
  447. c:=#9;
  448. '"':
  449. c:='"';
  450. '0'..'7':
  451. Begin
  452. temp:=s[i];
  453. temp:=temp+s[i+1];
  454. temp:=temp+s[i+2];
  455. inc(i,2);
  456. c:=chr(ValOctal(temp));
  457. end;
  458. 'x':
  459. Begin
  460. temp:=s[i+1];
  461. temp:=temp+s[i+2];
  462. inc(i,2);
  463. c:=chr(ValHexaDecimal(temp));
  464. end;
  465. else
  466. Begin
  467. Message1(asmr_e_escape_seq_ignored,s[i]);
  468. c:=s[i];
  469. end;
  470. end;
  471. end
  472. else
  473. c:=s[i];
  474. inc(len);
  475. hs[len]:=c;
  476. end;
  477. hs[0]:=chr(len);
  478. EscapeToPascal:=hs;
  479. end;
  480. Function ValDecimal(const S:String):longint;
  481. { Converts a decimal string to longint }
  482. var
  483. vs,c : longint;
  484. Begin
  485. vs:=0;
  486. for c:=1 to length(s) do
  487. begin
  488. vs:=vs*10;
  489. if s[c] in ['0'..'9'] then
  490. inc(vs,ord(s[c])-ord('0'))
  491. else
  492. begin
  493. Message1(asmr_e_error_converting_decimal,s);
  494. ValDecimal:=0;
  495. exit;
  496. end;
  497. end;
  498. ValDecimal:=vs;
  499. end;
  500. Function ValOctal(const S:String):longint;
  501. { Converts an octal string to longint }
  502. var
  503. vs,c : longint;
  504. Begin
  505. vs:=0;
  506. for c:=1 to length(s) do
  507. begin
  508. vs:=vs shl 3;
  509. if s[c] in ['0'..'7'] then
  510. inc(vs,ord(s[c])-ord('0'))
  511. else
  512. begin
  513. Message1(asmr_e_error_converting_octal,s);
  514. ValOctal:=0;
  515. exit;
  516. end;
  517. end;
  518. ValOctal:=vs;
  519. end;
  520. Function ValBinary(const S:String):longint;
  521. { Converts a binary string to longint }
  522. var
  523. vs,c : longint;
  524. Begin
  525. vs:=0;
  526. for c:=1 to length(s) do
  527. begin
  528. vs:=vs shl 1;
  529. if s[c] in ['0'..'1'] then
  530. inc(vs,ord(s[c])-ord('0'))
  531. else
  532. begin
  533. Message1(asmr_e_error_converting_binary,s);
  534. ValBinary:=0;
  535. exit;
  536. end;
  537. end;
  538. ValBinary:=vs;
  539. end;
  540. Function ValHexadecimal(const S:String):longint;
  541. { Converts a binary string to longint }
  542. var
  543. vs,c : longint;
  544. Begin
  545. vs:=0;
  546. for c:=1 to length(s) do
  547. begin
  548. vs:=vs shl 4;
  549. case s[c] of
  550. '0'..'9' :
  551. inc(vs,ord(s[c])-ord('0'));
  552. 'A'..'F' :
  553. inc(vs,ord(s[c])-ord('A')+10);
  554. 'a'..'f' :
  555. inc(vs,ord(s[c])-ord('a')+10);
  556. else
  557. begin
  558. Message1(asmr_e_error_converting_hexadecimal,s);
  559. ValHexadecimal:=0;
  560. exit;
  561. end;
  562. end;
  563. end;
  564. ValHexadecimal:=vs;
  565. end;
  566. Function PadZero(Var s: String; n: byte): Boolean;
  567. Begin
  568. PadZero:=TRUE;
  569. { Do some error checking first }
  570. if Length(s) = n then
  571. exit
  572. else
  573. if Length(s) > n then
  574. Begin
  575. PadZero:=FALSE;
  576. delete(s,n+1,length(s));
  577. exit;
  578. end
  579. else
  580. PadZero:=TRUE;
  581. { Fill it up with the specified character }
  582. fillchar(s[length(s)+1],n-1,#0);
  583. s[0]:=chr(n);
  584. end;
  585. {****************************************************************************
  586. TOperand
  587. ****************************************************************************}
  588. constructor TOperand.init;
  589. begin
  590. size:=S_NO;
  591. hastype:=false;
  592. hasvar:=false;
  593. FillChar(Opr,sizeof(Opr),0);
  594. end;
  595. destructor TOperand.done;
  596. begin
  597. end;
  598. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  599. begin
  600. end;
  601. Procedure TOperand.SetSize(_size:longint;force:boolean);
  602. begin
  603. if force or
  604. ((size = S_NO) and (_size<=extended_size)) then
  605. Begin
  606. case _size of
  607. 1 : size:=S_B;
  608. 2 : size:=S_W{ could be S_IS};
  609. 4 : size:=S_L{ could be S_IL or S_FS};
  610. 8 : size:=S_IQ{ could be S_D or S_FL};
  611. extended_size : size:=S_FX;
  612. end;
  613. end;
  614. end;
  615. Function TOperand.SetupResult:boolean;
  616. Begin
  617. SetupResult:=false;
  618. { replace by correct offset. }
  619. if assigned(procinfo^.returntype.def) and
  620. (procinfo^.returntype.def<>pdef(voiddef)) then
  621. begin
  622. opr.ref.offset:=procinfo^.return_offset;
  623. opr.ref.base:= procinfo^.framepointer;
  624. { always assume that the result is valid. }
  625. procinfo^.funcret_state:=vs_assigned;
  626. SetupResult:=true;
  627. end
  628. else
  629. Message(asmr_e_void_function);
  630. end;
  631. Function TOperand.SetupSelf:boolean;
  632. Begin
  633. SetupSelf:=false;
  634. if assigned(procinfo^._class) then
  635. Begin
  636. opr.typ:=OPR_REFERENCE;
  637. opr.ref.offset:=procinfo^.selfpointer_offset;
  638. opr.ref.base:=procinfo^.framepointer;
  639. opr.ref.options:=ref_selffixup;
  640. SetupSelf:=true;
  641. end
  642. else
  643. Message(asmr_e_cannot_use_SELF_outside_a_method);
  644. end;
  645. Function TOperand.SetupOldEBP:boolean;
  646. Begin
  647. SetupOldEBP:=false;
  648. if lexlevel>normal_function_level then
  649. Begin
  650. opr.typ:=OPR_REFERENCE;
  651. opr.ref.offset:=procinfo^.framepointer_offset;
  652. opr.ref.base:=procinfo^.framepointer;
  653. SetupOldEBP:=true;
  654. end
  655. else
  656. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  657. end;
  658. Function TOperand.SetupVar(const hs:string;GetOffset : boolean): Boolean;
  659. { search and sets up the correct fields in the Instr record }
  660. { for the NON-constant identifier passed to the routine. }
  661. { if not found returns FALSE. }
  662. var
  663. sym : psym;
  664. harrdef : parraydef;
  665. Begin
  666. SetupVar:=false;
  667. { are we in a routine ? }
  668. getsym(hs,false);
  669. sym:=srsym;
  670. if sym=nil then
  671. exit;
  672. case sym^.typ of
  673. varsym :
  674. begin
  675. { we always assume in asm statements that }
  676. { that the variable is valid. }
  677. pvarsym(sym)^.varstate:=vs_used;
  678. inc(pvarsym(sym)^.refs);
  679. case pvarsym(sym)^.owner^.symtabletype of
  680. objectsymtable :
  681. begin
  682. { this is not allowed, because we don't know if the self
  683. register is still free, and loading it first is also
  684. not possible, because this could break code }
  685. { Be TP/Delphi compatible in Delphi or TP modes }
  686. if (m_tp in aktmodeswitches) then
  687. begin
  688. opr.typ:=OPR_CONSTANT;
  689. opr.val:=pvarsym(sym)^.address;
  690. end
  691. { I do not agree here people using method vars should ensure
  692. that %esi is valid there }
  693. else
  694. begin
  695. opr.ref.base:=self_pointer;
  696. opr.ref.offset:=pvarsym(sym)^.address;
  697. end;
  698. hasvar:=true;
  699. SetupVar:=true;
  700. Exit;
  701. end;
  702. unitsymtable,
  703. globalsymtable,
  704. staticsymtable :
  705. opr.ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname);
  706. parasymtable :
  707. begin
  708. { if we only want the offset we don't have to care
  709. the base will be zeroed after ! }
  710. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) or
  711. { this below is wrong because there are two parast
  712. for global functions one of interface the second of
  713. implementation
  714. if (pvarsym(sym)^.owner=procinfo^.def^.parast) or }
  715. GetOffset then
  716. begin
  717. opr.ref.base:=procinfo^.framepointer;
  718. end
  719. else
  720. begin
  721. if (procinfo^.framepointer=R_ESP) and
  722. assigned(procinfo^.parent) and
  723. (lexlevel=pvarsym(sym)^.owner^.symtablelevel+1) and
  724. { same problem as above !!
  725. (procinfo^.parent^.sym^.definition^.parast=pvarsym(sym)^.owner) and }
  726. (lexlevel>normal_function_level) then
  727. opr.ref.base:=procinfo^.parent^.framepointer
  728. else
  729. message1(asmr_e_local_para_unreachable,hs);
  730. end;
  731. opr.ref.offset:=pvarsym(sym)^.address;
  732. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) then
  733. begin
  734. opr.ref.offsetfixup:=aktprocsym^.definition^.parast^.address_fixup;
  735. opr.ref.options:=ref_parafixup;
  736. end
  737. else
  738. begin
  739. opr.ref.offsetfixup:=0;
  740. opr.ref.options:=ref_none;
  741. end;
  742. if (pvarsym(sym)^.varspez=vs_var) or
  743. ((pvarsym(sym)^.varspez=vs_const) and
  744. push_addr_param(pvarsym(sym)^.vartype.def)) then
  745. SetSize(target_os.size_of_pointer,false);
  746. end;
  747. localsymtable :
  748. begin
  749. if (vo_is_external in pvarsym(sym)^.varoptions) then
  750. opr.ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname)
  751. else
  752. begin
  753. { if we only want the offset we don't have to care
  754. the base will be zeroed after ! }
  755. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) or
  756. {if (pvarsym(sym)^.owner=procinfo^.def^.localst) or}
  757. GetOffset then
  758. opr.ref.base:=procinfo^.framepointer
  759. else
  760. begin
  761. if (procinfo^.framepointer=R_ESP) and
  762. assigned(procinfo^.parent) and
  763. (lexlevel=pvarsym(sym)^.owner^.symtablelevel+1) and
  764. {(procinfo^.parent^.sym^.definition^.localst=pvarsym(sym)^.owner) and}
  765. (lexlevel>normal_function_level) then
  766. opr.ref.base:=procinfo^.parent^.framepointer
  767. else
  768. message1(asmr_e_local_para_unreachable,hs);
  769. end;
  770. opr.ref.offset:=-(pvarsym(sym)^.address);
  771. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) then
  772. begin
  773. opr.ref.offsetfixup:=aktprocsym^.definition^.localst^.address_fixup;
  774. opr.ref.options:=ref_localfixup;
  775. end
  776. else
  777. begin
  778. opr.ref.offsetfixup:=0;
  779. opr.ref.options:=ref_none;
  780. end;
  781. end;
  782. end;
  783. end;
  784. case pvarsym(sym)^.vartype.def^.deftype of
  785. orddef,
  786. enumdef,
  787. pointerdef,
  788. floatdef :
  789. SetSize(pvarsym(sym)^.getsize,false);
  790. arraydef :
  791. begin
  792. { for arrays try to get the element size, take care of
  793. multiple indexes }
  794. harrdef:=Parraydef(PVarsym(sym)^.vartype.def);
  795. while assigned(harrdef^.elementtype.def) and
  796. (harrdef^.elementtype.def^.deftype=arraydef) do
  797. harrdef:=parraydef(harrdef^.elementtype.def);
  798. SetSize(harrdef^.elesize,false);
  799. end;
  800. end;
  801. hasvar:=true;
  802. SetupVar:=true;
  803. Exit;
  804. end;
  805. typedconstsym :
  806. begin
  807. opr.ref.symbol:=newasmsymbol(ptypedconstsym(sym)^.mangledname);
  808. case ptypedconstsym(sym)^.typedconsttype.def^.deftype of
  809. orddef,
  810. enumdef,
  811. pointerdef,
  812. floatdef :
  813. SetSize(ptypedconstsym(sym)^.getsize,false);
  814. arraydef :
  815. begin
  816. { for arrays try to get the element size, take care of
  817. multiple indexes }
  818. harrdef:=Parraydef(PTypedConstSym(sym)^.typedconsttype.def);
  819. while assigned(harrdef^.elementtype.def) and
  820. (harrdef^.elementtype.def^.deftype=arraydef) do
  821. harrdef:=parraydef(harrdef^.elementtype.def);
  822. SetSize(harrdef^.elesize,false);
  823. end;
  824. end;
  825. hasvar:=true;
  826. SetupVar:=true;
  827. Exit;
  828. end;
  829. constsym :
  830. begin
  831. if pconstsym(sym)^.consttyp in [constint,constchar,constbool] then
  832. begin
  833. opr.typ:=OPR_CONSTANT;
  834. opr.val:=pconstsym(sym)^.value;
  835. SetupVar:=true;
  836. Exit;
  837. end;
  838. end;
  839. typesym :
  840. begin
  841. if ptypesym(sym)^.restype.def^.deftype in [recorddef,objectdef] then
  842. begin
  843. opr.typ:=OPR_CONSTANT;
  844. opr.val:=0;
  845. SetupVar:=TRUE;
  846. Exit;
  847. end;
  848. end;
  849. procsym :
  850. begin
  851. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  852. Message(asmr_w_calling_overload_func);
  853. opr.typ:=OPR_SYMBOL;
  854. opr.symbol:=newasmsymbol(pprocsym(sym)^.definition^.mangledname);
  855. hasvar:=true;
  856. SetupVar:=TRUE;
  857. Exit;
  858. end;
  859. else
  860. begin
  861. Message(asmr_e_unsupported_symbol_type);
  862. exit;
  863. end;
  864. end;
  865. end;
  866. { looks for internal names of variables and routines }
  867. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  868. {$ifndef OLDDIRECTVAR}
  869. var
  870. p : pasmsymbol;
  871. begin
  872. SetupDirectVar:=false;
  873. p:=getasmsymbol(hs);
  874. if assigned(p) then
  875. begin
  876. opr.ref.symbol:=p;
  877. hasvar:=true;
  878. SetupDirectVar:=true;
  879. end;
  880. end;
  881. {$else}
  882. var
  883. p : pai_external;
  884. Begin
  885. SearchDirectVar:=false;
  886. { search in the list of internals }
  887. p:=search_assembler_symbol(internals,hs,EXT_ANY);
  888. if p=nil then
  889. p:=search_assembler_symbol(externals,hs,EXT_ANY);
  890. if p<>nil then
  891. begin
  892. instr.operands[operandnum].opr.ref.symbol:=p^.sym;
  893. case p^.exttyp of
  894. EXT_BYTE : instr.operands[operandnum].size:=S_B;
  895. EXT_WORD : instr.operands[operandnum].size:=S_W;
  896. EXT_NEAR,EXT_FAR,EXT_PROC,EXT_DWORD,EXT_CODEPTR,EXT_DATAPTR:
  897. instr.operands[operandnum].size:=S_L;
  898. EXT_QWORD : instr.operands[operandnum].size:=S_FL;
  899. EXT_TBYTE : instr.operands[operandnum].size:=S_FX;
  900. else
  901. { this is in the case where the instruction is LEA }
  902. { or something like that, in that case size is not }
  903. { important. }
  904. instr.operands[operandnum].size:=S_NO;
  905. end;
  906. instr.operands[operandnum].hasvar:=true;
  907. SearchDirectVar:=TRUE;
  908. Exit;
  909. end;
  910. end;
  911. {$endif}
  912. procedure TOperand.InitRef;
  913. {*********************************************************************}
  914. { Description: This routine first check if the opcode is of }
  915. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  916. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  917. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  918. { to point to the default segment. }
  919. {*********************************************************************}
  920. Begin
  921. case opr.typ of
  922. OPR_REFERENCE:
  923. exit;
  924. OPR_NONE: ;
  925. else
  926. Message(asmr_e_invalid_operand_type);
  927. end;
  928. opr.typ := OPR_REFERENCE;
  929. reset_reference(opr.ref);
  930. end;
  931. procedure TOperand.BuildOperand;
  932. begin
  933. abstract;
  934. end;
  935. {****************************************************************************
  936. TInstruction
  937. ****************************************************************************}
  938. constructor TInstruction.init;
  939. Begin
  940. Opcode:=A_NONE;
  941. Opsize:=S_NO;
  942. Condition:=C_NONE;
  943. Ops:=0;
  944. InitOperands;
  945. end;
  946. destructor TInstruction.done;
  947. var
  948. i : longint;
  949. Begin
  950. for i:=1 to 3 do
  951. Dispose(Operands[i],Done);
  952. end;
  953. procedure TInstruction.InitOperands;
  954. var
  955. i : longint;
  956. begin
  957. for i:=1 to 3 do
  958. New(Operands[i],init);
  959. end;
  960. Procedure TInstruction.SwapOperands;
  961. Var
  962. p : POperand;
  963. Begin
  964. case Ops of
  965. 2 :
  966. begin
  967. p:=Operands[1];
  968. Operands[1]:=Operands[2];
  969. Operands[2]:=p;
  970. end;
  971. 3 :
  972. begin
  973. p:=Operands[1];
  974. Operands[1]:=Operands[3];
  975. Operands[3]:=p;
  976. end;
  977. end;
  978. end;
  979. procedure TInstruction.ConcatInstruction(p:PAasmOutput);
  980. begin
  981. abstract;
  982. end;
  983. procedure TInstruction.BuildOpcode;
  984. begin
  985. abstract;
  986. end;
  987. {***************************************************************************
  988. TLocalLabel
  989. ***************************************************************************}
  990. constructor TLocalLabel.Init(const n:string);
  991. begin
  992. inherited InitName(n);
  993. lab:=nil;
  994. emitted:=false;
  995. end;
  996. function TLocalLabel.Getpasmlabel:pasmlabel;
  997. begin
  998. if not assigned(lab) then
  999. begin
  1000. getlabel(lab);
  1001. { this label is forced to be used so it's always written }
  1002. inc(lab^.refs);
  1003. end;
  1004. Getpasmlabel:=lab;
  1005. end;
  1006. {***************************************************************************
  1007. TLocalLabelList
  1008. ***************************************************************************}
  1009. procedure LocalLabelEmitted(p:PNamedIndexObject);{$ifndef FPC}far;{$endif}
  1010. begin
  1011. if not PLocalLabel(p)^.emitted then
  1012. Message1(asmr_e_unknown_label_identifier,p^.name);
  1013. end;
  1014. procedure TLocalLabelList.CheckEmitted;
  1015. begin
  1016. ForEach({$ifndef TP}@{$endif}LocalLabelEmitted)
  1017. end;
  1018. function CreateLocalLabel(const s: string; var hl: pasmlabel; emit:boolean):boolean;
  1019. var
  1020. lab : PLocalLabel;
  1021. Begin
  1022. CreateLocalLabel:=true;
  1023. { Check if it already is defined }
  1024. lab:=PLocalLabel(LocalLabelList^.Search(s));
  1025. if not assigned(lab) then
  1026. begin
  1027. new(lab,init(s));
  1028. LocalLabelList^.Insert(lab);
  1029. end;
  1030. { set emitted flag and check for dup syms }
  1031. if emit then
  1032. begin
  1033. if lab^.Emitted then
  1034. begin
  1035. Message1(asmr_e_dup_local_sym,lab^.Name);
  1036. CreateLocalLabel:=false;
  1037. end;
  1038. lab^.Emitted:=true;
  1039. end;
  1040. hl:=lab^.Getpasmlabel;
  1041. end;
  1042. {****************************************************************************
  1043. Symbol table helper routines
  1044. ****************************************************************************}
  1045. Function SearchType(const hs:string): Boolean;
  1046. begin
  1047. getsym(hs,false);
  1048. SearchType:=assigned(srsym) and
  1049. (srsym^.typ=typesym);
  1050. end;
  1051. Function SearchRecordType(const s:string): boolean;
  1052. Begin
  1053. SearchRecordType:=false;
  1054. { Check the constants in symtable }
  1055. getsym(s,false);
  1056. if srsym <> nil then
  1057. Begin
  1058. case srsym^.typ of
  1059. typesym :
  1060. begin
  1061. if ptypesym(srsym)^.restype.def^.deftype in [recorddef,objectdef] then
  1062. begin
  1063. SearchRecordType:=true;
  1064. exit;
  1065. end;
  1066. end;
  1067. end;
  1068. end;
  1069. end;
  1070. Function SearchIConstant(const s:string; var l:longint): boolean;
  1071. {**********************************************************************}
  1072. { Description: Searches for a CONSTANT of name s in either the local }
  1073. { symbol list, then in the global symbol list, and returns the value }
  1074. { of that constant in l. Returns TRUE if successfull, if not found, }
  1075. { or if the constant is not of correct type, then returns FALSE }
  1076. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1077. { respectively. }
  1078. {**********************************************************************}
  1079. Begin
  1080. SearchIConstant:=false;
  1081. { check for TRUE or FALSE reserved words first }
  1082. if s = 'TRUE' then
  1083. Begin
  1084. SearchIConstant:=TRUE;
  1085. l:=1;
  1086. exit;
  1087. end;
  1088. if s = 'FALSE' then
  1089. Begin
  1090. SearchIConstant:=TRUE;
  1091. l:=0;
  1092. exit;
  1093. end;
  1094. { Check the constants in symtable }
  1095. getsym(s,false);
  1096. if srsym <> nil then
  1097. Begin
  1098. case srsym^.typ of
  1099. constsym :
  1100. begin
  1101. if (pconstsym(srsym)^.consttyp in [constord,constint,constchar,constbool]) then
  1102. Begin
  1103. l:=pconstsym(srsym)^.value;
  1104. SearchIConstant:=TRUE;
  1105. exit;
  1106. end;
  1107. end;
  1108. enumsym:
  1109. Begin
  1110. l:=penumsym(srsym)^.value;
  1111. SearchIConstant:=TRUE;
  1112. exit;
  1113. end;
  1114. end;
  1115. end;
  1116. end;
  1117. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1118. { search and returns the offset and size of records/objects of the base }
  1119. { with field name setup in field. }
  1120. { returns FALSE if not found. }
  1121. { used when base is a variable or a typed constant name. }
  1122. var
  1123. st : psymtable;
  1124. harrdef : parraydef;
  1125. sym : psym;
  1126. i : longint;
  1127. base : string;
  1128. Begin
  1129. GetRecordOffsetSize:=FALSE;
  1130. Offset:=0;
  1131. Size:=0;
  1132. i:=pos('.',s);
  1133. if i=0 then
  1134. i:=255;
  1135. base:=Copy(s,1,i-1);
  1136. delete(s,1,i);
  1137. if base='SELF' then
  1138. st:=procinfo^._class^.symtable
  1139. else
  1140. begin
  1141. getsym(base,false);
  1142. sym:=srsym;
  1143. st:=nil;
  1144. { we can start with a var,type,typedconst }
  1145. case sym^.typ of
  1146. varsym :
  1147. begin
  1148. case pvarsym(sym)^.vartype.def^.deftype of
  1149. recorddef :
  1150. st:=precorddef(pvarsym(sym)^.vartype.def)^.symtable;
  1151. objectdef :
  1152. st:=pobjectdef(pvarsym(sym)^.vartype.def)^.symtable;
  1153. end;
  1154. end;
  1155. typesym :
  1156. begin
  1157. case ptypesym(sym)^.restype.def^.deftype of
  1158. recorddef :
  1159. st:=precorddef(ptypesym(sym)^.restype.def)^.symtable;
  1160. objectdef :
  1161. st:=pobjectdef(ptypesym(sym)^.restype.def)^.symtable;
  1162. end;
  1163. end;
  1164. typedconstsym :
  1165. begin
  1166. case ptypedconstsym(sym)^.typedconsttype.def^.deftype of
  1167. recorddef :
  1168. st:=precorddef(ptypedconstsym(sym)^.typedconsttype.def)^.symtable;
  1169. objectdef :
  1170. st:=pobjectdef(ptypedconstsym(sym)^.typedconsttype.def)^.symtable;
  1171. end;
  1172. end;
  1173. end;
  1174. end;
  1175. { now walk all recordsymtables }
  1176. while assigned(st) and (s<>'') do
  1177. begin
  1178. { load next field in base }
  1179. i:=pos('.',s);
  1180. if i=0 then
  1181. i:=255;
  1182. base:=Copy(s,1,i-1);
  1183. delete(s,1,i);
  1184. if st^.symtabletype=objectsymtable then
  1185. sym:=search_class_member(pobjectdef(st^.defowner),base)
  1186. else
  1187. sym:=st^.search(base);
  1188. if not assigned(sym) then
  1189. begin
  1190. GetRecordOffsetSize:=false;
  1191. exit;
  1192. end;
  1193. st:=nil;
  1194. case sym^.typ of
  1195. varsym :
  1196. begin
  1197. inc(Offset,pvarsym(sym)^.address);
  1198. Size:=PVarsym(sym)^.getsize;
  1199. case pvarsym(sym)^.vartype.def^.deftype of
  1200. arraydef :
  1201. begin
  1202. { for arrays try to get the element size, take care of
  1203. multiple indexes }
  1204. harrdef:=Parraydef(PVarsym(sym)^.vartype.def);
  1205. while assigned(harrdef^.elementtype.def) and
  1206. (harrdef^.elementtype.def^.deftype=arraydef) do
  1207. harrdef:=parraydef(harrdef^.elementtype.def);
  1208. size:=harrdef^.elesize;
  1209. end;
  1210. recorddef :
  1211. st:=precorddef(pvarsym(sym)^.vartype.def)^.symtable;
  1212. objectdef :
  1213. st:=pobjectdef(pvarsym(sym)^.vartype.def)^.symtable;
  1214. end;
  1215. end;
  1216. end;
  1217. end;
  1218. GetRecordOffsetSize:=(s='');
  1219. end;
  1220. Function SearchLabel(const s: string; var hl: pasmlabel;emit:boolean): boolean;
  1221. var
  1222. sym : psym;
  1223. hs : string;
  1224. Begin
  1225. hl:=nil;
  1226. SearchLabel:=false;
  1227. { Check for pascal labels, which are case insensetive }
  1228. hs:=upper(s);
  1229. getsym(hs,false);
  1230. sym:=srsym;
  1231. if sym=nil then
  1232. exit;
  1233. case sym^.typ of
  1234. labelsym :
  1235. begin
  1236. hl:=plabelsym(sym)^.lab;
  1237. if emit then
  1238. plabelsym(sym)^.defined:=true
  1239. else
  1240. plabelsym(sym)^.used:=true;
  1241. SearchLabel:=true;
  1242. exit;
  1243. end;
  1244. end;
  1245. end;
  1246. {*************************************************************************}
  1247. { Instruction Generation Utilities }
  1248. {*************************************************************************}
  1249. Procedure ConcatString(p : paasmoutput;s:string);
  1250. {*********************************************************************}
  1251. { PROCEDURE ConcatString(s:string); }
  1252. { Description: This routine adds the character chain pointed to in }
  1253. { s to the instruction linked list. }
  1254. {*********************************************************************}
  1255. Var
  1256. pc: PChar;
  1257. Begin
  1258. getmem(pc,length(s)+1);
  1259. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1260. end;
  1261. Procedure ConcatPasString(p : paasmoutput;s:string);
  1262. {*********************************************************************}
  1263. { PROCEDURE ConcatPasString(s:string); }
  1264. { Description: This routine adds the character chain pointed to in }
  1265. { s to the instruction linked list, contrary to ConcatString it }
  1266. { uses a pascal style string, so it conserves null characters. }
  1267. {*********************************************************************}
  1268. Begin
  1269. p^.concat(new(pai_string,init(s)));
  1270. end;
  1271. Procedure ConcatDirect(p : paasmoutput;s:string);
  1272. {*********************************************************************}
  1273. { PROCEDURE ConcatDirect(s:string) }
  1274. { Description: This routine output the string directly to the asm }
  1275. { output, it is only sed when writing special labels in AT&T mode, }
  1276. { and should not be used without due consideration, since it may }
  1277. { cause problems. }
  1278. {*********************************************************************}
  1279. Var
  1280. pc: PChar;
  1281. Begin
  1282. getmem(pc,length(s)+1);
  1283. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1284. end;
  1285. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1286. {*********************************************************************}
  1287. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1288. { Description: This routine adds the value constant to the current }
  1289. { instruction linked list. }
  1290. { maxvalue -> indicates the size of the data to initialize: }
  1291. { $ff -> create a byte node. }
  1292. { $ffff -> create a word node. }
  1293. { $ffffffff -> create a dword node. }
  1294. {*********************************************************************}
  1295. Begin
  1296. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1297. Begin
  1298. Message(asmr_e_constant_out_of_bounds);
  1299. { assuming a value of maxvalue }
  1300. value:=maxvalue;
  1301. end;
  1302. if maxvalue = $ff then
  1303. p^.concat(new(pai_const,init_8bit(byte(value))))
  1304. else
  1305. if maxvalue = $ffff then
  1306. p^.concat(new(pai_const,init_16bit(word(value))))
  1307. else
  1308. if maxvalue = longint($ffffffff) then
  1309. p^.concat(new(pai_const,init_32bit(longint(value))));
  1310. end;
  1311. Procedure ConcatConstSymbol(p : paasmoutput;const sym:string;l:longint);
  1312. begin
  1313. p^.concat(new(pai_const_symbol,initname_offset(sym,l)));
  1314. end;
  1315. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1316. {***********************************************************************}
  1317. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1318. { Description: This routine adds the value constant to the current }
  1319. { instruction linked list. }
  1320. { real_typ -> indicates the type of the real data to initialize: }
  1321. { s32real -> create a single node. }
  1322. { s64real -> create a double node. }
  1323. { s80real -> create an extended node. }
  1324. { s64bit -> create a comp node. }
  1325. { f32bit -> create a fixed node. (not used normally) }
  1326. {***********************************************************************}
  1327. Begin
  1328. case real_typ of
  1329. s32real : p^.concat(new(pai_real_32bit,init(value)));
  1330. s64real : p^.concat(new(pai_real_64bit,init(value)));
  1331. s80real : p^.concat(new(pai_real_80bit,init(value)));
  1332. s64comp : p^.concat(new(pai_comp_64bit,init(value)));
  1333. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1334. end;
  1335. end;
  1336. Procedure ConcatLabel(p: paasmoutput;var l : pasmlabel);
  1337. {*********************************************************************}
  1338. { PROCEDURE ConcatLabel }
  1339. { Description: This routine either emits a label or a labeled }
  1340. { instruction to the linked list of instructions. }
  1341. {*********************************************************************}
  1342. begin
  1343. p^.concat(new(pai_label,init(l)));
  1344. end;
  1345. procedure ConcatAlign(p:paasmoutput;l:longint);
  1346. {*********************************************************************}
  1347. { PROCEDURE ConcatPublic }
  1348. { Description: This routine emits an global definition to the }
  1349. { linked list of instructions.(used by AT&T styled asm) }
  1350. {*********************************************************************}
  1351. begin
  1352. p^.concat(new(pai_align,init(l)));
  1353. end;
  1354. procedure ConcatPublic(p:paasmoutput;const s : string);
  1355. {*********************************************************************}
  1356. { PROCEDURE ConcatPublic }
  1357. { Description: This routine emits an global definition to the }
  1358. { linked list of instructions.(used by AT&T styled asm) }
  1359. {*********************************************************************}
  1360. begin
  1361. p^.concat(new(pai_symbol,initname_global(s,0)));
  1362. end;
  1363. procedure ConcatLocal(p:paasmoutput;const s : string);
  1364. {*********************************************************************}
  1365. { PROCEDURE ConcatLocal }
  1366. { Description: This routine emits an local definition to the }
  1367. { linked list of instructions. }
  1368. {*********************************************************************}
  1369. begin
  1370. p^.concat(new(pai_symbol,initname(s,0)));
  1371. end;
  1372. Procedure ConcatGlobalBss(const s : string;size : longint);
  1373. {*********************************************************************}
  1374. { PROCEDURE ConcatGlobalBss }
  1375. { Description: This routine emits an global datablock to the }
  1376. { linked list of instructions. }
  1377. {*********************************************************************}
  1378. begin
  1379. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1380. end;
  1381. Procedure ConcatLocalBss(const s : string;size : longint);
  1382. {*********************************************************************}
  1383. { PROCEDURE ConcatLocalBss }
  1384. { Description: This routine emits a local datablcok to the }
  1385. { linked list of instructions. }
  1386. {*********************************************************************}
  1387. begin
  1388. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1389. end;
  1390. end.
  1391. {
  1392. $Log$
  1393. Revision 1.46 2000-05-26 18:23:11 peter
  1394. * fixed % parsing and added modulo support
  1395. * changed some evaulator errors to more generic illegal expresion
  1396. Revision 1.45 2000/05/23 20:36:28 peter
  1397. + typecasting support for variables, but be carefull as word,byte can't
  1398. be used because they are reserved assembler keywords
  1399. Revision 1.44 2000/05/22 12:47:52 pierre
  1400. fix wrong handling of var para for size bug 961
  1401. Revision 1.43 2000/05/18 17:05:16 peter
  1402. * fixed size of const parameters in asm readers
  1403. Revision 1.42 2000/05/11 09:56:22 pierre
  1404. * fixed several compare problems between longints and
  1405. const > $80000000 that are treated as int64 constanst
  1406. by Delphi reported by Kovacs Attila Zoltan
  1407. Revision 1.41 2000/05/08 13:23:05 peter
  1408. * fixed reference parsing
  1409. Revision 1.40 2000/04/06 07:56:04 pierre
  1410. * bug in TOperand.SetSize corrected
  1411. Revision 1.39 2000/04/04 13:48:45 pierre
  1412. + TOperand.SetCorrectSize virtual method added
  1413. to be able to change the suffix according to the instruction
  1414. (FIADD word ptr w need a s as ATT suffix
  1415. wheras FILD word ptr w need a w suffix :( )
  1416. Revision 1.38 2000/03/28 22:10:12 pierre
  1417. * Object fields are simple offsets in TP/Delphi mode
  1418. Revision 1.37 2000/03/16 15:10:25 pierre
  1419. * correct the fixups for inlined assembler code
  1420. Revision 1.36 2000/03/15 23:10:01 pierre
  1421. * fix for bug 848 (that still genrated wrong code)
  1422. + better testing for variables used in assembler
  1423. (gives an error if variable is not directly reachable !)
  1424. Revision 1.35 2000/02/09 13:23:03 peter
  1425. * log truncated
  1426. Revision 1.34 2000/01/07 01:14:37 peter
  1427. * updated copyright to 2000
  1428. Revision 1.33 1999/12/22 00:57:30 peter
  1429. * label are set to used so an error is given if used but not defined
  1430. Revision 1.32 1999/12/17 10:43:34 florian
  1431. * 761 fixed
  1432. Revision 1.31 1999/11/30 10:40:54 peter
  1433. + ttype, tsymlist
  1434. Revision 1.30 1999/11/17 17:05:04 pierre
  1435. * Notes/hints changes
  1436. Revision 1.29 1999/11/09 23:06:46 peter
  1437. * esi_offset -> selfpointer_offset to be newcg compatible
  1438. * hcogegen -> cgbase fixes for newcg
  1439. Revision 1.28 1999/11/06 14:34:26 peter
  1440. * truncated log to 20 revs
  1441. Revision 1.27 1999/09/27 23:44:58 peter
  1442. * procinfo is now a pointer
  1443. * support for result setting in sub procedure
  1444. Revision 1.26 1999/09/08 16:04:04 peter
  1445. * better support for object fields and more error checks for
  1446. field accesses which create buggy code
  1447. Revision 1.25 1999/09/04 20:29:11 florian
  1448. * bug 577 fixed
  1449. Revision 1.24 1999/08/27 14:37:50 peter
  1450. * fixed crash with typedconst array
  1451. Revision 1.23 1999/08/13 21:28:38 peter
  1452. * more reference types support
  1453. * arraydef size returns elementsize, also for multiple indexing array
  1454. Revision 1.22 1999/08/04 00:23:28 florian
  1455. * renamed i386asm and i386base to cpuasm and cpubase
  1456. Revision 1.21 1999/08/03 22:03:12 peter
  1457. * moved bitmask constants to sets
  1458. * some other type/const renamings
  1459. Revision 1.20 1999/07/29 20:54:06 peter
  1460. * write .size also
  1461. }