tcctest.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814
  1. /*
  2. * TCC auto test program
  3. */
  4. #include "config.h"
  5. #if GCC_MAJOR >= 3
  6. /* Unfortunately, gcc version < 3 does not handle that! */
  7. #define ALL_ISOC99
  8. /* only gcc 3 handles _Bool correctly */
  9. #define BOOL_ISOC99
  10. /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
  11. #define CORRECT_CR_HANDLING
  12. #endif
  13. #if defined(_WIN32)
  14. #define LONG_LONG_FORMAT "%lld"
  15. #define ULONG_LONG_FORMAT "%llu"
  16. #else
  17. #define LONG_LONG_FORMAT "%Ld"
  18. #define ULONG_LONG_FORMAT "%Lu"
  19. #endif
  20. // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
  21. #if defined(_WIN32) && defined(__GNUC__)
  22. #define LONG_DOUBLE double
  23. #define LONG_DOUBLE_LITERAL(x) x
  24. #else
  25. #define LONG_DOUBLE long double
  26. #define LONG_DOUBLE_LITERAL(x) x ## L
  27. #endif
  28. /* deprecated and no longer supported in gcc 3.3 */
  29. //#define ACCEPT_CR_IN_STRINGS
  30. /* __VA_ARGS__ and __func__ support */
  31. #define C99_MACROS
  32. /* test various include syntaxes */
  33. #define TCCLIB_INC <tcclib.h>
  34. #define TCCLIB_INC1 <tcclib
  35. #define TCCLIB_INC2 h>
  36. #define TCCLIB_INC3 "tcclib.h"
  37. #include TCCLIB_INC
  38. #include TCCLIB_INC1.TCCLIB_INC2
  39. #include TCCLIB_INC1.h>
  40. #include TCCLIB_INC3
  41. #include <tcclib.h>
  42. #include "tcclib.h"
  43. #include "tcctest.h"
  44. /* Test two more ways to include a file named like a pp-number */
  45. #define INC(name) <tests/name.h>
  46. #define funnyname 42test.h
  47. #define incdir tests/
  48. #define incname < incdir funnyname >
  49. #define __stringify(x) #x
  50. #define stringify(x) __stringify(x)
  51. #include INC(42test)
  52. #include incname
  53. #include stringify(funnyname)
  54. void intdiv_test();
  55. void string_test();
  56. void expr_test();
  57. void macro_test();
  58. void recursive_macro_test();
  59. void scope_test();
  60. void forward_test();
  61. void funcptr_test();
  62. void loop_test();
  63. void switch_test();
  64. void goto_test();
  65. void enum_test();
  66. void typedef_test();
  67. void struct_test();
  68. void array_test();
  69. void expr_ptr_test();
  70. void bool_test();
  71. void optimize_out();
  72. void expr2_test();
  73. void constant_expr_test();
  74. void expr_cmp_test();
  75. void char_short_test();
  76. void init_test(void);
  77. void compound_literal_test(void);
  78. int kr_test();
  79. void struct_assign_test(void);
  80. void cast_test(void);
  81. void bitfield_test(void);
  82. void c99_bool_test(void);
  83. void float_test(void);
  84. void longlong_test(void);
  85. void manyarg_test(void);
  86. void stdarg_test(void);
  87. void whitespace_test(void);
  88. void relocation_test(void);
  89. void old_style_function(void);
  90. void alloca_test(void);
  91. void c99_vla_test(int size1, int size2);
  92. void sizeof_test(void);
  93. void typeof_test(void);
  94. void local_label_test(void);
  95. void statement_expr_test(void);
  96. void asm_test(void);
  97. void builtin_test(void);
  98. void weak_test(void);
  99. void global_data_test(void);
  100. void cmp_comparison_test(void);
  101. void math_cmp_test(void);
  102. void callsave_test(void);
  103. void builtin_frame_address_test(void);
  104. void attrib_test(void);
  105. int fib(int n);
  106. void num(int n);
  107. void forward_ref(void);
  108. int isid(int c);
  109. /* Line joining happens before tokenization, so the following
  110. must be parsed as ellipsis. */
  111. void funny_line_continuation (int, ..\
  112. . );
  113. char via_volatile (char);
  114. #define A 2
  115. #define N 1234 + A
  116. #define pf printf
  117. #define M1(a, b) (a) + (b)
  118. #define str\
  119. (s) # s
  120. #define glue(a, b) a ## b
  121. #define xglue(a, b) glue(a, b)
  122. #define HIGHLOW "hello"
  123. #define LOW LOW ", world"
  124. static int onetwothree = 123;
  125. #define onetwothree4 onetwothree
  126. #define onetwothree xglue(onetwothree,4)
  127. #define min(a, b) ((a) < (b) ? (a) : (b))
  128. #ifdef C99_MACROS
  129. #define dprintf(level,...) printf(__VA_ARGS__)
  130. #endif
  131. /* gcc vararg macros */
  132. #define dprintf1(level, fmt, args...) printf(fmt, ## args)
  133. #define MACRO_NOARGS()
  134. #define AAA 3
  135. #undef AAA
  136. #define AAA 4
  137. #if 1
  138. #define B3 1
  139. #elif 1
  140. #define B3 2
  141. #elif 0
  142. #define B3 3
  143. #else
  144. #define B3 4
  145. #endif
  146. #ifdef __TINYC__
  147. /* We try to handle this syntax. Make at least sure it doesn't segfault. */
  148. char invalid_function_def()[] {}
  149. #endif
  150. #define __INT64_C(c) c ## LL
  151. #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
  152. int qq(int x)
  153. {
  154. return x + 40;
  155. }
  156. #define qq(x) x
  157. #define spin_lock(lock) do { } while (0)
  158. #define wq_spin_lock spin_lock
  159. #define TEST2() wq_spin_lock(a)
  160. #define UINT_MAX ((unsigned) -1)
  161. void intdiv_test(void)
  162. {
  163. printf("18/21=%u\n", 18/21);
  164. printf("18%%21=%u\n", 18%21);
  165. printf("41/21=%u\n", 41/21);
  166. printf("41%%21=%u\n", 41%21);
  167. printf("42/21=%u\n", 42/21);
  168. printf("42%%21=%u\n", 42%21);
  169. printf("43/21=%u\n", 43/21);
  170. printf("43%%21=%u\n", 43%21);
  171. printf("126/21=%u\n", 126/21);
  172. printf("126%%21=%u\n", 126%21);
  173. printf("131/21=%u\n", 131/21);
  174. printf("131%%21=%u\n", 131%21);
  175. printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
  176. printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2);
  177. printf("18/-21=%u\n", 18/-21);
  178. printf("18%%-21=%u\n", 18%-21);
  179. printf("41/-21=%u\n", 41/-21);
  180. printf("41%%-21=%u\n", 41%-21);
  181. printf("42/-21=%u\n", 42/-21);
  182. printf("42%%-21=%u\n", 42%-21);
  183. printf("43/-21=%u\n", 43/-21);
  184. printf("43%%-21=%u\n", 43%-21);
  185. printf("126/-21=%u\n", 126/-21);
  186. printf("126%%-21=%u\n", 126%-21);
  187. printf("131/-21=%u\n", 131/-21);
  188. printf("131%%-21=%u\n", 131%-21);
  189. printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
  190. printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2);
  191. printf("-18/21=%u\n", -18/21);
  192. printf("-18%%21=%u\n", -18%21);
  193. printf("-41/21=%u\n", -41/21);
  194. printf("-41%%21=%u\n", -41%21);
  195. printf("-42/21=%u\n", -42/21);
  196. printf("-42%%21=%u\n", -42%21);
  197. printf("-43/21=%u\n", -43/21);
  198. printf("-43%%21=%u\n", -43%21);
  199. printf("-126/21=%u\n", -126/21);
  200. printf("-126%%21=%u\n", -126%21);
  201. printf("-131/21=%u\n", -131/21);
  202. printf("-131%%21=%u\n", -131%21);
  203. printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
  204. printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2);
  205. printf("-18/-21=%u\n", -18/-21);
  206. printf("-18%%-21=%u\n", -18%-21);
  207. printf("-41/-21=%u\n", -41/-21);
  208. printf("-41%%-21=%u\n", -41%-21);
  209. printf("-42/-21=%u\n", -42/-21);
  210. printf("-42%%-21=%u\n", -42%-21);
  211. printf("-43/-21=%u\n", -43/-21);
  212. printf("-43%%-21=%u\n", -43%-21);
  213. printf("-126/-21=%u\n", -126/-21);
  214. printf("-126%%-21=%u\n", -126%-21);
  215. printf("-131/-21=%u\n", -131/-21);
  216. printf("-131%%-21=%u\n", -131%-21);
  217. printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
  218. printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
  219. }
  220. void macro_test(void)
  221. {
  222. printf("macro:\n");
  223. pf("N=%d\n", N);
  224. printf("aaa=%d\n", AAA);
  225. printf("min=%d\n", min(1, min(2, -1)));
  226. printf("s1=%s\n", glue(HIGH, LOW));
  227. printf("s2=%s\n", xglue(HIGH, LOW));
  228. printf("s3=%s\n", str("c"));
  229. printf("s4=%s\n", str(a1));
  230. printf("B3=%d\n", B3);
  231. printf("onetwothree=%d\n", onetwothree);
  232. #ifdef A
  233. printf("A defined\n");
  234. #endif
  235. #ifdef B
  236. printf("B defined\n");
  237. #endif
  238. #ifdef A
  239. printf("A defined\n");
  240. #else
  241. printf("A not defined\n");
  242. #endif
  243. #ifdef B
  244. printf("B defined\n");
  245. #else
  246. printf("B not defined\n");
  247. #endif
  248. #ifdef A
  249. printf("A defined\n");
  250. #ifdef B
  251. printf("B1 defined\n");
  252. #else
  253. printf("B1 not defined\n");
  254. #endif
  255. #else
  256. printf("A not defined\n");
  257. #ifdef B
  258. printf("B2 defined\n");
  259. #else
  260. printf("B2 not defined\n");
  261. #endif
  262. #endif
  263. #if 1+1
  264. printf("test true1\n");
  265. #endif
  266. #if 0
  267. printf("test true2\n");
  268. #endif
  269. #if 1-1
  270. printf("test true3\n");
  271. #endif
  272. #if defined(A)
  273. printf("test trueA\n");
  274. #endif
  275. #if defined(B)
  276. printf("test trueB\n");
  277. #endif
  278. #if 0
  279. printf("test 0\n");
  280. #elif 0
  281. printf("test 1\n");
  282. #elif 2
  283. printf("test 2\n");
  284. #else
  285. printf("test 3\n");
  286. #endif
  287. MACRO_NOARGS();
  288. #ifdef __LINE__
  289. printf("__LINE__ defined\n");
  290. #endif
  291. printf("__LINE__=%d __FILE__=%s\n",
  292. __LINE__, __FILE__);
  293. #if 0
  294. #line 200
  295. printf("__LINE__=%d __FILE__=%s\n",
  296. __LINE__, __FILE__);
  297. #line 203 "test"
  298. printf("__LINE__=%d __FILE__=%s\n",
  299. __LINE__, __FILE__);
  300. #line 227 "tcctest.c"
  301. #endif
  302. /* not strictly preprocessor, but we test it there */
  303. #ifdef C99_MACROS
  304. printf("__func__ = %s\n", __func__);
  305. dprintf(1, "vaarg=%d\n", 1);
  306. #endif
  307. dprintf1(1, "vaarg1\n");
  308. dprintf1(1, "vaarg1=%d\n", 2);
  309. dprintf1(1, "vaarg1=%d %d\n", 1, 2);
  310. /* gcc extension */
  311. printf("func='%s'\n", __FUNCTION__);
  312. /* complicated macros in glibc */
  313. printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
  314. {
  315. int a;
  316. a = 1;
  317. glue(a+, +);
  318. printf("a=%d\n", a);
  319. glue(a <, <= 2);
  320. printf("a=%d\n", a);
  321. }
  322. /* macro function with argument outside the macro string */
  323. #define MF_s MF_hello
  324. #define MF_hello(msg) printf("%s\n",msg)
  325. #define MF_t printf("tralala\n"); MF_hello
  326. MF_s("hi");
  327. MF_t("hi");
  328. /* test macro substitution inside args (should not eat stream) */
  329. printf("qq=%d\n", qq(qq)(2));
  330. /* test zero argument case. NOTE: gcc 2.95.x does not accept a
  331. null argument without a space. gcc 3.2 fixes that. */
  332. #define qq1(x) 1
  333. printf("qq1=%d\n", qq1( ));
  334. /* comment with stray handling *\
  335. /
  336. /* this is a valid *\/ comment */
  337. /* this is a valid comment *\*/
  338. // this is a valid\
  339. comment
  340. /* test function macro substitution when the function name is
  341. substituted */
  342. TEST2();
  343. /* And again when the name and parentheses are separated by a
  344. comment. */
  345. TEST2 /* the comment */ ();
  346. printf("%s\n", get_basefile_from_header());
  347. printf("%s\n", __BASE_FILE__);
  348. printf("%s\n", get_file_from_header());
  349. printf("%s\n", __FILE__);
  350. /* Check that funnily named include was in fact included */
  351. have_included_42test_h = 1;
  352. have_included_42test_h_second = 1;
  353. have_included_42test_h_third = 1;
  354. }
  355. static void print_num(char *fn, int line, int num) {
  356. printf("fn %s, line %d, num %d\n", fn, line, num);
  357. }
  358. void recursive_macro_test(void)
  359. {
  360. #define ELF32_ST_TYPE(val) ((val) & 0xf)
  361. #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
  362. #define STB_WEAK 2 /* Weak symbol */
  363. #define ELFW(type) ELF##32##_##type
  364. printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
  365. #define WRAP(x) x
  366. #define print_num(x) print_num(__FILE__,__LINE__,x)
  367. print_num(123);
  368. WRAP(print_num(123));
  369. WRAP(WRAP(print_num(123)));
  370. static struct recursive_macro { int rm_field; } G;
  371. #define rm_field (G.rm_field)
  372. printf("rm_field = %d\n", rm_field);
  373. printf("rm_field = %d\n", WRAP(rm_field));
  374. WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
  375. }
  376. int op(a,b)
  377. {
  378. return a / b;
  379. }
  380. int ret(a)
  381. {
  382. if (a == 2)
  383. return 1;
  384. if (a == 3)
  385. return 2;
  386. return 0;
  387. }
  388. void ps(const char *s)
  389. {
  390. int c;
  391. while (1) {
  392. c = *s;
  393. if (c == 0)
  394. break;
  395. printf("%c", c);
  396. s++;
  397. }
  398. }
  399. const char foo1_string[] = "\
  400. bar\n\
  401. test\14\
  402. 1";
  403. void string_test()
  404. {
  405. unsigned int b;
  406. printf("string:\n");
  407. printf("\141\1423\143\n");/* dezdez test */
  408. printf("\x41\x42\x43\x3a\n");
  409. printf("c=%c\n", 'r');
  410. printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
  411. printf("foo1_string='%s'\n", foo1_string);
  412. #if 0
  413. printf("wstring=%S\n", L"abc");
  414. printf("wstring=%S\n", L"abc" L"def" "ghi");
  415. printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
  416. printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
  417. #endif
  418. ps("test\n");
  419. b = 32;
  420. while ((b = b + 1) < 96) {
  421. printf("%c", b);
  422. }
  423. printf("\n");
  424. printf("fib=%d\n", fib(33));
  425. b = 262144;
  426. while (b != 0x80000000) {
  427. num(b);
  428. b = b * 2;
  429. }
  430. }
  431. void loop_test()
  432. {
  433. int i;
  434. i = 0;
  435. while (i < 10)
  436. printf("%d", i++);
  437. printf("\n");
  438. for(i = 0; i < 10;i++)
  439. printf("%d", i);
  440. printf("\n");
  441. i = 0;
  442. do {
  443. printf("%d", i++);
  444. } while (i < 10);
  445. printf("\n");
  446. char count = 123;
  447. /* c99 for loop init test */
  448. for (size_t count = 1; count < 3; count++)
  449. printf("count=%d\n", count);
  450. printf("count = %d\n", count);
  451. /* break/continue tests */
  452. i = 0;
  453. while (1) {
  454. if (i == 6)
  455. break;
  456. i++;
  457. if (i == 3)
  458. continue;
  459. printf("%d", i);
  460. }
  461. printf("\n");
  462. /* break/continue tests */
  463. i = 0;
  464. do {
  465. if (i == 6)
  466. break;
  467. i++;
  468. if (i == 3)
  469. continue;
  470. printf("%d", i);
  471. } while(1);
  472. printf("\n");
  473. for(i = 0;i < 10;i++) {
  474. if (i == 3)
  475. continue;
  476. printf("%d", i);
  477. }
  478. printf("\n");
  479. }
  480. typedef int typedef_and_label;
  481. void goto_test()
  482. {
  483. int i;
  484. static void *label_table[3] = { &&label1, &&label2, &&label3 };
  485. printf("goto:\n");
  486. i = 0;
  487. /* This needs to parse as label, not as start of decl. */
  488. typedef_and_label:
  489. s_loop:
  490. if (i >= 10)
  491. goto s_end;
  492. printf("%d", i);
  493. i++;
  494. goto s_loop;
  495. s_end:
  496. printf("\n");
  497. /* we also test computed gotos (GCC extension) */
  498. for(i=0;i<3;i++) {
  499. goto *label_table[i];
  500. label1:
  501. printf("label1\n");
  502. goto next;
  503. label2:
  504. printf("label2\n");
  505. goto next;
  506. label3:
  507. printf("label3\n");
  508. next: ;
  509. }
  510. }
  511. enum {
  512. E0,
  513. E1 = 2,
  514. E2 = 4,
  515. E3,
  516. E4,
  517. };
  518. enum test {
  519. E5 = 1000,
  520. };
  521. struct S_enum {
  522. enum {E6 = 42, E7, E8} e:8;
  523. };
  524. enum ELong {
  525. /* This is either 0 on L32 machines, or a large number
  526. on L64 machines. We should be able to store this. */
  527. EL_large = ((unsigned long)0xf000 << 31) << 1,
  528. };
  529. enum { BIASU = -1U<<31 };
  530. enum { BIASS = -1 << 31 };
  531. static int getint(int i)
  532. {
  533. if (i)
  534. return 0;
  535. else
  536. return (int)(-1U << 31);
  537. }
  538. void enum_test()
  539. {
  540. enum test b1;
  541. /* The following should give no warning */
  542. unsigned *p = &b1;
  543. struct S_enum s = {E7};
  544. printf("enum: %d\n", s.e);
  545. printf("enum:\n%d %d %d %d %d %d\n",
  546. E0, E1, E2, E3, E4, E5);
  547. b1 = 1;
  548. printf("b1=%d\n", b1);
  549. printf("enum large: %ld\n", EL_large);
  550. if (getint(0) == BIASU)
  551. printf("enum unsigned: ok\n");
  552. else
  553. printf("enum unsigned: wrong\n");
  554. if (getint(0) == BIASS)
  555. printf("enum unsigned: ok\n");
  556. else
  557. printf("enum unsigned: wrong\n");
  558. }
  559. typedef int *my_ptr;
  560. typedef int mytype1;
  561. typedef int mytype2;
  562. void typedef_test()
  563. {
  564. my_ptr a;
  565. mytype1 mytype2;
  566. int b;
  567. a = &b;
  568. *a = 1234;
  569. printf("typedef:\n");
  570. printf("a=%d\n", *a);
  571. mytype2 = 2;
  572. printf("mytype2=%d\n", mytype2);
  573. }
  574. void forward_test()
  575. {
  576. printf("forward:\n");
  577. forward_ref();
  578. forward_ref();
  579. }
  580. void forward_ref(void)
  581. {
  582. printf("forward ok\n");
  583. }
  584. typedef struct struct1 {
  585. int f1;
  586. int f2, f3;
  587. union union1 {
  588. int v1;
  589. int v2;
  590. } u;
  591. char str[3];
  592. } struct1;
  593. struct struct2 {
  594. int a;
  595. char b;
  596. };
  597. union union2 {
  598. int w1;
  599. int w2;
  600. };
  601. struct struct1 st1, st2;
  602. struct empty_mem {
  603. /* nothing */ ;
  604. int x;
  605. };
  606. int main(int argc, char **argv)
  607. {
  608. string_test();
  609. expr_test();
  610. macro_test();
  611. recursive_macro_test();
  612. scope_test();
  613. forward_test();
  614. funcptr_test();
  615. loop_test();
  616. switch_test();
  617. goto_test();
  618. enum_test();
  619. typedef_test();
  620. struct_test();
  621. array_test();
  622. expr_ptr_test();
  623. bool_test();
  624. optimize_out();
  625. expr2_test();
  626. constant_expr_test();
  627. expr_cmp_test();
  628. char_short_test();
  629. init_test();
  630. compound_literal_test();
  631. kr_test();
  632. struct_assign_test();
  633. cast_test();
  634. bitfield_test();
  635. c99_bool_test();
  636. float_test();
  637. longlong_test();
  638. manyarg_test();
  639. stdarg_test();
  640. whitespace_test();
  641. relocation_test();
  642. old_style_function();
  643. alloca_test();
  644. c99_vla_test(5, 2);
  645. sizeof_test();
  646. typeof_test();
  647. statement_expr_test();
  648. local_label_test();
  649. asm_test();
  650. builtin_test();
  651. #ifndef _WIN32
  652. weak_test();
  653. #endif
  654. global_data_test();
  655. cmp_comparison_test();
  656. math_cmp_test();
  657. callsave_test();
  658. builtin_frame_address_test();
  659. intdiv_test();
  660. if (via_volatile (42) != 42)
  661. printf ("via_volatile broken\n");
  662. attrib_test();
  663. return 0;
  664. }
  665. int tab[3];
  666. int tab2[3][2];
  667. int g;
  668. void f1(g)
  669. {
  670. printf("g1=%d\n", g);
  671. }
  672. void scope_test()
  673. {
  674. printf("scope:\n");
  675. g = 2;
  676. f1(1);
  677. printf("g2=%d\n", g);
  678. {
  679. int g;
  680. g = 3;
  681. printf("g3=%d\n", g);
  682. {
  683. int g;
  684. g = 4;
  685. printf("g4=%d\n", g);
  686. }
  687. }
  688. printf("g5=%d\n", g);
  689. }
  690. void array_test()
  691. {
  692. int i, j, a[4];
  693. printf("array:\n");
  694. printf("sizeof(a) = %d\n", sizeof(a));
  695. printf("sizeof(\"a\") = %d\n", sizeof("a"));
  696. #ifdef C99_MACROS
  697. printf("sizeof(__func__) = %d\n", sizeof(__func__));
  698. #endif
  699. printf("sizeof tab %d\n", sizeof(tab));
  700. printf("sizeof tab2 %d\n", sizeof tab2);
  701. tab[0] = 1;
  702. tab[1] = 2;
  703. tab[2] = 3;
  704. printf("%d %d %d\n", tab[0], tab[1], tab[2]);
  705. for(i=0;i<3;i++)
  706. for(j=0;j<2;j++)
  707. tab2[i][j] = 10 * i + j;
  708. for(i=0;i<3*2;i++) {
  709. printf(" %3d", ((int *)tab2)[i]);
  710. }
  711. printf("\n");
  712. printf("sizeof(size_t)=%d\n", sizeof(size_t));
  713. printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
  714. }
  715. void expr_test()
  716. {
  717. int a, b;
  718. a = 0;
  719. printf("%d\n", a += 1);
  720. printf("%d\n", a -= 2);
  721. printf("%d\n", a *= 31232132);
  722. printf("%d\n", a /= 4);
  723. printf("%d\n", a %= 20);
  724. printf("%d\n", a &= 6);
  725. printf("%d\n", a ^= 7);
  726. printf("%d\n", a |= 8);
  727. printf("%d\n", a >>= 3);
  728. printf("%d\n", a <<= 4);
  729. a = 22321;
  730. b = -22321;
  731. printf("%d\n", a + 1);
  732. printf("%d\n", a - 2);
  733. printf("%d\n", a * 312);
  734. printf("%d\n", a / 4);
  735. printf("%d\n", b / 4);
  736. printf("%d\n", (unsigned)b / 4);
  737. printf("%d\n", a % 20);
  738. printf("%d\n", b % 20);
  739. printf("%d\n", (unsigned)b % 20);
  740. printf("%d\n", a & 6);
  741. printf("%d\n", a ^ 7);
  742. printf("%d\n", a | 8);
  743. printf("%d\n", a >> 3);
  744. printf("%d\n", b >> 3);
  745. printf("%d\n", (unsigned)b >> 3);
  746. printf("%d\n", a << 4);
  747. printf("%d\n", ~a);
  748. printf("%d\n", -a);
  749. printf("%d\n", +a);
  750. printf("%d\n", 12 + 1);
  751. printf("%d\n", 12 - 2);
  752. printf("%d\n", 12 * 312);
  753. printf("%d\n", 12 / 4);
  754. printf("%d\n", 12 % 20);
  755. printf("%d\n", 12 & 6);
  756. printf("%d\n", 12 ^ 7);
  757. printf("%d\n", 12 | 8);
  758. printf("%d\n", 12 >> 2);
  759. printf("%d\n", 12 << 4);
  760. printf("%d\n", ~12);
  761. printf("%d\n", -12);
  762. printf("%d\n", +12);
  763. printf("%d %d %d %d\n",
  764. isid('a'),
  765. isid('g'),
  766. isid('T'),
  767. isid('('));
  768. }
  769. int isid(int c)
  770. {
  771. return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
  772. }
  773. /**********************/
  774. int vstack[10], *vstack_ptr;
  775. void vpush(int vt, int vc)
  776. {
  777. *vstack_ptr++ = vt;
  778. *vstack_ptr++ = vc;
  779. }
  780. void vpop(int *ft, int *fc)
  781. {
  782. *fc = *--vstack_ptr;
  783. *ft = *--vstack_ptr;
  784. }
  785. void expr2_test()
  786. {
  787. int a, b;
  788. printf("expr2:\n");
  789. vstack_ptr = vstack;
  790. vpush(1432432, 2);
  791. vstack_ptr[-2] &= ~0xffffff80;
  792. vpop(&a, &b);
  793. printf("res= %d %d\n", a, b);
  794. }
  795. void constant_expr_test()
  796. {
  797. int a;
  798. printf("constant_expr:\n");
  799. a = 3;
  800. printf("%d\n", a * 16);
  801. printf("%d\n", a * 1);
  802. printf("%d\n", a + 0);
  803. }
  804. int tab4[10];
  805. void expr_ptr_test()
  806. {
  807. int *p, *q;
  808. int i = -1;
  809. printf("expr_ptr:\n");
  810. p = tab4;
  811. q = tab4 + 10;
  812. printf("diff=%d\n", q - p);
  813. p++;
  814. printf("inc=%d\n", p - tab4);
  815. p--;
  816. printf("dec=%d\n", p - tab4);
  817. ++p;
  818. printf("inc=%d\n", p - tab4);
  819. --p;
  820. printf("dec=%d\n", p - tab4);
  821. printf("add=%d\n", p + 3 - tab4);
  822. printf("add=%d\n", 3 + p - tab4);
  823. /* check if 64bit support is ok */
  824. q = p = 0;
  825. q += i;
  826. printf("%p %p %ld\n", q, p, p-q);
  827. printf("%d %d %d %d %d %d\n",
  828. p == q, p != q, p < q, p <= q, p >= q, p > q);
  829. i = 0xf0000000;
  830. p += i;
  831. printf("%p %p %ld\n", q, p, p-q);
  832. printf("%d %d %d %d %d %d\n",
  833. p == q, p != q, p < q, p <= q, p >= q, p > q);
  834. p = (int *)((char *)p + 0xf0000000);
  835. printf("%p %p %ld\n", q, p, p-q);
  836. printf("%d %d %d %d %d %d\n",
  837. p == q, p != q, p < q, p <= q, p >= q, p > q);
  838. p += 0xf0000000;
  839. printf("%p %p %ld\n", q, p, p-q);
  840. printf("%d %d %d %d %d %d\n",
  841. p == q, p != q, p < q, p <= q, p >= q, p > q);
  842. {
  843. struct size12 {
  844. int i, j, k;
  845. };
  846. struct size12 s[2], *sp = s;
  847. int i, j;
  848. sp->i = 42;
  849. sp++;
  850. j = -1;
  851. printf("%d\n", sp[j].i);
  852. }
  853. #ifdef __LP64__
  854. i = 1;
  855. p = (int*)0x100000000UL + i;
  856. i = ((long)p) >> 32;
  857. printf("largeptr: %p %d\n", p, i);
  858. #endif
  859. }
  860. void expr_cmp_test()
  861. {
  862. int a, b;
  863. printf("constant_expr:\n");
  864. a = -1;
  865. b = 1;
  866. printf("%d\n", a == a);
  867. printf("%d\n", a != a);
  868. printf("%d\n", a < b);
  869. printf("%d\n", a <= b);
  870. printf("%d\n", a <= a);
  871. printf("%d\n", b >= a);
  872. printf("%d\n", a >= a);
  873. printf("%d\n", b > a);
  874. printf("%d\n", (unsigned)a < b);
  875. printf("%d\n", (unsigned)a <= b);
  876. printf("%d\n", (unsigned)a <= a);
  877. printf("%d\n", (unsigned)b >= a);
  878. printf("%d\n", (unsigned)a >= a);
  879. printf("%d\n", (unsigned)b > a);
  880. }
  881. struct empty {
  882. };
  883. struct aligntest1 {
  884. char a[10];
  885. };
  886. struct aligntest2 {
  887. int a;
  888. char b[10];
  889. };
  890. struct aligntest3 {
  891. double a, b;
  892. };
  893. struct aligntest4 {
  894. double a[0];
  895. };
  896. struct __attribute__((aligned(16))) aligntest5
  897. {
  898. int i;
  899. };
  900. struct aligntest6
  901. {
  902. int i;
  903. } __attribute__((aligned(16)));
  904. struct aligntest7
  905. {
  906. int i;
  907. };
  908. struct aligntest5 altest5[2];
  909. struct aligntest6 altest6[2];
  910. int pad1;
  911. /* altest7 is correctly aligned to 16 bytes also with TCC,
  912. but __alignof__ returns the wrong result (4) because we
  913. can't store the alignment yet when specified on symbols
  914. directly (it's stored in the type so we'd need to make
  915. a copy of it). -- FIXED */
  916. struct aligntest7 altest7[2] __attribute__((aligned(16)));
  917. struct aligntest8
  918. {
  919. int i;
  920. } __attribute__((aligned(4096)));
  921. struct Large {
  922. unsigned long flags;
  923. union {
  924. void *u1;
  925. int *u2;
  926. };
  927. struct {
  928. union {
  929. unsigned long index;
  930. void *freelist;
  931. };
  932. union {
  933. unsigned long counters;
  934. struct {
  935. int bla;
  936. };
  937. };
  938. };
  939. union {
  940. struct {
  941. long u3;
  942. long u4;
  943. };
  944. void *u5;
  945. struct {
  946. unsigned long compound_head;
  947. unsigned int compound_dtor;
  948. unsigned int compound_order;
  949. };
  950. };
  951. } __attribute__((aligned(2 * sizeof(long))));
  952. typedef unsigned long long __attribute__((aligned(4))) unaligned_u64;
  953. struct aligntest9 {
  954. unsigned int buf_nr;
  955. unaligned_u64 start_lba;
  956. };
  957. struct aligntest10 {
  958. unsigned int buf_nr;
  959. unsigned long long start_lba;
  960. };
  961. void struct_test()
  962. {
  963. struct1 *s;
  964. union union2 u;
  965. struct Large ls;
  966. printf("struct:\n");
  967. printf("sizes: %d %d %d %d\n",
  968. sizeof(struct struct1),
  969. sizeof(struct struct2),
  970. sizeof(union union1),
  971. sizeof(union union2));
  972. printf("offsets: %d\n", (int)((char*)&st1.u.v1 - (char*)&st1));
  973. st1.f1 = 1;
  974. st1.f2 = 2;
  975. st1.f3 = 3;
  976. printf("st1: %d %d %d\n",
  977. st1.f1, st1.f2, st1.f3);
  978. st1.u.v1 = 1;
  979. st1.u.v2 = 2;
  980. printf("union1: %d\n", st1.u.v1);
  981. u.w1 = 1;
  982. u.w2 = 2;
  983. printf("union2: %d\n", u.w1);
  984. s = &st2;
  985. s->f1 = 3;
  986. s->f2 = 2;
  987. s->f3 = 1;
  988. printf("st2: %d %d %d\n",
  989. s->f1, s->f2, s->f3);
  990. printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
  991. /* align / size tests */
  992. printf("aligntest1 sizeof=%d alignof=%d\n",
  993. sizeof(struct aligntest1), __alignof__(struct aligntest1));
  994. printf("aligntest2 sizeof=%d alignof=%d\n",
  995. sizeof(struct aligntest2), __alignof__(struct aligntest2));
  996. printf("aligntest3 sizeof=%d alignof=%d\n",
  997. sizeof(struct aligntest3), __alignof__(struct aligntest3));
  998. printf("aligntest4 sizeof=%d alignof=%d\n",
  999. sizeof(struct aligntest4), __alignof__(struct aligntest4));
  1000. printf("aligntest5 sizeof=%d alignof=%d\n",
  1001. sizeof(struct aligntest5), __alignof__(struct aligntest5));
  1002. printf("aligntest6 sizeof=%d alignof=%d\n",
  1003. sizeof(struct aligntest6), __alignof__(struct aligntest6));
  1004. printf("aligntest7 sizeof=%d alignof=%d\n",
  1005. sizeof(struct aligntest7), __alignof__(struct aligntest7));
  1006. printf("aligntest8 sizeof=%d alignof=%d\n",
  1007. sizeof(struct aligntest8), __alignof__(struct aligntest8));
  1008. printf("aligntest9 sizeof=%d alignof=%d\n",
  1009. sizeof(struct aligntest9), __alignof__(struct aligntest9));
  1010. printf("aligntest10 sizeof=%d alignof=%d\n",
  1011. sizeof(struct aligntest10), __alignof__(struct aligntest10));
  1012. printf("altest5 sizeof=%d alignof=%d\n",
  1013. sizeof(altest5), __alignof__(altest5));
  1014. printf("altest6 sizeof=%d alignof=%d\n",
  1015. sizeof(altest6), __alignof__(altest6));
  1016. printf("altest7 sizeof=%d alignof=%d\n",
  1017. sizeof(altest7), __alignof__(altest7));
  1018. /* empty structures (GCC extension) */
  1019. printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
  1020. printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
  1021. printf("Large: sizeof=%d\n", sizeof(ls));
  1022. memset(&ls, 0, sizeof(ls));
  1023. ls.compound_head = 42;
  1024. printf("Large: offsetof(compound_head)=%d\n", (int)((char*)&ls.compound_head - (char*)&ls));
  1025. }
  1026. /* XXX: depend on endianness */
  1027. void char_short_test()
  1028. {
  1029. int var1, var2;
  1030. printf("char_short:\n");
  1031. var1 = 0x01020304;
  1032. var2 = 0xfffefdfc;
  1033. printf("s8=%d %d\n",
  1034. *(char *)&var1, *(char *)&var2);
  1035. printf("u8=%d %d\n",
  1036. *(unsigned char *)&var1, *(unsigned char *)&var2);
  1037. printf("s16=%d %d\n",
  1038. *(short *)&var1, *(short *)&var2);
  1039. printf("u16=%d %d\n",
  1040. *(unsigned short *)&var1, *(unsigned short *)&var2);
  1041. printf("s32=%d %d\n",
  1042. *(int *)&var1, *(int *)&var2);
  1043. printf("u32=%d %d\n",
  1044. *(unsigned int *)&var1, *(unsigned int *)&var2);
  1045. *(char *)&var1 = 0x08;
  1046. printf("var1=%x\n", var1);
  1047. *(short *)&var1 = 0x0809;
  1048. printf("var1=%x\n", var1);
  1049. *(int *)&var1 = 0x08090a0b;
  1050. printf("var1=%x\n", var1);
  1051. }
  1052. /******************/
  1053. typedef struct Sym {
  1054. int v;
  1055. int t;
  1056. int c;
  1057. struct Sym *next;
  1058. struct Sym *prev;
  1059. } Sym;
  1060. #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
  1061. #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
  1062. static int toupper1(int a)
  1063. {
  1064. return TOUPPER(a);
  1065. }
  1066. static unsigned int calc_vm_flags(unsigned int prot)
  1067. {
  1068. unsigned int prot_bits;
  1069. /* This used to segfault in some revisions: */
  1070. prot_bits = ((0x1==0x00000001)?(prot&0x1):(prot&0x1)?0x00000001:0);
  1071. return prot_bits;
  1072. }
  1073. void bool_test()
  1074. {
  1075. int *s, a, b, t, f, i;
  1076. a = 0;
  1077. s = (void*)0;
  1078. printf("!s=%d\n", !s);
  1079. if (!s || !s[0])
  1080. a = 1;
  1081. printf("a=%d\n", a);
  1082. printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
  1083. printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
  1084. printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
  1085. #if 1 && 1
  1086. printf("a1\n");
  1087. #endif
  1088. #if 1 || 0
  1089. printf("a2\n");
  1090. #endif
  1091. #if 1 ? 0 : 1
  1092. printf("a3\n");
  1093. #endif
  1094. #if 0 ? 0 : 1
  1095. printf("a4\n");
  1096. #endif
  1097. a = 4;
  1098. printf("b=%d\n", a + (0 ? 1 : a / 2));
  1099. /* test register spilling */
  1100. a = 10;
  1101. b = 10;
  1102. a = (a + b) * ((a < b) ?
  1103. ((b - a) * (a - b)): a + b);
  1104. printf("a=%d\n", a);
  1105. /* test complex || or && expressions */
  1106. t = 1;
  1107. f = 0;
  1108. a = 32;
  1109. printf("exp=%d\n", f == (32 <= a && a <= 3));
  1110. printf("r=%d\n", (t || f) + (t && f));
  1111. /* test ? : cast */
  1112. {
  1113. int aspect_on;
  1114. int aspect_native = 65536;
  1115. double bfu_aspect = 1.0;
  1116. int aspect;
  1117. for(aspect_on = 0; aspect_on < 2; aspect_on++) {
  1118. aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
  1119. printf("aspect=%d\n", aspect);
  1120. }
  1121. }
  1122. /* test ? : GCC extension */
  1123. {
  1124. static int v1 = 34 ? : -1; /* constant case */
  1125. static int v2 = 0 ? : -1; /* constant case */
  1126. int a = 30;
  1127. printf("%d %d\n", v1, v2);
  1128. printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
  1129. }
  1130. /* again complex expression */
  1131. for(i=0;i<256;i++) {
  1132. if (toupper1 (i) != TOUPPER (i))
  1133. printf("error %d\n", i);
  1134. }
  1135. printf ("bits = 0x%x\n", calc_vm_flags (0x1));
  1136. }
  1137. extern int undefined_function(void);
  1138. extern int defined_function(void);
  1139. static inline void refer_to_undefined(void)
  1140. {
  1141. undefined_function();
  1142. }
  1143. void optimize_out(void)
  1144. {
  1145. int i = 0 ? undefined_function() : defined_function();
  1146. printf ("oo:%d\n", i);
  1147. int j = 1 ? defined_function() : undefined_function();
  1148. printf ("oo:%d\n", j);
  1149. if (0)
  1150. printf("oo:%d\n", undefined_function());
  1151. else
  1152. printf("oo:%d\n", defined_function());
  1153. if (1)
  1154. printf("oo:%d\n", defined_function());
  1155. else
  1156. printf("oo:%d\n", undefined_function());
  1157. while (1) {
  1158. printf("oow:%d\n", defined_function());
  1159. break;
  1160. printf("oow:%d\n", undefined_function());
  1161. }
  1162. j = 1;
  1163. /* Following is a switch without {} block intentionally. */
  1164. switch (j)
  1165. case 1: break;
  1166. printf ("oos:%d\n", defined_function());
  1167. /* The following break shouldn't lead to disabled code after
  1168. the while. */
  1169. while (1)
  1170. break;
  1171. printf ("ool1:%d\n", defined_function());
  1172. /* Same for the other types of loops. */
  1173. do
  1174. break;
  1175. while (1);
  1176. printf ("ool2:%d\n", defined_function());
  1177. for (;;)
  1178. break;
  1179. printf ("ool3:%d\n", defined_function());
  1180. /* Normal {} blocks without controlling statements
  1181. shouldn't reactivate code emission */
  1182. while (1) {
  1183. {
  1184. break;
  1185. }
  1186. printf ("ool4:%d\n", undefined_function());
  1187. }
  1188. j = 1;
  1189. while (j) {
  1190. if (j == 0)
  1191. break; /* this break shouldn't disable code outside the if. */
  1192. printf("ool5:%d\n", defined_function());
  1193. j--;
  1194. }
  1195. j = 1;
  1196. while (j) {
  1197. if (1)
  1198. j--;
  1199. else
  1200. breakhere: break;
  1201. printf("ool6:%d\n", defined_function());
  1202. goto breakhere;
  1203. }
  1204. /* Test that constants in logical && are optimized: */
  1205. i = 0 && undefined_function();
  1206. i = defined_function() && 0 && undefined_function();
  1207. if (0 && undefined_function())
  1208. undefined_function();
  1209. if (defined_function() && 0)
  1210. undefined_function();
  1211. if (0 && 0)
  1212. undefined_function();
  1213. if (defined_function() && 0 && undefined_function())
  1214. undefined_function();
  1215. /* The same for || : */
  1216. i = 1 || undefined_function();
  1217. i = defined_function() || 1 || undefined_function();
  1218. if (1 || undefined_function())
  1219. ;
  1220. else
  1221. undefined_function();
  1222. if (defined_function() || 1)
  1223. ;
  1224. else
  1225. undefined_function();
  1226. if (1 || 1)
  1227. ;
  1228. else
  1229. undefined_function();
  1230. if (defined_function() || 1 || undefined_function())
  1231. ;
  1232. else
  1233. undefined_function();
  1234. if (defined_function() && 0)
  1235. refer_to_undefined();
  1236. if (0) {
  1237. (void)sizeof( ({
  1238. do { } while (0);
  1239. 0;
  1240. }) );
  1241. undefined_function();
  1242. }
  1243. /* Leave the "if(1)return; printf()" in this order and last in the function */
  1244. if (1)
  1245. return;
  1246. printf ("oor:%d\n", undefined_function());
  1247. }
  1248. int defined_function(void)
  1249. {
  1250. static int i = 40;
  1251. return i++;
  1252. }
  1253. /* GCC accepts that */
  1254. static int tab_reinit[];
  1255. static int tab_reinit[10];
  1256. //int cinit1; /* a global variable can be defined several times without error ! */
  1257. int cinit1;
  1258. int cinit1;
  1259. int cinit1 = 0;
  1260. int *cinit2 = (int []){3, 2, 1};
  1261. void compound_literal_test(void)
  1262. {
  1263. int *p, i;
  1264. char *q, *q3;
  1265. printf("compound_test:\n");
  1266. p = (int []){1, 2, 3};
  1267. for(i=0;i<3;i++)
  1268. printf(" %d", p[i]);
  1269. printf("\n");
  1270. for(i=0;i<3;i++)
  1271. printf("%d", cinit2[i]);
  1272. printf("\n");
  1273. q = "tralala1";
  1274. printf("q1=%s\n", q);
  1275. q = (char *){ "tralala2" };
  1276. printf("q2=%s\n", q);
  1277. q3 = (char *){ q };
  1278. printf("q3=%s\n", q3);
  1279. q = (char []){ "tralala3" };
  1280. printf("q4=%s\n", q);
  1281. #ifdef ALL_ISOC99
  1282. p = (int []){1, 2, cinit1 + 3};
  1283. for(i=0;i<3;i++)
  1284. printf(" %d", p[i]);
  1285. printf("\n");
  1286. for(i=0;i<3;i++) {
  1287. p = (int []){1, 2, 4 + i};
  1288. printf("%d %d %d\n",
  1289. p[0],
  1290. p[1],
  1291. p[2]);
  1292. }
  1293. #endif
  1294. }
  1295. /* K & R protos */
  1296. kr_func1(a, b)
  1297. {
  1298. return a + b;
  1299. }
  1300. int kr_func2(a, b)
  1301. {
  1302. return a + b;
  1303. }
  1304. kr_test()
  1305. {
  1306. printf("kr_test:\n");
  1307. printf("func1=%d\n", kr_func1(3, 4));
  1308. printf("func2=%d\n", kr_func2(3, 4));
  1309. return 0;
  1310. }
  1311. void num(int n)
  1312. {
  1313. char *tab, *p;
  1314. tab = (char*)malloc(20);
  1315. p = tab;
  1316. while (1) {
  1317. *p = 48 + (n % 10);
  1318. p++;
  1319. n = n / 10;
  1320. if (n == 0)
  1321. break;
  1322. }
  1323. while (p != tab) {
  1324. p--;
  1325. printf("%c", *p);
  1326. }
  1327. printf("\n");
  1328. free(tab);
  1329. }
  1330. /* structure assignment tests */
  1331. struct structa1 {
  1332. int f1;
  1333. char f2;
  1334. };
  1335. struct structa1 ssta1;
  1336. void struct_assign_test1(struct structa1 s1, int t, float f)
  1337. {
  1338. printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
  1339. }
  1340. struct structa1 struct_assign_test2(struct structa1 s1, int t)
  1341. {
  1342. s1.f1 += t;
  1343. s1.f2 -= t;
  1344. return s1;
  1345. }
  1346. void struct_assign_test(void)
  1347. {
  1348. struct S {
  1349. struct structa1 lsta1, lsta2;
  1350. int i;
  1351. } s, *ps;
  1352. ps = &s;
  1353. ps->i = 4;
  1354. #if 0
  1355. printf("struct_assign_test:\n");
  1356. s.lsta1.f1 = 1;
  1357. s.lsta1.f2 = 2;
  1358. printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
  1359. s.lsta2 = s.lsta1;
  1360. printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
  1361. #else
  1362. s.lsta2.f1 = 1;
  1363. s.lsta2.f2 = 2;
  1364. #endif
  1365. struct_assign_test1(ps->lsta2, 3, 4.5);
  1366. printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
  1367. ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
  1368. printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
  1369. static struct {
  1370. void (*elem)();
  1371. } t[] = {
  1372. /* XXX: we should allow this even without braces */
  1373. { struct_assign_test }
  1374. };
  1375. printf("%d\n", struct_assign_test == t[0].elem);
  1376. }
  1377. /* casts to short/char */
  1378. void cast1(char a, short b, unsigned char c, unsigned short d)
  1379. {
  1380. printf("%d %d %d %d\n", a, b, c, d);
  1381. }
  1382. char bcast;
  1383. short scast;
  1384. void cast_test()
  1385. {
  1386. int a;
  1387. char c;
  1388. char tab[10];
  1389. unsigned b,d;
  1390. short s;
  1391. char *p = NULL;
  1392. p -= 0x700000000042;
  1393. printf("cast_test:\n");
  1394. a = 0xfffff;
  1395. cast1(a, a, a, a);
  1396. a = 0xffffe;
  1397. printf("%d %d %d %d\n",
  1398. (char)(a + 1),
  1399. (short)(a + 1),
  1400. (unsigned char)(a + 1),
  1401. (unsigned short)(a + 1));
  1402. printf("%d %d %d %d\n",
  1403. (char)0xfffff,
  1404. (short)0xfffff,
  1405. (unsigned char)0xfffff,
  1406. (unsigned short)0xfffff);
  1407. a = (bcast = 128) + 1;
  1408. printf("%d\n", a);
  1409. a = (scast = 65536) + 1;
  1410. printf("%d\n", a);
  1411. printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
  1412. /* test cast from unsigned to signed short to int */
  1413. b = 0xf000;
  1414. d = (short)b;
  1415. printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
  1416. b = 0xf0f0;
  1417. d = (char)b;
  1418. printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
  1419. /* test implicit int casting for array accesses */
  1420. c = 0;
  1421. tab[1] = 2;
  1422. tab[c] = 1;
  1423. printf("%d %d\n", tab[0], tab[1]);
  1424. /* test implicit casting on some operators */
  1425. printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
  1426. printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
  1427. printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
  1428. /* from pointer to integer types */
  1429. printf("%d %d %ld %ld %lld %lld\n",
  1430. (int)p, (unsigned int)p,
  1431. (long)p, (unsigned long)p,
  1432. (long long)p, (unsigned long long)p);
  1433. /* from integers to pointers */
  1434. printf("%p %p %p %p\n",
  1435. (void *)a, (void *)b, (void *)c, (void *)d);
  1436. }
  1437. /* initializers tests */
  1438. struct structinit1 {
  1439. int f1;
  1440. char f2;
  1441. short f3;
  1442. int farray[3];
  1443. };
  1444. int sinit1 = 2;
  1445. int sinit2 = { 3 };
  1446. int sinit3[3] = { 1, 2, {{3}}, };
  1447. int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
  1448. int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
  1449. int sinit6[] = { 1, 2, 3 };
  1450. int sinit7[] = { [2] = 3, [0] = 1, 2 };
  1451. char sinit8[] = "hello" "trala";
  1452. struct structinit1 sinit9 = { 1, 2, 3 };
  1453. struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
  1454. struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
  1455. #ifdef ALL_ISOC99
  1456. .farray[0] = 10,
  1457. .farray[1] = 11,
  1458. .farray[2] = 12,
  1459. #endif
  1460. };
  1461. char *sinit12 = "hello world";
  1462. char *sinit13[] = {
  1463. "test1",
  1464. "test2",
  1465. "test3",
  1466. };
  1467. char sinit14[10] = { "abc" };
  1468. int sinit15[3] = { sizeof(sinit15), 1, 2 };
  1469. struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
  1470. struct bar {
  1471. char *s;
  1472. int len;
  1473. } sinit17[] = {
  1474. "a1", 4,
  1475. "a2", 1
  1476. };
  1477. int sinit18[10] = {
  1478. [2 ... 5] = 20,
  1479. 2,
  1480. [8] = 10,
  1481. };
  1482. struct complexinit0 {
  1483. int a;
  1484. int b;
  1485. };
  1486. struct complexinit {
  1487. int a;
  1488. const struct complexinit0 *b;
  1489. };
  1490. const static struct complexinit cix[] = {
  1491. [0] = {
  1492. .a = 2000,
  1493. .b = (const struct complexinit0[]) {
  1494. { 2001, 2002 },
  1495. { 2003, 2003 },
  1496. {}
  1497. }
  1498. }
  1499. };
  1500. struct complexinit2 {
  1501. int a;
  1502. int b[];
  1503. };
  1504. struct complexinit2 cix20;
  1505. struct complexinit2 cix21 = {
  1506. .a = 3000,
  1507. .b = { 3001, 3002, 3003 }
  1508. };
  1509. struct complexinit2 cix22 = {
  1510. .a = 4000,
  1511. .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
  1512. };
  1513. typedef int arrtype1[];
  1514. arrtype1 sinit19 = {1};
  1515. arrtype1 sinit20 = {2,3};
  1516. typedef int arrtype2[3];
  1517. arrtype2 sinit21 = {4};
  1518. arrtype2 sinit22 = {5,6,7};
  1519. /* Address comparisons of non-weak symbols with zero can be const-folded */
  1520. int sinit23[2] = { "astring" ? sizeof("astring") : -1,
  1521. &sinit23 ? 42 : -1 };
  1522. extern int external_inited = 42;
  1523. void init_test(void)
  1524. {
  1525. int linit1 = 2;
  1526. int linit2 = { 3 };
  1527. int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
  1528. int linit6[] = { 1, 2, 3 };
  1529. int i, j;
  1530. char linit8[] = "hello" "trala";
  1531. int linit12[10] = { 1, 2 };
  1532. int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
  1533. char linit14[10] = "abc";
  1534. int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
  1535. struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
  1536. int linit17 = sizeof(linit17);
  1537. int zero = 0;
  1538. /* Addresses on non-weak symbols are non-zero, but not the access itself */
  1539. int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };
  1540. printf("init_test:\n");
  1541. printf("sinit1=%d\n", sinit1);
  1542. printf("sinit2=%d\n", sinit2);
  1543. printf("sinit3=%d %d %d %d\n",
  1544. sizeof(sinit3),
  1545. sinit3[0],
  1546. sinit3[1],
  1547. sinit3[2]
  1548. );
  1549. printf("sinit6=%d\n", sizeof(sinit6));
  1550. printf("sinit7=%d %d %d %d\n",
  1551. sizeof(sinit7),
  1552. sinit7[0],
  1553. sinit7[1],
  1554. sinit7[2]
  1555. );
  1556. printf("sinit8=%s\n", sinit8);
  1557. printf("sinit9=%d %d %d\n",
  1558. sinit9.f1,
  1559. sinit9.f2,
  1560. sinit9.f3
  1561. );
  1562. printf("sinit10=%d %d %d\n",
  1563. sinit10.f1,
  1564. sinit10.f2,
  1565. sinit10.f3
  1566. );
  1567. printf("sinit11=%d %d %d %d %d %d\n",
  1568. sinit11.f1,
  1569. sinit11.f2,
  1570. sinit11.f3,
  1571. sinit11.farray[0],
  1572. sinit11.farray[1],
  1573. sinit11.farray[2]
  1574. );
  1575. for(i=0;i<3;i++)
  1576. for(j=0;j<2;j++)
  1577. printf("[%d][%d] = %d %d %d\n",
  1578. i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
  1579. printf("linit1=%d\n", linit1);
  1580. printf("linit2=%d\n", linit2);
  1581. printf("linit6=%d\n", sizeof(linit6));
  1582. printf("linit8=%d %s\n", sizeof(linit8), linit8);
  1583. printf("sinit12=%s\n", sinit12);
  1584. printf("sinit13=%d %s %s %s\n",
  1585. sizeof(sinit13),
  1586. sinit13[0],
  1587. sinit13[1],
  1588. sinit13[2]);
  1589. printf("sinit14=%s\n", sinit14);
  1590. for(i=0;i<10;i++) printf(" %d", linit12[i]);
  1591. printf("\n");
  1592. for(i=0;i<10;i++) printf(" %d", linit13[i]);
  1593. printf("\n");
  1594. for(i=0;i<10;i++) printf(" %d", linit14[i]);
  1595. printf("\n");
  1596. for(i=0;i<10;i++) printf(" %d", linit15[i]);
  1597. printf("\n");
  1598. printf("%d %d %d %d\n",
  1599. linit16.a1,
  1600. linit16.a2,
  1601. linit16.a3,
  1602. linit16.a4);
  1603. /* test that initialisation is done after variable declare */
  1604. printf("linit17=%d\n", linit17);
  1605. printf("sinit15=%d\n", sinit15[0]);
  1606. printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
  1607. printf("sinit17=%s %d %s %d\n",
  1608. sinit17[0].s, sinit17[0].len,
  1609. sinit17[1].s, sinit17[1].len);
  1610. for(i=0;i<10;i++)
  1611. printf("%x ", sinit18[i]);
  1612. printf("\n");
  1613. /* complex init check */
  1614. printf("cix: %d %d %d %d %d %d %d\n",
  1615. cix[0].a,
  1616. cix[0].b[0].a, cix[0].b[0].b,
  1617. cix[0].b[1].a, cix[0].b[1].b,
  1618. cix[0].b[2].a, cix[0].b[2].b);
  1619. printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
  1620. printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
  1621. printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]);
  1622. printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20));
  1623. printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]);
  1624. printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
  1625. printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
  1626. printf("arrtype6: %d\n", sizeof(arrtype2));
  1627. printf("sinit23= %d %d\n", sinit23[0], sinit23[1]);
  1628. printf("linit18= %d %d\n", linit18[0], linit18[1]);
  1629. }
  1630. void switch_uc(unsigned char uc)
  1631. {
  1632. switch (uc) {
  1633. case 0xfb ... 0xfe:
  1634. printf("ucsw:1\n");
  1635. break;
  1636. case 0xff:
  1637. printf("ucsw:2\n");
  1638. break;
  1639. case 0 ... 5:
  1640. printf("ucsw:3\n");
  1641. break;
  1642. default:
  1643. printf("ucsw: broken!\n");
  1644. }
  1645. }
  1646. void switch_sc(signed char sc)
  1647. {
  1648. switch (sc) {
  1649. case -5 ... -2:
  1650. printf("scsw:1\n");
  1651. break;
  1652. case -1:
  1653. printf("scsw:2\n");
  1654. break;
  1655. case 0 ... 5:
  1656. printf("scsw:3\n");
  1657. break;
  1658. default:
  1659. printf("scsw: broken!\n");
  1660. }
  1661. }
  1662. void switch_test()
  1663. {
  1664. int i;
  1665. unsigned long long ull;
  1666. long long ll;
  1667. for(i=0;i<15;i++) {
  1668. switch(i) {
  1669. case 0:
  1670. case 1:
  1671. printf("a");
  1672. break;
  1673. default:
  1674. printf("%d", i);
  1675. break;
  1676. case 8 ... 12:
  1677. printf("c");
  1678. break;
  1679. case 3:
  1680. printf("b");
  1681. break;
  1682. case 0xc33c6b9fU:
  1683. case 0x7c9eeeb9U:
  1684. break;
  1685. }
  1686. }
  1687. printf("\n");
  1688. for (i = 1; i <= 5; i++) {
  1689. ull = (unsigned long long)i << 61;
  1690. switch (ull) {
  1691. case 1ULL << 61:
  1692. printf("ullsw:1\n");
  1693. break;
  1694. case 2ULL << 61:
  1695. printf("ullsw:2\n");
  1696. break;
  1697. case 3ULL << 61:
  1698. printf("ullsw:3\n");
  1699. break;
  1700. case 4ULL << 61:
  1701. printf("ullsw:4\n");
  1702. break;
  1703. case 5ULL << 61:
  1704. printf("ullsw:5\n");
  1705. break;
  1706. default:
  1707. printf("ullsw: broken!\n");
  1708. }
  1709. }
  1710. for (i = 1; i <= 5; i++) {
  1711. ll = (long long)i << 61;
  1712. switch (ll) {
  1713. case 1LL << 61:
  1714. printf("llsw:1\n");
  1715. break;
  1716. case 2LL << 61:
  1717. printf("llsw:2\n");
  1718. break;
  1719. case 3LL << 61:
  1720. printf("llsw:3\n");
  1721. break;
  1722. case 4LL << 61:
  1723. printf("llsw:4\n");
  1724. break;
  1725. case 5LL << 61:
  1726. printf("llsw:5\n");
  1727. break;
  1728. default:
  1729. printf("llsw: broken!\n");
  1730. }
  1731. }
  1732. for (i = -5; i <= 5; i++) {
  1733. switch_uc((unsigned char)i);
  1734. }
  1735. for (i = -5; i <= 5; i++) {
  1736. switch_sc ((signed char)i);
  1737. }
  1738. }
  1739. /* ISOC99 _Bool type */
  1740. void c99_bool_test(void)
  1741. {
  1742. #ifdef BOOL_ISOC99
  1743. int a;
  1744. _Bool b;
  1745. printf("bool_test:\n");
  1746. printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
  1747. a = 3;
  1748. printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
  1749. b = 3;
  1750. printf("b = %d\n", b);
  1751. b++;
  1752. printf("b = %d\n", b);
  1753. #endif
  1754. }
  1755. void bitfield_test(void)
  1756. {
  1757. int a;
  1758. short sa;
  1759. unsigned char ca;
  1760. struct sbf1 {
  1761. int f1 : 3;
  1762. int : 2;
  1763. int f2 : 1;
  1764. int : 0;
  1765. int f3 : 5;
  1766. int f4 : 7;
  1767. unsigned int f5 : 7;
  1768. } st1;
  1769. printf("bitfield_test:");
  1770. printf("sizeof(st1) = %d\n", sizeof(st1));
  1771. st1.f1 = 3;
  1772. st1.f2 = 1;
  1773. st1.f3 = 15;
  1774. a = 120;
  1775. st1.f4 = a;
  1776. st1.f5 = a;
  1777. st1.f5++;
  1778. printf("%d %d %d %d %d\n",
  1779. st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
  1780. sa = st1.f5;
  1781. ca = st1.f5;
  1782. printf("%d %d\n", sa, ca);
  1783. st1.f1 = 7;
  1784. if (st1.f1 == -1)
  1785. printf("st1.f1 == -1\n");
  1786. else
  1787. printf("st1.f1 != -1\n");
  1788. if (st1.f2 == -1)
  1789. printf("st1.f2 == -1\n");
  1790. else
  1791. printf("st1.f2 != -1\n");
  1792. struct sbf2 {
  1793. long long f1 : 45;
  1794. long long : 2;
  1795. long long f2 : 35;
  1796. unsigned long long f3 : 38;
  1797. } st2;
  1798. st2.f1 = 0x123456789ULL;
  1799. a = 120;
  1800. st2.f2 = (long long)a << 25;
  1801. st2.f3 = a;
  1802. st2.f2++;
  1803. printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
  1804. #if 0
  1805. Disabled for now until further clarification re GCC compatibility
  1806. struct sbf3 {
  1807. int f1 : 7;
  1808. int f2 : 1;
  1809. char f3;
  1810. int f4 : 8;
  1811. int f5 : 1;
  1812. int f6 : 16;
  1813. } st3;
  1814. printf("sizeof(st3) = %d\n", sizeof(st3));
  1815. #endif
  1816. struct sbf4 {
  1817. int x : 31;
  1818. char y : 2;
  1819. } st4;
  1820. st4.y = 1;
  1821. printf("st4.y == %d\n", st4.y);
  1822. struct sbf5 {
  1823. int a;
  1824. char b;
  1825. int x : 12, y : 4, : 0, : 4, z : 3;
  1826. char c;
  1827. } st5 = { 1, 2, 3, 4, -3, 6 };
  1828. printf("st5 = %d %d %d %d %d %d\n", st5.a, st5.b, st5.x, st5.y, st5.z, st5.c);
  1829. struct sbf6 {
  1830. short x : 12;
  1831. unsigned char y : 2;
  1832. } st6;
  1833. st6.y = 1;
  1834. printf("st6.y == %d\n", st6.y);
  1835. }
  1836. #ifdef __x86_64__
  1837. #define FLOAT_FMT "%f\n"
  1838. #else
  1839. /* x86's float isn't compatible with GCC */
  1840. #define FLOAT_FMT "%.5f\n"
  1841. #endif
  1842. /* declare strto* functions as they are C99 */
  1843. double strtod(const char *nptr, char **endptr);
  1844. #if defined(_WIN32)
  1845. float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
  1846. LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
  1847. #else
  1848. float strtof(const char *nptr, char **endptr);
  1849. LONG_DOUBLE strtold(const char *nptr, char **endptr);
  1850. #endif
  1851. #define FTEST(prefix, typename, type, fmt)\
  1852. void prefix ## cmp(type a, type b)\
  1853. {\
  1854. printf("%d %d %d %d %d %d\n",\
  1855. a == b,\
  1856. a != b,\
  1857. a < b,\
  1858. a > b,\
  1859. a >= b,\
  1860. a <= b);\
  1861. printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
  1862. a,\
  1863. b,\
  1864. a + b,\
  1865. a - b,\
  1866. a * b,\
  1867. a / b,\
  1868. -a);\
  1869. printf(fmt "\n", ++a);\
  1870. printf(fmt "\n", a++);\
  1871. printf(fmt "\n", a);\
  1872. b = 0;\
  1873. printf("%d %d\n", !a, !b);\
  1874. }\
  1875. void prefix ## fcast(type a)\
  1876. {\
  1877. float fa;\
  1878. double da;\
  1879. LONG_DOUBLE la;\
  1880. int ia;\
  1881. long long llia;\
  1882. unsigned int ua;\
  1883. unsigned long long llua;\
  1884. type b;\
  1885. fa = a;\
  1886. da = a;\
  1887. la = a;\
  1888. printf("ftof: %f %f %Lf\n", fa, da, la);\
  1889. ia = (int)a;\
  1890. llia = (long long)a;\
  1891. a = (a >= 0) ? a : -a;\
  1892. ua = (unsigned int)a;\
  1893. llua = (unsigned long long)a;\
  1894. printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
  1895. ia = -1234;\
  1896. ua = 0x81234500;\
  1897. llia = -0x123456789012345LL;\
  1898. llua = 0xf123456789012345LLU;\
  1899. b = ia;\
  1900. printf("itof: " fmt "\n", b);\
  1901. b = ua;\
  1902. printf("utof: " fmt "\n", b);\
  1903. b = llia;\
  1904. printf("lltof: " fmt "\n", b);\
  1905. b = llua;\
  1906. printf("ulltof: " fmt "\n", b);\
  1907. }\
  1908. \
  1909. float prefix ## retf(type a) { return a; }\
  1910. double prefix ## retd(type a) { return a; }\
  1911. LONG_DOUBLE prefix ## retld(type a) { return a; }\
  1912. \
  1913. void prefix ## call(void)\
  1914. {\
  1915. printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
  1916. printf("double: %f\n", prefix ## retd(42.123456789));\
  1917. printf("long double: %Lf\n", prefix ## retld(42.123456789));\
  1918. printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
  1919. }\
  1920. \
  1921. void prefix ## signed_zeros(void) \
  1922. {\
  1923. type x = 0.0, y = -0.0, n, p;\
  1924. if (x == y)\
  1925. printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
  1926. 1.0 / x != 1.0 / y);\
  1927. else\
  1928. printf ("x != y; this is wrong!\n");\
  1929. \
  1930. n = -x;\
  1931. if (x == n)\
  1932. printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
  1933. 1.0 / x != 1.0 / n);\
  1934. else\
  1935. printf ("x != -x; this is wrong!\n");\
  1936. \
  1937. p = +y;\
  1938. if (x == p)\
  1939. printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
  1940. 1.0 / x != 1.0 / p);\
  1941. else\
  1942. printf ("x != +y; this is wrong!\n");\
  1943. p = -y;\
  1944. if (x == p)\
  1945. printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
  1946. 1.0 / x != 1.0 / p);\
  1947. else\
  1948. printf ("x != -y; this is wrong!\n");\
  1949. }\
  1950. void prefix ## test(void)\
  1951. {\
  1952. printf("testing '%s'\n", #typename);\
  1953. prefix ## cmp(1, 2.5);\
  1954. prefix ## cmp(2, 1.5);\
  1955. prefix ## cmp(1, 1);\
  1956. prefix ## fcast(234.6);\
  1957. prefix ## fcast(-2334.6);\
  1958. prefix ## call();\
  1959. prefix ## signed_zeros();\
  1960. }
  1961. FTEST(f, float, float, "%f")
  1962. FTEST(d, double, double, "%f")
  1963. FTEST(ld, long double, LONG_DOUBLE, "%Lf")
  1964. double ftab1[3] = { 1.2, 3.4, -5.6 };
  1965. void float_test(void)
  1966. {
  1967. #if !defined(__arm__) || defined(__ARM_PCS_VFP)
  1968. float fa, fb;
  1969. double da, db;
  1970. int a;
  1971. unsigned int b;
  1972. printf("float_test:\n");
  1973. printf("sizeof(float) = %d\n", sizeof(float));
  1974. printf("sizeof(double) = %d\n", sizeof(double));
  1975. printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
  1976. ftest();
  1977. dtest();
  1978. ldtest();
  1979. printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
  1980. printf("%f %f %f\n", 2.12, .5, 2.3e10);
  1981. // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
  1982. da = 123;
  1983. printf("da=%f\n", da);
  1984. fa = 123;
  1985. printf("fa=%f\n", fa);
  1986. a = 4000000000;
  1987. da = a;
  1988. printf("da = %f\n", da);
  1989. b = 4000000000;
  1990. db = b;
  1991. printf("db = %f\n", db);
  1992. #endif
  1993. }
  1994. int fib(int n)
  1995. {
  1996. if (n <= 2)
  1997. return 1;
  1998. else
  1999. return fib(n-1) + fib(n-2);
  2000. }
  2001. void funcptr_test()
  2002. {
  2003. void (*func)(int);
  2004. int a;
  2005. struct {
  2006. int dummy;
  2007. void (*func)(int);
  2008. } st1;
  2009. long diff;
  2010. printf("funcptr:\n");
  2011. func = &num;
  2012. (*func)(12345);
  2013. func = num;
  2014. a = 1;
  2015. a = 1;
  2016. func(12345);
  2017. /* more complicated pointer computation */
  2018. st1.func = num;
  2019. st1.func(12346);
  2020. printf("sizeof1 = %d\n", sizeof(funcptr_test));
  2021. printf("sizeof2 = %d\n", sizeof funcptr_test);
  2022. printf("sizeof3 = %d\n", sizeof(&funcptr_test));
  2023. printf("sizeof4 = %d\n", sizeof &funcptr_test);
  2024. a = 0;
  2025. func = num + a;
  2026. diff = func - num;
  2027. func(42);
  2028. (func + diff)(42);
  2029. (num + a)(43);
  2030. }
  2031. void lloptest(long long a, long long b)
  2032. {
  2033. unsigned long long ua, ub;
  2034. ua = a;
  2035. ub = b;
  2036. /* arith */
  2037. printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
  2038. a + b,
  2039. a - b,
  2040. a * b);
  2041. if (b != 0) {
  2042. printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
  2043. a / b,
  2044. a % b);
  2045. }
  2046. /* binary */
  2047. printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
  2048. a & b,
  2049. a | b,
  2050. a ^ b);
  2051. /* tests */
  2052. printf("test: %d %d %d %d %d %d\n",
  2053. a == b,
  2054. a != b,
  2055. a < b,
  2056. a > b,
  2057. a >= b,
  2058. a <= b);
  2059. printf("utest: %d %d %d %d %d %d\n",
  2060. ua == ub,
  2061. ua != ub,
  2062. ua < ub,
  2063. ua > ub,
  2064. ua >= ub,
  2065. ua <= ub);
  2066. /* arith2 */
  2067. a++;
  2068. b++;
  2069. printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
  2070. printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
  2071. printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
  2072. printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
  2073. b = ub = 0;
  2074. printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
  2075. }
  2076. void llshift(long long a, int b)
  2077. {
  2078. printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
  2079. (unsigned long long)a >> b,
  2080. a >> b,
  2081. a << b);
  2082. printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
  2083. (unsigned long long)a >> 3,
  2084. a >> 3,
  2085. a << 3);
  2086. printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
  2087. (unsigned long long)a >> 35,
  2088. a >> 35,
  2089. a << 35);
  2090. }
  2091. void llfloat(void)
  2092. {
  2093. float fa;
  2094. double da;
  2095. LONG_DOUBLE lda;
  2096. long long la, lb, lc;
  2097. unsigned long long ula, ulb, ulc;
  2098. la = 0x12345678;
  2099. ula = 0x72345678;
  2100. la = (la << 20) | 0x12345;
  2101. ula = ula << 33;
  2102. printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
  2103. fa = la;
  2104. da = la;
  2105. lda = la;
  2106. printf("lltof: %f %f %Lf\n", fa, da, lda);
  2107. la = fa;
  2108. lb = da;
  2109. lc = lda;
  2110. printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
  2111. fa = ula;
  2112. da = ula;
  2113. lda = ula;
  2114. printf("ulltof: %f %f %Lf\n", fa, da, lda);
  2115. ula = fa;
  2116. ulb = da;
  2117. ulc = lda;
  2118. printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
  2119. }
  2120. long long llfunc1(int a)
  2121. {
  2122. return a * 2;
  2123. }
  2124. struct S {
  2125. int id;
  2126. char item;
  2127. };
  2128. long long int value(struct S *v)
  2129. {
  2130. return ((long long int)v->item);
  2131. }
  2132. long long llfunc2(long long x, long long y, int z)
  2133. {
  2134. return x * y * z;
  2135. }
  2136. void longlong_test(void)
  2137. {
  2138. long long a, b, c;
  2139. int ia;
  2140. unsigned int ua;
  2141. printf("longlong_test:\n");
  2142. printf("sizeof(long long) = %d\n", sizeof(long long));
  2143. ia = -1;
  2144. ua = -2;
  2145. a = ia;
  2146. b = ua;
  2147. printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
  2148. printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
  2149. (long long)1,
  2150. (long long)-2,
  2151. 1LL,
  2152. 0x1234567812345679);
  2153. a = llfunc1(-3);
  2154. printf(LONG_LONG_FORMAT "\n", a);
  2155. lloptest(1000, 23);
  2156. lloptest(0xff, 0x1234);
  2157. b = 0x72345678 << 10;
  2158. lloptest(-3, b);
  2159. llshift(0x123, 5);
  2160. llshift(-23, 5);
  2161. b = 0x72345678LL << 10;
  2162. llshift(b, 47);
  2163. llfloat();
  2164. #if 1
  2165. b = 0x12345678;
  2166. a = -1;
  2167. c = a + b;
  2168. printf("%Lx\n", c);
  2169. #endif
  2170. /* long long reg spill test */
  2171. {
  2172. struct S a;
  2173. a.item = 3;
  2174. printf("%lld\n", value(&a));
  2175. }
  2176. lloptest(0x80000000, 0);
  2177. {
  2178. long long *p, v, **pp;
  2179. v = 1;
  2180. p = &v;
  2181. p[0]++;
  2182. printf("another long long spill test : %lld\n", *p);
  2183. pp = &p;
  2184. v = llfunc2(**pp, **pp, ia);
  2185. printf("a long long function (arm-)reg-args test : %lld\n", v);
  2186. }
  2187. a = 68719476720LL;
  2188. b = 4294967295LL;
  2189. printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
  2190. printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
  2191. /* long long pointer deref in argument passing test */
  2192. a = 0x123;
  2193. long long *p = &a;
  2194. llshift(*p, 5);
  2195. }
  2196. void manyarg_test(void)
  2197. {
  2198. LONG_DOUBLE ld = 1234567891234LL;
  2199. printf("manyarg_test:\n");
  2200. printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
  2201. 1, 2, 3, 4, 5, 6, 7, 8,
  2202. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
  2203. printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2204. LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
  2205. 1, 2, 3, 4, 5, 6, 7, 8,
  2206. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2207. 1234567891234LL, 987654321986LL,
  2208. 42.0, 43.0);
  2209. printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2210. LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
  2211. ld, 1, 2, 3, 4, 5, 6, 7, 8,
  2212. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2213. 1234567891234LL, 987654321986LL,
  2214. 42.0, 43.0);
  2215. printf("%d %d %d %d %d %d %d %d %Lf\n",
  2216. 1, 2, 3, 4, 5, 6, 7, 8, ld);
  2217. printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2218. LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
  2219. 1, 2, 3, 4, 5, 6, 7, 8,
  2220. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2221. 1234567891234LL, 987654321986LL,
  2222. 42.0, 43.0, ld);
  2223. printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2224. "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
  2225. 1, 2, 3, 4, 5, 6, 7, 8,
  2226. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2227. ld, 1234567891234LL, 987654321986LL,
  2228. 42.0, 43.0, ld);
  2229. }
  2230. void vprintf1(const char *fmt, ...)
  2231. {
  2232. va_list ap, aq;
  2233. const char *p;
  2234. int c, i;
  2235. double d;
  2236. long long ll;
  2237. LONG_DOUBLE ld;
  2238. va_start(aq, fmt);
  2239. va_copy(ap, aq);
  2240. p = fmt;
  2241. for(;;) {
  2242. c = *p;
  2243. if (c == '\0')
  2244. break;
  2245. p++;
  2246. if (c == '%') {
  2247. c = *p;
  2248. switch(c) {
  2249. case '\0':
  2250. goto the_end;
  2251. case 'd':
  2252. i = va_arg(ap, int);
  2253. printf("%d", i);
  2254. break;
  2255. case 'f':
  2256. d = va_arg(ap, double);
  2257. printf("%f", d);
  2258. break;
  2259. case 'l':
  2260. ll = va_arg(ap, long long);
  2261. printf(LONG_LONG_FORMAT, ll);
  2262. break;
  2263. case 'F':
  2264. ld = va_arg(ap, LONG_DOUBLE);
  2265. printf("%Lf", ld);
  2266. break;
  2267. }
  2268. p++;
  2269. } else {
  2270. putchar(c);
  2271. }
  2272. }
  2273. the_end:
  2274. va_end(aq);
  2275. va_end(ap);
  2276. }
  2277. struct myspace {
  2278. short int profile;
  2279. };
  2280. void stdarg_for_struct(struct myspace bob, ...)
  2281. {
  2282. struct myspace george, bill;
  2283. va_list ap;
  2284. short int validate;
  2285. va_start(ap, bob);
  2286. bill = va_arg(ap, struct myspace);
  2287. george = va_arg(ap, struct myspace);
  2288. validate = va_arg(ap, int);
  2289. printf("stdarg_for_struct: %d %d %d %d\n",
  2290. bob.profile, bill.profile, george.profile, validate);
  2291. va_end(ap);
  2292. }
  2293. void stdarg_for_libc(const char *fmt, ...)
  2294. {
  2295. va_list args;
  2296. va_start(args, fmt);
  2297. vprintf(fmt, args);
  2298. va_end(args);
  2299. }
  2300. void stdarg_test(void)
  2301. {
  2302. LONG_DOUBLE ld = 1234567891234LL;
  2303. struct myspace bob;
  2304. vprintf1("%d %d %d\n", 1, 2, 3);
  2305. vprintf1("%f %d %f\n", 1.0, 2, 3.0);
  2306. vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
  2307. vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
  2308. vprintf1("%d %f %l %F %d %f %l %F\n",
  2309. 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
  2310. vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
  2311. 1, 2, 3, 4, 5, 6, 7, 8,
  2312. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
  2313. vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
  2314. 1, 2, 3, 4, 5, 6, 7, 8,
  2315. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
  2316. vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2317. "%l %l %f %f\n",
  2318. 1, 2, 3, 4, 5, 6, 7, 8,
  2319. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2320. 1234567891234LL, 987654321986LL,
  2321. 42.0, 43.0);
  2322. vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2323. "%l %l %f %f\n",
  2324. ld, 1, 2, 3, 4, 5, 6, 7, 8,
  2325. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2326. 1234567891234LL, 987654321986LL,
  2327. 42.0, 43.0);
  2328. vprintf1("%d %d %d %d %d %d %d %d %F\n",
  2329. 1, 2, 3, 4, 5, 6, 7, 8, ld);
  2330. vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2331. "%l %l %f %f %F\n",
  2332. 1, 2, 3, 4, 5, 6, 7, 8,
  2333. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2334. 1234567891234LL, 987654321986LL,
  2335. 42.0, 43.0, ld);
  2336. vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2337. "%F %l %l %f %f %F\n",
  2338. 1, 2, 3, 4, 5, 6, 7, 8,
  2339. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2340. ld, 1234567891234LL, 987654321986LL,
  2341. 42.0, 43.0, ld);
  2342. bob.profile = 42;
  2343. stdarg_for_struct(bob, bob, bob, bob.profile);
  2344. stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
  2345. }
  2346. void whitespace_test(void)
  2347. {
  2348. char *str;
  2349. #if 1
  2350. pri\
  2351. ntf("whitspace:\n");
  2352. #endif
  2353. pf("N=%d\n", 2);
  2354. #ifdef CORRECT_CR_HANDLING
  2355. pri\
  2356. ntf("aaa=%d\n", 3);
  2357. #endif
  2358. pri\
  2359. \
  2360. ntf("min=%d\n", 4);
  2361. #ifdef ACCEPT_CR_IN_STRINGS
  2362. printf("len1=%d\n", strlen("
  2363. "));
  2364. #ifdef CORRECT_CR_HANDLING
  2365. str = "
  2366. ";
  2367. printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
  2368. #endif
  2369. printf("len1=%d\n", strlen(" a
  2370. "));
  2371. #endif /* ACCEPT_CR_IN_STRINGS */
  2372. }
  2373. int reltab[3] = { 1, 2, 3 };
  2374. int *rel1 = &reltab[1];
  2375. int *rel2 = &reltab[2];
  2376. #ifdef _WIN64
  2377. void relocation_test(void) {}
  2378. #else
  2379. void getmyaddress(void)
  2380. {
  2381. printf("in getmyaddress\n");
  2382. }
  2383. #ifdef __LP64__
  2384. long __pa_symbol(void)
  2385. {
  2386. /* This 64bit constant was handled incorrectly, it was used as addend
  2387. (which can hold 64bit just fine) in connection with a symbol,
  2388. and TCC generates wrong code for that (displacements are 32bit only).
  2389. This effectively is "+ 0x80000000", and if addresses of globals
  2390. are below 2GB the result should be a number without high 32 bits set. */
  2391. return ((long)(((unsigned long)(&rel1))) - (0xffffffff80000000UL));
  2392. }
  2393. #endif
  2394. unsigned long theaddress = (unsigned long)getmyaddress;
  2395. void relocation_test(void)
  2396. {
  2397. void (*fptr)(void) = (void (*)(void))theaddress;
  2398. printf("*rel1=%d\n", *rel1);
  2399. printf("*rel2=%d\n", *rel2);
  2400. fptr();
  2401. #ifdef __LP64__
  2402. printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
  2403. #endif
  2404. }
  2405. #endif
  2406. void old_style_f(a,b,c)
  2407. int a, b;
  2408. double c;
  2409. {
  2410. printf("a=%d b=%d b=%f\n", a, b, c);
  2411. }
  2412. void decl_func1(int cmpfn())
  2413. {
  2414. printf("cmpfn=%lx\n", (long)cmpfn);
  2415. }
  2416. void decl_func2(cmpfn)
  2417. int cmpfn();
  2418. {
  2419. printf("cmpfn=%lx\n", (long)cmpfn);
  2420. }
  2421. void old_style_function(void)
  2422. {
  2423. old_style_f((void *)1, 2, 3.0);
  2424. decl_func1(NULL);
  2425. decl_func2(NULL);
  2426. }
  2427. void alloca_test()
  2428. {
  2429. #if defined __i386__ || defined __x86_64__ || defined __arm__
  2430. char *p = alloca(16);
  2431. strcpy(p,"123456789012345");
  2432. printf("alloca: p is %s\n", p);
  2433. char *demo = "This is only a test.\n";
  2434. /* Test alloca embedded in a larger expression */
  2435. printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
  2436. #endif
  2437. }
  2438. void *bounds_checking_is_enabled()
  2439. {
  2440. char ca[10], *cp = ca-1;
  2441. return (ca != cp + 1) ? cp : NULL;
  2442. }
  2443. typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
  2444. void c99_vla_test(int size1, int size2)
  2445. {
  2446. #if defined __i386__ || defined __x86_64__
  2447. int size = size1 * size2;
  2448. int tab1[size][2], tab2[10][2];
  2449. void *tab1_ptr, *tab2_ptr, *bad_ptr;
  2450. /* "size" should have been 'captured' at tab1 declaration,
  2451. so modifying it should have no effect on VLA behaviour. */
  2452. size = size-1;
  2453. printf("Test C99 VLA 1 (sizeof): ");
  2454. printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
  2455. tab1_ptr = tab1;
  2456. tab2_ptr = tab2;
  2457. printf("Test C99 VLA 2 (ptrs subtract): ");
  2458. printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
  2459. printf("Test C99 VLA 3 (ptr add): ");
  2460. printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
  2461. printf("Test C99 VLA 4 (ptr access): ");
  2462. tab1[size1][1] = 42;
  2463. printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
  2464. printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
  2465. if (bad_ptr = bounds_checking_is_enabled()) {
  2466. int *t1 = &tab1[size1 * size2 - 1][3];
  2467. int *t2 = &tab2[9][3];
  2468. printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
  2469. printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
  2470. char*c1 = 1 + sizeof(tab1) + (char*)tab1;
  2471. char*c2 = 1 + sizeof(tab2) + (char*)tab2;
  2472. printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
  2473. printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
  2474. int *i1 = tab1[-1];
  2475. int *i2 = tab2[-1];
  2476. printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
  2477. printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
  2478. int *x1 = tab1[size1 * size2 + 1];
  2479. int *x2 = tab2[10 + 1];
  2480. printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
  2481. printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
  2482. } else {
  2483. printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
  2484. }
  2485. printf("\n");
  2486. #endif
  2487. }
  2488. #ifndef __TINYC__
  2489. typedef __SIZE_TYPE__ uintptr_t;
  2490. #endif
  2491. void sizeof_test(void)
  2492. {
  2493. int a;
  2494. int **ptr;
  2495. printf("sizeof(int) = %d\n", sizeof(int));
  2496. printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
  2497. printf("sizeof(long) = %d\n", sizeof(long));
  2498. printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
  2499. printf("sizeof(short) = %d\n", sizeof(short));
  2500. printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
  2501. printf("sizeof(char) = %d\n", sizeof(char));
  2502. printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
  2503. printf("sizeof(func) = %d\n", sizeof sizeof_test());
  2504. a = 1;
  2505. printf("sizeof(a++) = %d\n", sizeof a++);
  2506. printf("a=%d\n", a);
  2507. ptr = NULL;
  2508. printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
  2509. /* The type of sizeof should be as large as a pointer, actually
  2510. it should be size_t. */
  2511. printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
  2512. uintptr_t t = 1;
  2513. uintptr_t t2;
  2514. /* Effectively <<32, but defined also on 32bit machines. */
  2515. t <<= 16;
  2516. t <<= 16;
  2517. t++;
  2518. /* This checks that sizeof really can be used to manipulate
  2519. uintptr_t objects, without truncation. */
  2520. t2 = t & -sizeof(uintptr_t);
  2521. printf ("%lu %lu\n", t, t2);
  2522. /* some alignof tests */
  2523. printf("__alignof__(int) = %d\n", __alignof__(int));
  2524. printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
  2525. printf("__alignof__(short) = %d\n", __alignof__(short));
  2526. printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
  2527. printf("__alignof__(char) = %d\n", __alignof__(char));
  2528. printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
  2529. printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
  2530. /* sizes of VLAs need to be evaluated even inside sizeof: */
  2531. a = 2;
  2532. printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a]));
  2533. /* And checking if sizeof compound literal works. Parenthesized: */
  2534. printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
  2535. sizeof( (struct {int i; int j;}){4,5} ));
  2536. /* And as direct sizeof argument (as unary expression): */
  2537. printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
  2538. sizeof (struct {short i; short j;}){4,5} );
  2539. /* sizeof(x && y) should be sizeof(int), even if constant
  2540. evaluating is possible. */
  2541. printf("sizeof(t && 0) = %d\n", sizeof(t && 0));
  2542. printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
  2543. printf("sizeof(t || 1) = %d\n", sizeof(t || 1));
  2544. printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
  2545. }
  2546. void typeof_test(void)
  2547. {
  2548. double a;
  2549. typeof(a) b;
  2550. typeof(float) c;
  2551. a = 1.5;
  2552. b = 2.5;
  2553. c = 3.5;
  2554. printf("a=%f b=%f c=%f\n", a, b, c);
  2555. }
  2556. struct hlist_node;
  2557. struct hlist_head {
  2558. struct hlist_node *first, *last;
  2559. };
  2560. void consume_ulong (unsigned long i)
  2561. {
  2562. i = 0;
  2563. }
  2564. void statement_expr_test(void)
  2565. {
  2566. int a, i;
  2567. /* Basic stmt expr test */
  2568. a = 0;
  2569. for(i=0;i<10;i++) {
  2570. a += 1 +
  2571. ( { int b, j;
  2572. b = 0;
  2573. for(j=0;j<5;j++)
  2574. b += j; b;
  2575. } );
  2576. }
  2577. printf("a=%d\n", a);
  2578. /* Test that symbols aren't freed prematurely.
  2579. With SYM_DEBUG valgrind will show a read from a freed
  2580. symbol, and tcc will show an (invalid) warning on the initialization
  2581. of 'ptr' below, if symbols are popped after the stmt expr. */
  2582. void *v = (void*)39;
  2583. typeof(({
  2584. (struct hlist_node *)v;
  2585. })) x;
  2586. typeof (x)
  2587. ptr = (struct hlist_node *)v;
  2588. /* This part used to segfault when symbols were popped prematurely.
  2589. The symbols for the static local would be overwritten with
  2590. helper symbols from the pre-processor expansions in between. */
  2591. #define some_attr __attribute__((aligned(1)))
  2592. #define tps(str) ({ \
  2593. static const char *t some_attr = str; \
  2594. t; \
  2595. })
  2596. printf ("stmtexpr: %s %s\n",
  2597. tps("somerandomlongstring"),
  2598. tps("anotherlongstring"));
  2599. /* Test that the three decls of 't' don't interact. */
  2600. int t = 40;
  2601. int b = ({ int t = 41; t; });
  2602. int c = ({ int t = 42; t; });
  2603. /* Test that aggregate return values work. */
  2604. struct hlist_head h
  2605. = ({
  2606. typedef struct hlist_head T;
  2607. long pre = 48;
  2608. T t = { (void*)43, (void*)44 };
  2609. long post = 49;
  2610. t;
  2611. });
  2612. printf ("stmtexpr: %d %d %d\n", t, b, c);
  2613. printf ("stmtexpr: %ld %ld\n", (long)h.first, (long)h.last);
  2614. /* Test that we can give out addresses of local labels. */
  2615. consume_ulong(({ __label__ __here; __here: (unsigned long)&&__here; }));
  2616. }
  2617. void local_label_test(void)
  2618. {
  2619. int a;
  2620. goto l1;
  2621. l2:
  2622. a = 1 + ({
  2623. __label__ l1, l2, l3, l4;
  2624. goto l1;
  2625. l4:
  2626. printf("aa1\n");
  2627. goto l3;
  2628. l2:
  2629. printf("aa3\n");
  2630. goto l4;
  2631. l1:
  2632. printf("aa2\n");
  2633. goto l2;
  2634. l3:;
  2635. 1;
  2636. });
  2637. printf("a=%d\n", a);
  2638. return;
  2639. l4:
  2640. printf("bb1\n");
  2641. goto l2;
  2642. l1:
  2643. printf("bb2\n");
  2644. goto l4;
  2645. }
  2646. /* inline assembler test */
  2647. #if defined(__i386__) || defined(__x86_64__)
  2648. /* from linux kernel */
  2649. static char * strncat1(char * dest,const char * src,size_t count)
  2650. {
  2651. long d0, d1, d2, d3;
  2652. __asm__ __volatile__(
  2653. "repne\n\t"
  2654. "scasb\n\t"
  2655. "dec %1\n\t"
  2656. "mov %8,%3\n"
  2657. "1:\tdec %3\n\t"
  2658. "js 2f\n\t"
  2659. "lodsb\n\t"
  2660. "stosb\n\t"
  2661. "testb %%al,%%al\n\t"
  2662. "jne 1b\n"
  2663. "2:\txor %2,%2\n\t"
  2664. "stosb"
  2665. : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
  2666. : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
  2667. : "memory");
  2668. return dest;
  2669. }
  2670. static char * strncat2(char * dest,const char * src,size_t count)
  2671. {
  2672. long d0, d1, d2, d3;
  2673. __asm__ __volatile__(
  2674. "repne scasb\n\t" /* one-line repne prefix + string op */
  2675. "dec %1\n\t"
  2676. "mov %8,%3\n"
  2677. "1:\tdec %3\n\t"
  2678. "js 2f\n\t"
  2679. "lodsb\n\t"
  2680. "stosb\n\t"
  2681. "testb %%al,%%al\n\t"
  2682. "jne 1b\n"
  2683. "2:\txor %2,%2\n\t"
  2684. "stosb"
  2685. : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
  2686. : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
  2687. : "memory");
  2688. return dest;
  2689. }
  2690. static inline void * memcpy1(void * to, const void * from, size_t n)
  2691. {
  2692. long d0, d1, d2;
  2693. __asm__ __volatile__(
  2694. "rep ; movsl\n\t"
  2695. "testb $2,%b4\n\t"
  2696. "je 1f\n\t"
  2697. "movsw\n"
  2698. "1:\ttestb $1,%b4\n\t"
  2699. "je 2f\n\t"
  2700. "movsb\n"
  2701. "2:"
  2702. : "=&c" (d0), "=&D" (d1), "=&S" (d2)
  2703. :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
  2704. : "memory");
  2705. return (to);
  2706. }
  2707. static inline void * memcpy2(void * to, const void * from, size_t n)
  2708. {
  2709. long d0, d1, d2;
  2710. __asm__ __volatile__(
  2711. "rep movsl\n\t" /* one-line rep prefix + string op */
  2712. "testb $2,%b4\n\t"
  2713. "je 1f\n\t"
  2714. "movsw\n"
  2715. "1:\ttestb $1,%b4\n\t"
  2716. "je 2f\n\t"
  2717. "movsb\n"
  2718. "2:"
  2719. : "=&c" (d0), "=&D" (d1), "=&S" (d2)
  2720. :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
  2721. : "memory");
  2722. return (to);
  2723. }
  2724. static __inline__ void sigaddset1(unsigned int *set, int _sig)
  2725. {
  2726. __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
  2727. }
  2728. static __inline__ void sigdelset1(unsigned int *set, int _sig)
  2729. {
  2730. asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc", "flags");
  2731. }
  2732. static __inline__ __const__ unsigned int swab32(unsigned int x)
  2733. {
  2734. __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
  2735. "rorl $16,%0\n\t" /* swap words */
  2736. "xchgb %b0,%h0" /* swap higher bytes */
  2737. :"=" "q" (x)
  2738. : "0" (x));
  2739. return x;
  2740. }
  2741. static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
  2742. {
  2743. unsigned long long res;
  2744. #ifdef __x86_64__
  2745. /* Using the A constraint is wrong (it means rdx:rax, which is too large)
  2746. but still test the 32bit->64bit mull. */
  2747. unsigned int resh, resl;
  2748. __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
  2749. res = ((unsigned long long)resh << 32) | resl;
  2750. #else
  2751. __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
  2752. #endif
  2753. return res;
  2754. }
  2755. static __inline__ unsigned long long inc64(unsigned long long a)
  2756. {
  2757. unsigned long long res;
  2758. #ifdef __x86_64__
  2759. /* Using the A constraint is wrong, and increments are tested
  2760. elsewhere. */
  2761. res = a + 1;
  2762. #else
  2763. __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
  2764. #endif
  2765. return res;
  2766. }
  2767. struct struct123 {
  2768. int a;
  2769. int b;
  2770. };
  2771. struct struct1231 {
  2772. unsigned long addr;
  2773. };
  2774. unsigned long mconstraint_test(struct struct1231 *r)
  2775. {
  2776. unsigned long ret;
  2777. unsigned int a[2];
  2778. a[0] = 0;
  2779. __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
  2780. : "=&r" (ret), "=m" (a)
  2781. : "m" (*(struct struct123 *)r->addr));
  2782. return ret + a[0];
  2783. }
  2784. #ifdef __x86_64__
  2785. int fls64(unsigned long long x)
  2786. {
  2787. int bitpos = -1;
  2788. asm("bsrq %1,%q0"
  2789. : "+r" (bitpos)
  2790. : "rm" (x));
  2791. return bitpos + 1;
  2792. }
  2793. #endif
  2794. void other_constraints_test(void)
  2795. {
  2796. unsigned long ret;
  2797. int var;
  2798. #ifndef _WIN64
  2799. __asm__ volatile ("mov %P1,%0" : "=r" (ret) : "p" (&var));
  2800. printf ("oc1: %d\n", ret == (unsigned long)&var);
  2801. #endif
  2802. }
  2803. #ifndef _WIN32
  2804. /* Test global asm blocks playing with aliases. */
  2805. void base_func(void)
  2806. {
  2807. printf ("asmc: base\n");
  2808. }
  2809. extern void override_func1 (void);
  2810. extern void override_func2 (void);
  2811. asm(".weak override_func1\n.set override_func1, base_func");
  2812. asm(".set override_func1, base_func");
  2813. asm(".set override_func2, base_func");
  2814. void override_func2 (void)
  2815. {
  2816. printf ("asmc: override2\n");
  2817. }
  2818. /* This checks a construct used by the linux kernel to encode
  2819. references to strings by PC relative references. */
  2820. extern int bug_table[] __attribute__((section("__bug_table")));
  2821. char * get_asm_string (void)
  2822. {
  2823. extern int some_symbol;
  2824. asm volatile (".globl some_symbol\n"
  2825. "jmp .+6\n"
  2826. "1:\n"
  2827. "some_symbol: .long 0\n"
  2828. ".pushsection __bug_table, \"a\"\n"
  2829. ".globl bug_table\n"
  2830. "bug_table:\n"
  2831. /* The first entry (1b-2b) is unused in this test,
  2832. but we include it to check if cross-section
  2833. PC-relative references work. */
  2834. "2:\t.long 1b - 2b, %c0 - 2b\n"
  2835. ".popsection\n" : : "i" ("A string"));
  2836. char * str = ((char*)bug_table) + bug_table[1];
  2837. return str;
  2838. }
  2839. /* This checks another constructs with local labels. */
  2840. extern unsigned char alld_stuff[];
  2841. asm(".data\n"
  2842. ".byte 41\n"
  2843. "alld_stuff:\n"
  2844. "661:\n"
  2845. ".byte 42\n"
  2846. "662:\n"
  2847. ".pushsection .data.ignore\n"
  2848. ".long 661b - .\n" /* This reference to 661 generates an external sym
  2849. which shouldn't somehow overwrite the offset that's
  2850. already determined for it. */
  2851. ".popsection\n"
  2852. ".byte 662b - 661b\n" /* So that this value is undeniably 1. */);
  2853. void asm_local_label_diff (void)
  2854. {
  2855. printf ("asm_local_label_diff: %d %d\n", alld_stuff[0], alld_stuff[1]);
  2856. }
  2857. /* This checks that static local variables are available from assembler. */
  2858. void asm_local_statics (void)
  2859. {
  2860. static int localint = 41;
  2861. asm("incl %0" : "+m" (localint));
  2862. printf ("asm_local_statics: %d\n", localint);
  2863. }
  2864. #endif
  2865. static
  2866. unsigned int set;
  2867. void fancy_copy (unsigned *in, unsigned *out)
  2868. {
  2869. asm volatile ("" : "=r" (*out) : "0" (*in));
  2870. }
  2871. void fancy_copy2 (unsigned *in, unsigned *out)
  2872. {
  2873. asm volatile ("mov %0,(%1)" : : "r" (*in), "r" (out) : "memory");
  2874. }
  2875. #if defined __x86_64__ && !defined _WIN64
  2876. void clobber_r12(void)
  2877. {
  2878. asm volatile("mov $1, %%r12" ::: "r12");
  2879. }
  2880. #endif
  2881. void test_high_clobbers(void)
  2882. {
  2883. #if defined __x86_64__ && !defined _WIN64
  2884. register long val asm("r12");
  2885. long val2;
  2886. /* This tests if asm clobbers correctly save/restore callee saved
  2887. registers if they are clobbered and if it's the high 8 x86-64
  2888. registers. This is fragile for GCC as the constraints do not
  2889. correctly capture the data flow, but good enough for us. */
  2890. asm volatile("mov $0x4542, %%r12" : "=r" (val):: "memory");
  2891. clobber_r12();
  2892. asm volatile("mov %%r12, %0" : "=r" (val2) : "r" (val): "memory");
  2893. printf("asmhc: 0x%x\n", val2);
  2894. #endif
  2895. }
  2896. static long cpu_number;
  2897. void trace_console(long len, long len2)
  2898. {
  2899. #ifdef __x86_64__
  2900. /* This generated invalid code when the emission of the switch
  2901. table isn't disabled. The asms are necessary to show the bug,
  2902. normal statements don't work (they need to generate some code
  2903. even under nocode_wanted, which normal statements don't do,
  2904. but asms do). Also at least these number of cases is necessary
  2905. to generate enough "random" bytes. They ultimately are enough
  2906. to create invalid instruction patterns to which the first
  2907. skip-to-decision-table jump jumps. If decision table emission
  2908. is disabled all of this is no problem.
  2909. It also is necessary that the switches are in a statement expression
  2910. (which has the property of not being enterable from outside. no
  2911. matter what). */
  2912. if (0
  2913. &&
  2914. ({
  2915. long pscr_ret__;
  2916. switch(len) {
  2917. case 4:
  2918. {
  2919. long pfo_ret__;
  2920. switch (len2) {
  2921. case 8: printf("bla"); pfo_ret__ = 42; break;
  2922. }
  2923. pscr_ret__ = pfo_ret__;
  2924. }
  2925. break;
  2926. case 8:
  2927. {
  2928. long pfo_ret__;
  2929. switch (len2) {
  2930. case 1:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
  2931. case 2:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
  2932. case 4:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
  2933. case 8:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
  2934. default: printf("impossible\n");
  2935. }
  2936. pscr_ret__ = pfo_ret__;
  2937. };
  2938. break;
  2939. }
  2940. pscr_ret__;
  2941. }))
  2942. {
  2943. printf("huh?\n");
  2944. }
  2945. #endif
  2946. }
  2947. void test_asm_dead_code(void)
  2948. {
  2949. long rdi;
  2950. /* Try to make sure that xdi contains a zero, and hence will
  2951. lead to a segfault if the next asm is evaluated without
  2952. arguments being set up. */
  2953. asm volatile ("" : "=D" (rdi) : "0" (0));
  2954. (void)sizeof (({
  2955. int var;
  2956. /* This shouldn't trigger a segfault, either the argument
  2957. registers need to be set up and the asm emitted despite
  2958. this being in an unevaluated context, or both the argument
  2959. setup _and_ the asm emission need to be suppressed. The latter
  2960. is better. Disabling asm code gen when suppression is on
  2961. also fixes the above trace_console bug, but that came earlier
  2962. than asm suppression. */
  2963. asm volatile ("movl $0,(%0)" : : "D" (&var) : "memory");
  2964. var;
  2965. }));
  2966. }
  2967. void asm_test(void)
  2968. {
  2969. char buf[128];
  2970. unsigned int val, val2;
  2971. struct struct123 s1;
  2972. struct struct1231 s2 = { (unsigned long)&s1 };
  2973. /* Hide the outer base_func, but check later that the inline
  2974. asm block gets the outer one. */
  2975. int base_func = 42;
  2976. void override_func3 (void);
  2977. unsigned long asmret;
  2978. #ifdef BOOL_ISOC99
  2979. _Bool somebool;
  2980. #endif
  2981. register int regvar asm("%esi");
  2982. printf("inline asm:\n");
  2983. // parse 0x1E-1 as 3 tokens in asm mode
  2984. asm volatile ("mov $0x1E-1,%eax");
  2985. /* test the no operand case */
  2986. asm volatile ("xorl %eax, %eax");
  2987. memcpy1(buf, "hello", 6);
  2988. strncat1(buf, " worldXXXXX", 3);
  2989. printf("%s\n", buf);
  2990. memcpy2(buf, "hello", 6);
  2991. strncat2(buf, " worldXXXXX", 3);
  2992. printf("%s\n", buf);
  2993. /* 'A' constraint test */
  2994. printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
  2995. printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
  2996. s1.a = 42;
  2997. s1.b = 43;
  2998. printf("mconstraint: %d", mconstraint_test(&s2));
  2999. printf(" %d %d\n", s1.a, s1.b);
  3000. other_constraints_test();
  3001. set = 0xff;
  3002. sigdelset1(&set, 2);
  3003. sigaddset1(&set, 16);
  3004. /* NOTE: we test here if C labels are correctly restored after the
  3005. asm statement */
  3006. goto label1;
  3007. label2:
  3008. __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
  3009. printf("set=0x%x\n", set);
  3010. val = 0x01020304;
  3011. printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
  3012. #ifndef _WIN32
  3013. override_func1();
  3014. override_func2();
  3015. /* The base_func ref from the following inline asm should find
  3016. the global one, not the local decl from this function. */
  3017. asm volatile(".weak override_func3\n.set override_func3, base_func");
  3018. override_func3();
  3019. printf("asmstr: %s\n", get_asm_string());
  3020. asm_local_label_diff();
  3021. asm_local_statics();
  3022. #endif
  3023. /* Check that we can also load structs of appropriate layout
  3024. into registers. */
  3025. asm volatile("" : "=r" (asmret) : "0"(s2));
  3026. if (asmret != s2.addr)
  3027. printf("asmstr: failed\n");
  3028. #ifdef BOOL_ISOC99
  3029. /* Check that the typesize correctly sets the register size to
  3030. 8 bit. */
  3031. asm volatile("cmp %1,%2; sete %0" : "=a"(somebool) : "r"(1), "r"(2));
  3032. if (!somebool)
  3033. printf("asmbool: failed\n");
  3034. #endif
  3035. val = 43;
  3036. fancy_copy (&val, &val2);
  3037. printf ("fancycpy(%d)=%d\n", val, val2);
  3038. val = 44;
  3039. fancy_copy2 (&val, &val2);
  3040. printf ("fancycpy2(%d)=%d\n", val, val2);
  3041. asm volatile ("mov $0x4243, %%esi" : "=r" (regvar));
  3042. printf ("regvar=%x\n", regvar);
  3043. test_high_clobbers();
  3044. trace_console(8, 8);
  3045. test_asm_dead_code();
  3046. return;
  3047. label1:
  3048. goto label2;
  3049. }
  3050. #else
  3051. void asm_test(void)
  3052. {
  3053. }
  3054. #endif
  3055. #define COMPAT_TYPE(type1, type2) \
  3056. {\
  3057. printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
  3058. __builtin_types_compatible_p (type1, type2));\
  3059. }
  3060. int constant_p_var;
  3061. void builtin_test(void)
  3062. {
  3063. short s;
  3064. int i;
  3065. long long ll;
  3066. #if GCC_MAJOR >= 3
  3067. COMPAT_TYPE(int, int);
  3068. COMPAT_TYPE(int, unsigned int);
  3069. COMPAT_TYPE(int, char);
  3070. COMPAT_TYPE(int, const int);
  3071. COMPAT_TYPE(int, volatile int);
  3072. COMPAT_TYPE(int *, int *);
  3073. COMPAT_TYPE(int *, void *);
  3074. COMPAT_TYPE(int *, const int *);
  3075. COMPAT_TYPE(char *, unsigned char *);
  3076. COMPAT_TYPE(char *, signed char *);
  3077. COMPAT_TYPE(char *, char *);
  3078. /* space is needed because tcc preprocessor introduces a space between each token */
  3079. COMPAT_TYPE(char * *, void *);
  3080. #endif
  3081. printf("res = %d\n", __builtin_constant_p(1));
  3082. printf("res = %d\n", __builtin_constant_p(1 + 2));
  3083. printf("res = %d\n", __builtin_constant_p(&constant_p_var));
  3084. printf("res = %d\n", __builtin_constant_p(constant_p_var));
  3085. printf("res = %d\n", __builtin_constant_p(100000 / constant_p_var));
  3086. s = 1;
  3087. ll = 2;
  3088. i = __builtin_choose_expr (1 != 0, ll, s);
  3089. printf("bce: %d\n", i);
  3090. i = __builtin_choose_expr (1 != 1, ll, s);
  3091. printf("bce: %d\n", i);
  3092. i = sizeof (__builtin_choose_expr (1, ll, s));
  3093. printf("bce: %d\n", i);
  3094. i = sizeof (__builtin_choose_expr (0, ll, s));
  3095. printf("bce: %d\n", i);
  3096. //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
  3097. }
  3098. #ifndef _WIN32
  3099. extern int __attribute__((weak)) weak_f1(void);
  3100. extern int __attribute__((weak)) weak_f2(void);
  3101. extern int weak_f3(void);
  3102. extern int __attribute__((weak)) weak_v1;
  3103. extern int __attribute__((weak)) weak_v2;
  3104. extern int weak_v3;
  3105. extern int (*weak_fpa)() __attribute__((weak));
  3106. extern int __attribute__((weak)) (*weak_fpb)();
  3107. extern __attribute__((weak)) int (*weak_fpc)();
  3108. extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
  3109. extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
  3110. extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
  3111. extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
  3112. extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
  3113. extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
  3114. static const size_t dummy = 0;
  3115. extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
  3116. extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
  3117. extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
  3118. int some_lib_func(void);
  3119. int dummy_impl_of_slf(void) { return 444; }
  3120. int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
  3121. int weak_toolate() __attribute__((weak));
  3122. int weak_toolate() { return 0; }
  3123. void __attribute__((weak)) weak_test(void)
  3124. {
  3125. printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
  3126. printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
  3127. printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
  3128. printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
  3129. printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
  3130. printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
  3131. printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
  3132. printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
  3133. printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
  3134. printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
  3135. printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
  3136. printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
  3137. printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
  3138. printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
  3139. printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
  3140. printf("some_lib_func=%d\n", &some_lib_func ? some_lib_func() : 0);
  3141. }
  3142. int __attribute__((weak)) weak_f2() { return 222; }
  3143. int __attribute__((weak)) weak_f3() { return 333; }
  3144. int __attribute__((weak)) weak_v2 = 222;
  3145. int __attribute__((weak)) weak_v3 = 333;
  3146. #endif
  3147. void const_func(const int a)
  3148. {
  3149. }
  3150. void const_warn_test(void)
  3151. {
  3152. const_func(1);
  3153. }
  3154. struct condstruct {
  3155. int i;
  3156. };
  3157. int getme (struct condstruct *s, int i)
  3158. {
  3159. int i1 = (i == 0 ? 0 : s)->i;
  3160. int i2 = (i == 0 ? s : 0)->i;
  3161. int i3 = (i == 0 ? (void*)0 : s)->i;
  3162. int i4 = (i == 0 ? s : (void*)0)->i;
  3163. return i1 + i2 + i3 + i4;
  3164. }
  3165. struct global_data
  3166. {
  3167. int a[40];
  3168. int *b[40];
  3169. };
  3170. struct global_data global_data;
  3171. int global_data_getstuff (int *, int);
  3172. void global_data_callit (int i)
  3173. {
  3174. *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
  3175. }
  3176. int global_data_getstuff (int *p, int i)
  3177. {
  3178. return *p + i;
  3179. }
  3180. void global_data_test (void)
  3181. {
  3182. global_data.a[0] = 42;
  3183. global_data.b[0] = &global_data.a[0];
  3184. global_data_callit (0);
  3185. printf ("%d\n", global_data.a[0]);
  3186. }
  3187. struct cmpcmpS
  3188. {
  3189. unsigned char fill : 3;
  3190. unsigned char b1 : 1;
  3191. unsigned char b2 : 1;
  3192. unsigned char fill2 : 3;
  3193. };
  3194. int glob1, glob2, glob3;
  3195. void compare_comparisons (struct cmpcmpS *s)
  3196. {
  3197. if (s->b1 != (glob1 == glob2)
  3198. || (s->b2 != (glob1 == glob3)))
  3199. printf ("comparing comparisons broken\n");
  3200. }
  3201. void cmp_comparison_test(void)
  3202. {
  3203. struct cmpcmpS s;
  3204. s.b1 = 1;
  3205. glob1 = 42; glob2 = 42;
  3206. s.b2 = 0;
  3207. glob3 = 43;
  3208. compare_comparisons (&s);
  3209. }
  3210. int fcompare (double a, double b, int code)
  3211. {
  3212. switch (code) {
  3213. case 0: return a == b;
  3214. case 1: return a != b;
  3215. case 2: return a < b;
  3216. case 3: return a >= b;
  3217. case 4: return a > b;
  3218. case 5: return a <= b;
  3219. }
  3220. }
  3221. void math_cmp_test(void)
  3222. {
  3223. double nan = 0.0/0.0;
  3224. double one = 1.0;
  3225. double two = 2.0;
  3226. int comp = 0;
  3227. #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
  3228. /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
  3229. And it does this in various ways so that all code generation paths
  3230. are checked (generating inverted tests, or non-inverted tests, or
  3231. producing a 0/1 value without jumps (that's done in the fcompare
  3232. function). */
  3233. #define FCMP(a,b,op,iop,code) \
  3234. if (fcompare (a,b,code)) \
  3235. bug (a,b,op,iop,1); \
  3236. if (a op b) \
  3237. bug (a,b,op,iop,2); \
  3238. if (a iop b) \
  3239. ; \
  3240. else \
  3241. bug (a,b,op,iop,3); \
  3242. if ((a op b) || comp) \
  3243. bug (a,b,op,iop,4); \
  3244. if ((a iop b) || comp) \
  3245. ; \
  3246. else \
  3247. bug (a,b,op,iop,5);
  3248. /* Equality tests. */
  3249. FCMP(nan, nan, ==, !=, 0);
  3250. FCMP(one, two, ==, !=, 0);
  3251. FCMP(one, one, !=, ==, 1);
  3252. /* Non-equality is a bit special. */
  3253. if (!fcompare (nan, nan, 1))
  3254. bug (nan, nan, !=, ==, 6);
  3255. /* Relational tests on numbers. */
  3256. FCMP(two, one, <, >=, 2);
  3257. FCMP(one, two, >=, <, 3);
  3258. FCMP(one, two, >, <=, 4);
  3259. FCMP(two, one, <=, >, 5);
  3260. /* Relational tests on NaNs. Note that the inverse op here is
  3261. always !=, there's no operator in C that is equivalent to !(a < b),
  3262. when NaNs are involved, same for the other relational ops. */
  3263. FCMP(nan, nan, <, !=, 2);
  3264. FCMP(nan, nan, >=, !=, 3);
  3265. FCMP(nan, nan, >, !=, 4);
  3266. FCMP(nan, nan, <=, !=, 5);
  3267. }
  3268. double get100 () { return 100.0; }
  3269. void callsave_test(void)
  3270. {
  3271. #if defined __i386__ || defined __x86_64__ || defined __arm__
  3272. int i, s; double *d; double t;
  3273. s = sizeof (double);
  3274. printf ("callsavetest: %d\n", s);
  3275. d = alloca (sizeof(double));
  3276. d[0] = 10.0;
  3277. /* x86-64 had a bug were the next call to get100 would evict
  3278. the lvalue &d[0] as VT_LLOCAL, and the reload would be done
  3279. in int type, not pointer type. When alloca returns a pointer
  3280. with the high 32 bit set (which is likely on x86-64) the access
  3281. generates a segfault. */
  3282. i = d[0] > get100 ();
  3283. printf ("%d\n", i);
  3284. #endif
  3285. }
  3286. void bfa3(ptrdiff_t str_offset)
  3287. {
  3288. printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
  3289. }
  3290. void bfa2(ptrdiff_t str_offset)
  3291. {
  3292. printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
  3293. bfa3(str_offset);
  3294. }
  3295. void bfa1(ptrdiff_t str_offset)
  3296. {
  3297. printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
  3298. bfa2(str_offset);
  3299. }
  3300. void builtin_frame_address_test(void)
  3301. {
  3302. /* builtin_frame_address fails on ARM with gcc which make test3 fail */
  3303. #ifndef __arm__
  3304. char str[] = "__builtin_frame_address";
  3305. char *fp0 = __builtin_frame_address(0);
  3306. printf("str: %s\n", str);
  3307. bfa1(str-fp0);
  3308. #endif
  3309. }
  3310. char via_volatile (char i)
  3311. {
  3312. char volatile vi;
  3313. vi = i;
  3314. return vi;
  3315. }
  3316. struct __attribute__((__packed__)) Spacked {
  3317. char a;
  3318. short b;
  3319. int c;
  3320. };
  3321. struct Spacked spacked;
  3322. typedef struct __attribute__((__packed__)) {
  3323. char a;
  3324. short b;
  3325. int c;
  3326. } Spacked2;
  3327. Spacked2 spacked2;
  3328. typedef struct Spacked3_s {
  3329. char a;
  3330. short b;
  3331. int c;
  3332. } __attribute__((__packed__)) Spacked3;
  3333. Spacked3 spacked3;
  3334. struct gate_struct64 {
  3335. unsigned short offset_low;
  3336. unsigned short segment;
  3337. unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
  3338. unsigned short offset_middle;
  3339. unsigned offset_high;
  3340. unsigned zero1;
  3341. } __attribute__((packed));
  3342. typedef struct gate_struct64 gate_desc;
  3343. gate_desc a_gate_desc;
  3344. void attrib_test(void)
  3345. {
  3346. #ifndef _WIN32
  3347. printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
  3348. sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
  3349. printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
  3350. printf("attr: %d %d\n", sizeof(gate_desc), sizeof(a_gate_desc));
  3351. #endif
  3352. }
  3353. extern __attribute__((__unused__)) char * __attribute__((__unused__)) *
  3354. strange_attrib_placement (void);
  3355. void * __attribute__((__unused__)) get_void_ptr (void *a)
  3356. {
  3357. return a;
  3358. }
  3359. /* This part checks for a bug in TOK_GET (used for inline expansion),
  3360. where the large long long constant left the the high bits set for
  3361. the integer constant token. */
  3362. static inline
  3363. int __get_order(unsigned long long size)
  3364. {
  3365. int order;
  3366. size -= 0xffff880000000000ULL; // this const left high bits set in the token
  3367. {
  3368. struct S { int i : 1; } s; // constructed for this '1'
  3369. }
  3370. order = size;
  3371. return order;
  3372. }
  3373. /* This just forces the above inline function to be actually emitted. */
  3374. int force_get_order(unsigned long s)
  3375. {
  3376. return __get_order(s);
  3377. }