rautils.pas 47 KB

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