summaryrefslogtreecommitdiff
path: root/src/cmd/gc/fmt.c
blob: b5f8a834f1620e55b5f9ebfd6601303dc0f85fb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include	<u.h>
#include	<libc.h>
#include	"go.h"
#include	"opnames.h"

//
// Format conversions
//	%L int		Line numbers
//
//	%E int		etype values (aka 'Kind')
//
//	%O int		Node Opcodes
//		Flags: "%#O": print go syntax. (automatic unless fmtmode == FDbg)
//
//	%J Node*	Node details
//		Flags: "%hJ" suppresses things not relevant until walk.
//
//	%V Val*		Constant values
//
//	%S Sym*		Symbols
//		Flags: +,- #: mode (see below)
//			"%hS"	unqualified identifier in any mode
//			"%hhS"  in export mode: unqualified identifier if exported, qualified if not
//
//	%T Type*	Types
//		Flags: +,- #: mode (see below)
//			'l' definition instead of name.
//			'h' omit "func" and receiver in function types
//			'u' (only in -/Sym mode) print type identifiers wit package name instead of prefix.
//
//	%N Node*	Nodes
//		Flags: +,- #: mode (see below)
//			'h' (only in +/debug mode) suppress recursion
//			'l' (only in Error mode) print "foo (type Bar)"
//
//	%H NodeList*	NodeLists
//		Flags: those of %N
//			','  separate items with ',' instead of ';'
//
//	%Z Strlit*	String literals
//
//   In mparith1.c:
//      %B Mpint*	Big integers
//	%F Mpflt*	Big floats
//
//   %S, %T and %N obey use the following flags to set the format mode:
enum {
	FErr,	//     error mode (default)
	FDbg,	//     "%+N" debug mode
	FExp,	//     "%#N" export mode
	FTypeId,  //   "%-N" turning-types-into-symbols-mode: identical types give identical strings
};
static int fmtmode;
static int fmtpkgpfx;	// %uT stickyness
//
// E.g. for %S:	%+S %#S %-S	print an identifier properly qualified for debug/export/internal mode.
//
// The mode flags  +, - and # are sticky, meaning they persist through
// recursions of %N, %T and %S, but not the h and l flags.  The u flag is
// sticky only on %T recursions and only used in %-/Sym mode.

//
// Useful format combinations:
//
//	%+N   %+H	multiline recursive debug dump of node/nodelist
//	%+hN  %+hH	non recursive debug dump
//
//	%#N   %#T	export format
//	%#lT		type definition instead of name
//	%#hT		omit"func" and receiver in function signature
//
//	%lN		"foo (type Bar)" for error messages
//
//	%-T		type identifiers
//	%-hT		type identifiers without "func" and arg names in type signatures (methodsym)
//	%-uT		type identifiers with package name instead of prefix (typesym, dcommontype, typehash)
//


static int
setfmode(unsigned long *flags)
{
	int fm;

	fm = fmtmode;
	if(*flags & FmtSign)
		fmtmode = FDbg;
	else if(*flags & FmtSharp)
		fmtmode = FExp;
	else if(*flags & FmtLeft)
		fmtmode = FTypeId;

	*flags &= ~(FmtSharp|FmtLeft|FmtSign);
	return fm;
}

// Fmt "%L": Linenumbers
static int
Lconv(Fmt *fp)
{
	return linklinefmt(ctxt, fp);
}

static char*
goopnames[] =
{
	[OADDR]		= "&",
	[OADD]		= "+",
	[OADDSTR]	= "+",
	[OANDAND]	= "&&",
	[OANDNOT]	= "&^",
	[OAND]		= "&",
	[OAPPEND]	= "append",
	[OAS]		= "=",
	[OAS2]		= "=",
	[OBREAK]	= "break",
	[OCALL]		= "function call",	// not actual syntax
	[OCAP]		= "cap",
	[OCASE]		= "case",
	[OCLOSE]	= "close",
	[OCOMPLEX]	= "complex",
	[OCOM]		= "^",
	[OCONTINUE]	= "continue",
	[OCOPY]		= "copy",
	[ODEC]		= "--",
	[ODELETE]	= "delete",
	[ODEFER]	= "defer",
	[ODIV]		= "/",
	[OEQ]		= "==",
	[OFALL]		= "fallthrough",
	[OFOR]		= "for",
	[OGE]		= ">=",
	[OGOTO]		= "goto",
	[OGT]		= ">",
	[OIF]		= "if",
	[OIMAG]		= "imag",
	[OINC]		= "++",
	[OIND]		= "*",
	[OLEN]		= "len",
	[OLE]		= "<=",
	[OLSH]		= "<<",
	[OLT]		= "<",
	[OMAKE]		= "make",
	[OMINUS]	= "-",
	[OMOD]		= "%",
	[OMUL]		= "*",
	[ONEW]		= "new",
	[ONE]		= "!=",
	[ONOT]		= "!",
	[OOROR]		= "||",
	[OOR]		= "|",
	[OPANIC]	= "panic",
	[OPLUS]		= "+",
	[OPRINTN]	= "println",
	[OPRINT]	= "print",
	[ORANGE]	= "range",
	[OREAL]		= "real",
	[ORECV]		= "<-",
	[ORECOVER]	= "recover",
	[ORETURN]	= "return",
	[ORSH]		= ">>",
	[OSELECT]	= "select",
	[OSEND]		= "<-",
	[OSUB]		= "-",
	[OSWITCH]	= "switch",
	[OXOR]		= "^",
};

// Fmt "%O":  Node opcodes
static int
Oconv(Fmt *fp)
{
	int o;

	o = va_arg(fp->args, int);
	if((fp->flags & FmtSharp) || fmtmode != FDbg)
		if(o >= 0 && o < nelem(goopnames) && goopnames[o] != nil)
			return fmtstrcpy(fp, goopnames[o]);

	if(o >= 0 && o < nelem(opnames) && opnames[o] != nil)
		return fmtstrcpy(fp, opnames[o]);

	return fmtprint(fp, "O-%d", o);
}

static const char* classnames[] = {
	"Pxxx",
	"PEXTERN",
	"PAUTO",
	"PPARAM",
	"PPARAMOUT",
	"PPARAMREF",
	"PFUNC",
};

// Fmt "%J": Node details.
static int
Jconv(Fmt *fp)
{
	Node *n;
	char *s;
	int c;

	n = va_arg(fp->args, Node*);

	c = fp->flags&FmtShort;

	if(!c && n->ullman != 0)
		fmtprint(fp, " u(%d)", n->ullman);

	if(!c && n->addable != 0)
		fmtprint(fp, " a(%d)", n->addable);

	if(!c && n->vargen != 0)
		fmtprint(fp, " g(%d)", n->vargen);

	if(n->lineno != 0)
		fmtprint(fp, " l(%d)", n->lineno);

	if(!c && n->xoffset != BADWIDTH)
		fmtprint(fp, " x(%lld%+lld)", n->xoffset, n->stkdelta);

	if(n->class != 0) {
		s = "";
		if(n->class & PHEAP) s = ",heap";
		if((n->class & ~PHEAP) < nelem(classnames))
			fmtprint(fp, " class(%s%s)", classnames[n->class&~PHEAP], s);
		else
			fmtprint(fp, " class(%d?%s)", n->class&~PHEAP, s);
	}

	if(n->colas != 0)
		fmtprint(fp, " colas(%d)", n->colas);

	if(n->funcdepth != 0)
		fmtprint(fp, " f(%d)", n->funcdepth);

	switch(n->esc) {
	case EscUnknown:
		break;
	case EscHeap:
		fmtprint(fp, " esc(h)");
		break;
	case EscScope:
		fmtprint(fp, " esc(s)");
		break;
	case EscNone:
		fmtprint(fp, " esc(no)");
		break;
	case EscNever:
		if(!c)
			fmtprint(fp, " esc(N)");
		break;
	default:
		fmtprint(fp, " esc(%d)", n->esc);
		break;
	}

	if(n->escloopdepth)
		fmtprint(fp, " ld(%d)", n->escloopdepth);

	if(!c && n->typecheck != 0)
		fmtprint(fp, " tc(%d)", n->typecheck);

	if(!c && n->dodata != 0)
		fmtprint(fp, " dd(%d)", n->dodata);

	if(n->isddd != 0)
		fmtprint(fp, " isddd(%d)", n->isddd);

	if(n->implicit != 0)
		fmtprint(fp, " implicit(%d)", n->implicit);

	if(n->embedded != 0)
		fmtprint(fp, " embedded(%d)", n->embedded);

	if(!c && n->used != 0)
		fmtprint(fp, " used(%d)", n->used);
	return 0;
}

// Fmt "%V": Values
static int
Vconv(Fmt *fp)
{
	Val *v;
	vlong x;

	v = va_arg(fp->args, Val*);

	switch(v->ctype) {
	case CTINT:
		if((fp->flags & FmtSharp) || fmtmode == FExp)
			return fmtprint(fp, "%#B", v->u.xval);
		return fmtprint(fp, "%B", v->u.xval);
	case CTRUNE:
		x = mpgetfix(v->u.xval);
		if(' ' <= x && x < 0x80 && x != '\\' && x != '\'')
			return fmtprint(fp, "'%c'", (int)x);
		if(0 <= x && x < (1<<16))
			return fmtprint(fp, "'\\u%04ux'", (int)x);
		if(0 <= x && x <= Runemax)
			return fmtprint(fp, "'\\U%08llux'", x);
		return fmtprint(fp, "('\\x00' + %B)", v->u.xval);
	case CTFLT:
		if((fp->flags & FmtSharp) || fmtmode == FExp)
			return fmtprint(fp, "%F", v->u.fval);
		return fmtprint(fp, "%#F", v->u.fval);
	case CTCPLX:
		if((fp->flags & FmtSharp) || fmtmode == FExp)
			return fmtprint(fp, "(%F+%Fi)", &v->u.cval->real, &v->u.cval->imag);
		if(mpcmpfltc(&v->u.cval->real, 0) == 0)
			return fmtprint(fp, "%#Fi", &v->u.cval->imag);
		if(mpcmpfltc(&v->u.cval->imag, 0) == 0)
			return fmtprint(fp, "%#F", &v->u.cval->real);
		if(mpcmpfltc(&v->u.cval->imag, 0) < 0)
			return fmtprint(fp, "(%#F%#Fi)", &v->u.cval->real, &v->u.cval->imag);
		return fmtprint(fp, "(%#F+%#Fi)", &v->u.cval->real, &v->u.cval->imag);
	case CTSTR:
		return fmtprint(fp, "\"%Z\"", v->u.sval);
	case CTBOOL:
		if( v->u.bval)
			return fmtstrcpy(fp, "true");
		return fmtstrcpy(fp, "false");
	case CTNIL:
		return fmtstrcpy(fp, "nil");
	}
	return fmtprint(fp, "<ctype=%d>", v->ctype);
}

// Fmt "%Z": escaped string literals
static int
Zconv(Fmt *fp)
{
	Rune r;
	Strlit *sp;
	char *s, *se;
	int n;

	sp = va_arg(fp->args, Strlit*);
	if(sp == nil)
		return fmtstrcpy(fp, "<nil>");

	s = sp->s;
	se = s + sp->len;

	// NOTE: Keep in sync with ../ld/go.c:/^Zconv.
	while(s < se) {
		n = chartorune(&r, s);
		s += n;
		switch(r) {
		case Runeerror:
			if(n == 1) {
				fmtprint(fp, "\\x%02x", (uchar)*(s-1));
				break;
			}
			// fall through
		default:
			if(r < ' ') {
				fmtprint(fp, "\\x%02x", r);
				break;
			}
			fmtrune(fp, r);
			break;
		case '\t':
			fmtstrcpy(fp, "\\t");
			break;
		case '\n':
			fmtstrcpy(fp, "\\n");
			break;
		case '\"':
		case '\\':
			fmtrune(fp, '\\');
			fmtrune(fp, r);
			break;
		case 0xFEFF: // BOM, basically disallowed in source code
			fmtstrcpy(fp, "\\uFEFF");
			break;
		}
	}
	return 0;
}

/*
s%,%,\n%g
s%\n+%\n%g
s%^[	]*T%%g
s%,.*%%g
s%.+%	[T&]		= "&",%g
s%^	........*\]%&~%g
s%~	%%g
*/

static char*
etnames[] =
{
	[TINT]		= "INT",
	[TUINT]		= "UINT",
	[TINT8]		= "INT8",
	[TUINT8]	= "UINT8",
	[TINT16]	= "INT16",
	[TUINT16]	= "UINT16",
	[TINT32]	= "INT32",
	[TUINT32]	= "UINT32",
	[TINT64]	= "INT64",
	[TUINT64]	= "UINT64",
	[TUINTPTR]	= "UINTPTR",
	[TFLOAT32]	= "FLOAT32",
	[TFLOAT64]	= "FLOAT64",
	[TCOMPLEX64]	= "COMPLEX64",
	[TCOMPLEX128]	= "COMPLEX128",
	[TBOOL]		= "BOOL",
	[TPTR32]	= "PTR32",
	[TPTR64]	= "PTR64",
	[TFUNC]		= "FUNC",
	[TARRAY]	= "ARRAY",
	[TSTRUCT]	= "STRUCT",
	[TCHAN]		= "CHAN",
	[TMAP]		= "MAP",
	[TINTER]	= "INTER",
	[TFORW]		= "FORW",
	[TFIELD]	= "FIELD",
	[TSTRING]	= "STRING",
	[TANY]		= "ANY",
};

// Fmt "%E": etype
static int
Econv(Fmt *fp)
{
	int et;

	et = va_arg(fp->args, int);
	if(et >= 0 && et < nelem(etnames) && etnames[et] != nil)
		return fmtstrcpy(fp, etnames[et]);
	return fmtprint(fp, "E-%d", et);
}

// Fmt "%S": syms
static int
symfmt(Fmt *fp, Sym *s)
{
	char *p;

	if(s->pkg && !(fp->flags&FmtShort)) {
		switch(fmtmode) {
		case FErr:	// This is for the user
			if(s->pkg == localpkg)
				return fmtstrcpy(fp, s->name);
			// If the name was used by multiple packages, display the full path,
			if(s->pkg->name && pkglookup(s->pkg->name, nil)->npkg > 1)
				return fmtprint(fp, "\"%Z\".%s", s->pkg->path, s->name);
			return fmtprint(fp, "%s.%s", s->pkg->name, s->name);
		case FDbg:
			return fmtprint(fp, "%s.%s", s->pkg->name, s->name);
		case FTypeId:
			if(fp->flags&FmtUnsigned)
				return fmtprint(fp, "%s.%s", s->pkg->name, s->name);	// dcommontype, typehash
			return fmtprint(fp, "%s.%s", s->pkg->prefix, s->name);	// (methodsym), typesym, weaksym
		case FExp:
			if(s->name && s->name[0] == '.')
				fatal("exporting synthetic symbol %s", s->name);
			if(s->pkg != builtinpkg)
				return fmtprint(fp, "@\"%Z\".%s", s->pkg->path, s->name);
		}
	}

	if(fp->flags&FmtByte) {  // FmtByte (hh) implies FmtShort (h)
		// skip leading "type." in method name
		p = utfrrune(s->name, '.');
		if(p)
			p++;
		else
			p = s->name;

		// exportname needs to see the name without the prefix too.
		if((fmtmode == FExp && !exportname(p)) || fmtmode == FDbg)
			return fmtprint(fp, "@\"%Z\".%s", s->pkg->path, p);

		return fmtstrcpy(fp, p);
	}

	return fmtstrcpy(fp, s->name);
}

static char*
basicnames[] =
{
	[TINT]		= "int",
	[TUINT]		= "uint",
	[TINT8]		= "int8",
	[TUINT8]	= "uint8",
	[TINT16]	= "int16",
	[TUINT16]	= "uint16",
	[TINT32]	= "int32",
	[TUINT32]	= "uint32",
	[TINT64]	= "int64",
	[TUINT64]	= "uint64",
	[TUINTPTR]	= "uintptr",
	[TFLOAT32]	= "float32",
	[TFLOAT64]	= "float64",
	[TCOMPLEX64]	= "complex64",
	[TCOMPLEX128]	= "complex128",
	[TBOOL]		= "bool",
	[TANY]		= "any",
	[TSTRING]	= "string",
	[TNIL]		= "nil",
	[TIDEAL]	= "untyped number",
	[TBLANK]	= "blank",
};

static int
typefmt(Fmt *fp, Type *t)
{
	Type *t1;
	Sym *s;

	if(t == T)
		return fmtstrcpy(fp, "<T>");

	if (t == bytetype || t == runetype) {
		// in %-T mode collapse rune and byte with their originals.
		if(fmtmode != FTypeId)
			return fmtprint(fp, "%hS", t->sym);
		t = types[t->etype];
	}

	if(t == errortype)
		return fmtstrcpy(fp, "error");

	// Unless the 'l' flag was specified, if the type has a name, just print that name.
	if(!(fp->flags&FmtLong) && t->sym && t->etype != TFIELD && t != types[t->etype]) {
		switch(fmtmode) {
		case FTypeId:
			if(fp->flags&FmtShort) {
				if(t->vargen)
					return fmtprint(fp, "%hS·%d", t->sym, t->vargen);
				return fmtprint(fp, "%hS", t->sym);
			}
			if(fp->flags&FmtUnsigned)
				return fmtprint(fp, "%uS", t->sym);
			// fallthrough
		case FExp:
			if(t->sym->pkg == localpkg && t->vargen)
				return fmtprint(fp, "%S·%d", t->sym, t->vargen);
			break;
		}
		return fmtprint(fp, "%S", t->sym);
	}

	if(t->etype < nelem(basicnames) && basicnames[t->etype] != nil) {
		if(fmtmode == FErr && (t == idealbool || t == idealstring))
			fmtstrcpy(fp, "untyped ");
		return fmtstrcpy(fp, basicnames[t->etype]);
	}

	if(fmtmode == FDbg)
		fmtprint(fp, "%E-", t->etype);

	switch(t->etype) {
	case TPTR32:
	case TPTR64:
		if(fmtmode == FTypeId && (fp->flags&FmtShort))
			return fmtprint(fp, "*%hT", t->type);
		return fmtprint(fp, "*%T", t->type);

	case TARRAY:
		if(t->bound >= 0)
			return fmtprint(fp, "[%lld]%T", t->bound, t->type);
		if(t->bound == -100)
			return fmtprint(fp, "[...]%T", t->type);
		return fmtprint(fp, "[]%T", t->type);

	case TCHAN:
		switch(t->chan) {
		case Crecv:
			return fmtprint(fp, "<-chan %T", t->type);
		case Csend:
			return fmtprint(fp, "chan<- %T", t->type);
		}

		if(t->type != T && t->type->etype == TCHAN && t->type->sym == S && t->type->chan == Crecv)
			return fmtprint(fp, "chan (%T)", t->type);
		return fmtprint(fp, "chan %T", t->type);

	case TMAP:
		return fmtprint(fp, "map[%T]%T", t->down, t->type);

	case TINTER:
		fmtstrcpy(fp, "interface {");
		for(t1=t->type; t1!=T; t1=t1->down)
			if(exportname(t1->sym->name)) {
				if(t1->down)
					fmtprint(fp, " %hS%hT;", t1->sym, t1->type);
				else
					fmtprint(fp, " %hS%hT ", t1->sym, t1->type);
			} else {
				// non-exported method names must be qualified
				if(t1->down)
					fmtprint(fp, " %uS%hT;", t1->sym, t1->type);
				else
					fmtprint(fp, " %uS%hT ", t1->sym, t1->type);
			}
		fmtstrcpy(fp, "}");
		return 0;

	case TFUNC:
		if(fp->flags & FmtShort) {
			fmtprint(fp, "%T", getinargx(t));
		} else {
			if(t->thistuple)
				fmtprint(fp, "method%T func%T", getthisx(t), getinargx(t));
			else
				fmtprint(fp, "func%T", getinargx(t));
		}
		switch(t->outtuple) {
		case 0:
			break;
		case 1:
			if(fmtmode != FExp) {
				fmtprint(fp, " %T", getoutargx(t)->type->type);	 // struct->field->field's type
				break;
			}
		default:
			fmtprint(fp, " %T", getoutargx(t));
			break;
		}
		return 0;

	case TSTRUCT:
		// Format the bucket struct for map[x]y as map.bucket[x]y.
		// This avoids a recursive print that generates very long names.
		if(t->map != T) {
			if(t->map->bucket == t) {
				return fmtprint(fp, "map.bucket[%T]%T", t->map->down, t->map->type);
			}
			if(t->map->hmap == t) {
				return fmtprint(fp, "map.hdr[%T]%T", t->map->down, t->map->type);
			}
			if(t->map->hiter == t) {
				return fmtprint(fp, "map.iter[%T]%T", t->map->down, t->map->type);
			}
			yyerror("unknown internal map type");
		}

		if(t->funarg) {
			fmtstrcpy(fp, "(");
			if(fmtmode == FTypeId || fmtmode == FErr) {	// no argument names on function signature, and no "noescape" tags
				for(t1=t->type; t1!=T; t1=t1->down)
					if(t1->down)
						fmtprint(fp, "%hT, ", t1);
					else
						fmtprint(fp, "%hT", t1);
			} else {
				for(t1=t->type; t1!=T; t1=t1->down)
					if(t1->down)
						fmtprint(fp, "%T, ", t1);
					else
						fmtprint(fp, "%T", t1);
			}
			fmtstrcpy(fp, ")");
		} else {
			fmtstrcpy(fp, "struct {");
			for(t1=t->type; t1!=T; t1=t1->down)
				if(t1->down)
					fmtprint(fp, " %lT;", t1);
				else
					fmtprint(fp, " %lT ", t1);
			fmtstrcpy(fp, "}");
		}
		return 0;

	case TFIELD:
		if(!(fp->flags&FmtShort)) {
			s = t->sym;

			// Take the name from the original, lest we substituted it with ~r%d or ~b%d.
			// ~r%d is a (formerly) unnamed result.
			if ((fmtmode == FErr || fmtmode == FExp) && t->nname != N) {
				if(t->nname->orig != N) {
					s = t->nname->orig->sym;
					if(s != S && s->name[0] == '~') {
						if(s->name[1] == 'r') // originally an unnamed result
							s = S;
						else if(s->name[1] == 'b') // originally the blank identifier _
							s = lookup("_");
					}
				} else 
					s = S;
			}
			
			if(s != S && !t->embedded) {
				if(t->funarg)
					fmtprint(fp, "%N ", t->nname);
				else if(fp->flags&FmtLong)
					fmtprint(fp, "%hhS ", s);  // qualify non-exported names (used on structs, not on funarg)
				else 
					fmtprint(fp, "%S ", s);
			} else if(fmtmode == FExp) {
				// TODO(rsc) this breaks on the eliding of unused arguments in the backend
				// when this is fixed, the special case in dcl.c checkarglist can go.
				//if(t->funarg)
				//	fmtstrcpy(fp, "_ ");
				//else
				if(t->embedded && s->pkg != nil && s->pkg->path->len > 0)
					fmtprint(fp, "@\"%Z\".? ", s->pkg->path);
				else
					fmtstrcpy(fp, "? ");
			}
		}

		if(t->isddd)
			fmtprint(fp, "...%T", t->type->type);
		else
			fmtprint(fp, "%T", t->type);

		if(!(fp->flags&FmtShort) && t->note)
			fmtprint(fp, " \"%Z\"", t->note);
		return 0;

	case TFORW:
		if(t->sym)
			return fmtprint(fp, "undefined %S", t->sym);
		return fmtstrcpy(fp, "undefined");

	case TUNSAFEPTR:
		if(fmtmode == FExp)
			return fmtprint(fp, "@\"unsafe\".Pointer");
		return fmtprint(fp, "unsafe.Pointer");
	}

	if(fmtmode == FExp)
		fatal("missing %E case during export", t->etype);
	// Don't know how to handle - fall back to detailed prints.
	return fmtprint(fp, "%E <%S> %T", t->etype, t->sym, t->type);
}

// Statements which may be rendered with a simplestmt as init.
static int
stmtwithinit(int op)
{
	switch(op) {
	case OIF:
	case OFOR:
	case OSWITCH:
		return 1;
	}
	return 0;
}

static int
stmtfmt(Fmt *f, Node *n)
{
	int complexinit, simpleinit, extrablock;

	// some statements allow for an init, but at most one,
	// but we may have an arbitrary number added, eg by typecheck
	// and inlining.  If it doesn't fit the syntax, emit an enclosing
	// block starting with the init statements.

	// if we can just say "for" n->ninit; ... then do so
	simpleinit = n->ninit && !n->ninit->next && !n->ninit->n->ninit && stmtwithinit(n->op);
	// otherwise, print the inits as separate statements
	complexinit = n->ninit && !simpleinit && (fmtmode != FErr);
	// but if it was for if/for/switch, put in an extra surrounding block to limit the scope
	extrablock = complexinit && stmtwithinit(n->op);

	if(extrablock)
		fmtstrcpy(f, "{");

	if(complexinit)
		fmtprint(f, " %H; ", n->ninit);

	switch(n->op){
	case ODCL:
		if(fmtmode == FExp) {
			switch(n->left->class&~PHEAP) {
			case PPARAM:
			case PPARAMOUT:
			case PAUTO:
				fmtprint(f, "var %N %T", n->left, n->left->type);
				goto ret;
			}
		}			
		fmtprint(f, "var %S %T", n->left->sym, n->left->type);
		break;

	case ODCLFIELD:
		if(n->left)
			fmtprint(f, "%N %N", n->left, n->right);
		else
			fmtprint(f, "%N", n->right);
		break;

	case OAS:
		// Don't export "v = <N>" initializing statements, hope they're always 
		// preceded by the DCL which will be re-parsed and typecheck to reproduce
		// the "v = <N>" again.
		if(fmtmode == FExp && n->right == N)
			break;

		if(n->colas && !complexinit)
			fmtprint(f, "%N := %N", n->left, n->right);
		else
			fmtprint(f, "%N = %N", n->left, n->right);
		break;

	case OASOP:
		fmtprint(f, "%N %#O= %N", n->left, n->etype, n->right);
		break;

	case OAS2:
		if(n->colas && !complexinit) {
			fmtprint(f, "%,H := %,H", n->list, n->rlist);
			break;
		}
		// fallthrough
	case OAS2DOTTYPE:
	case OAS2FUNC:
	case OAS2MAPR:
	case OAS2RECV:
		fmtprint(f, "%,H = %,H", n->list, n->rlist);
		break;

	case ORETURN:
		fmtprint(f, "return %,H", n->list);
		break;

	case ORETJMP:
		fmtprint(f, "retjmp %S", n->sym);
		break;
	
	case OPROC:
		fmtprint(f, "go %N", n->left);
		break;

	case ODEFER:
		fmtprint(f, "defer %N", n->left);
		break;

	case OIF:
		if(simpleinit)
			fmtprint(f, "if %N; %N { %H }", n->ninit->n, n->ntest, n->nbody);
		else
			fmtprint(f, "if %N { %H }", n->ntest, n->nbody);
		if(n->nelse)
			fmtprint(f, " else { %H }", n->nelse);
		break;

	case OFOR:
		if(fmtmode == FErr) {	// TODO maybe only if FmtShort, same below
			fmtstrcpy(f, "for loop");
			break;
		}

		fmtstrcpy(f, "for");
		if(simpleinit)
			fmtprint(f, " %N;", n->ninit->n);
		else if(n->nincr)
			fmtstrcpy(f, " ;");

		if(n->ntest)
			fmtprint(f, " %N", n->ntest);

		if(n->nincr)
			fmtprint(f, "; %N", n->nincr);
		else if(simpleinit)
			fmtstrcpy(f, ";");


		fmtprint(f, " { %H }", n->nbody);
		break;

	case ORANGE:
		if(fmtmode == FErr) {
			fmtstrcpy(f, "for loop");
			break;
		}

		fmtprint(f, "for %,H = range %N { %H }", n->list, n->right, n->nbody);
		break;

	case OSELECT:
	case OSWITCH:
		if(fmtmode == FErr) {
			fmtprint(f, "%O statement", n->op);
			break;
		}

		fmtprint(f, "%#O", n->op);
		if(simpleinit)
			fmtprint(f, " %N;", n->ninit->n);
		if(n->ntest)
			fmtprint(f, "%N", n->ntest);

		fmtprint(f, " { %H }", n->list);
		break;

	case OCASE:
	case OXCASE:
		if(n->list)
			fmtprint(f, "case %,H: %H", n->list, n->nbody);
		else
			fmtprint(f, "default: %H", n->nbody);
		break;

	case OBREAK:
	case OCONTINUE:
	case OGOTO:
	case OFALL:
	case OXFALL:
		if(n->left)
			fmtprint(f, "%#O %N", n->op, n->left);
		else
			fmtprint(f, "%#O", n->op);
		break;

	case OEMPTY:
		break;

	case OLABEL:
		fmtprint(f, "%N: ", n->left);
		break;
	  
	}
ret:

	if(extrablock)
		fmtstrcpy(f, "}");

	return 0;
}


static int opprec[] = {
	[OAPPEND] = 8,
	[OARRAYBYTESTR] = 8,
	[OARRAYLIT] = 8,
	[OARRAYRUNESTR] = 8,
	[OCALLFUNC] = 8,
	[OCALLINTER] = 8,
	[OCALLMETH] = 8,
	[OCALL] = 8,
	[OCAP] = 8,
	[OCLOSE] = 8,
	[OCONVIFACE] = 8,
	[OCONVNOP] = 8,
	[OCONV] = 8,
	[OCOPY] = 8,
	[ODELETE] = 8,
	[OLEN] = 8,
	[OLITERAL] = 8,
	[OMAKESLICE] = 8,
	[OMAKE] = 8,
	[OMAPLIT] = 8,
	[ONAME] = 8,
	[ONEW] = 8,
	[ONONAME] = 8,
	[OPACK] = 8,
	[OPANIC] = 8,
	[OPAREN] = 8,
	[OPRINTN] = 8,
	[OPRINT] = 8,
	[ORUNESTR] = 8,
	[OSTRARRAYBYTE] = 8,
	[OSTRARRAYRUNE] = 8,
	[OSTRUCTLIT] = 8,
	[OTARRAY] = 8,
	[OTCHAN] = 8,
	[OTFUNC] = 8,
	[OTINTER] = 8,
	[OTMAP] = 8,
	[OTPAREN] = 8,
	[OTSTRUCT] = 8,

	[OINDEXMAP] = 8,
	[OINDEX] = 8,
	[OSLICE] = 8,
	[OSLICESTR] = 8,
	[OSLICEARR] = 8,
	[OSLICE3] = 8,
	[OSLICE3ARR] = 8,
	[ODOTINTER] = 8,
	[ODOTMETH] = 8,
	[ODOTPTR] = 8,
	[ODOTTYPE2] = 8,
	[ODOTTYPE] = 8,
	[ODOT] = 8,
	[OXDOT] = 8,
	[OCALLPART] = 8,

	[OPLUS] = 7,
	[ONOT] = 7,
	[OCOM] = 7,
	[OMINUS] = 7,
	[OADDR] = 7,
	[OIND] = 7,
	[ORECV] = 7,

	[OMUL] = 6,
	[ODIV] = 6,
	[OMOD] = 6,
	[OLSH] = 6,
	[ORSH] = 6,
	[OAND] = 6,
	[OANDNOT] = 6,

	[OADD] = 5,
	[OSUB] = 5,
	[OOR] = 5,
	[OXOR] = 5,

	[OEQ] = 4,
	[OLT] = 4,
	[OLE] = 4,
	[OGE] = 4,
	[OGT] = 4,
	[ONE] = 4,
	[OCMPSTR] = 4,
	[OCMPIFACE] = 4,

	[OSEND] = 3,
	[OANDAND] = 2,
	[OOROR] = 1,

	// Statements handled by stmtfmt
	[OAS] = -1,
	[OAS2] = -1,
	[OAS2DOTTYPE] = -1,
	[OAS2FUNC] = -1,
	[OAS2MAPR] = -1,
	[OAS2RECV] = -1,
	[OASOP] = -1,
	[OBREAK] = -1,
	[OCASE] = -1,
	[OCONTINUE] = -1,
	[ODCL] = -1,
	[ODCLFIELD] = -1,
	[ODEFER] = -1,
	[OEMPTY] = -1,
	[OFALL] = -1,
	[OFOR] = -1,
	[OGOTO] = -1,
	[OIF] = -1,
	[OLABEL] = -1,
	[OPROC] = -1,
	[ORANGE] = -1,
	[ORETURN] = -1,
	[OSELECT] = -1,
	[OSWITCH] = -1,
	[OXCASE] = -1,
	[OXFALL] = -1,

	[OEND] = 0
};

static int
exprfmt(Fmt *f, Node *n, int prec)
{
	int nprec;
	int ptrlit;
	NodeList *l;

	while(n && n->implicit && (n->op == OIND || n->op == OADDR))
		n = n->left;

	if(n == N)
		return fmtstrcpy(f, "<N>");

	nprec = opprec[n->op];
	if(n->op == OTYPE && n->sym != S)
		nprec = 8;

	if(prec > nprec)
		return fmtprint(f, "(%N)", n);

	switch(n->op) {
	case OPAREN:
		return fmtprint(f, "(%N)", n->left);

	case ODDDARG:
		return fmtprint(f, "... argument");

	case OREGISTER:
		return fmtprint(f, "%R", n->val.u.reg);

	case OLITERAL:  // this is a bit of a mess
		if(fmtmode == FErr && n->sym != S)
			return fmtprint(f, "%S", n->sym);
		if(n->val.ctype == CTNIL && n->orig != N && n->orig != n)
			return exprfmt(f, n->orig, prec);
		if(n->type != T && n->type != types[n->type->etype] && n->type != idealbool && n->type != idealstring) {
			// Need parens when type begins with what might
			// be misinterpreted as a unary operator: * or <-.
			if(isptr[n->type->etype] || (n->type->etype == TCHAN && n->type->chan == Crecv))
				return fmtprint(f, "(%T)(%V)", n->type, &n->val);
			else 
				return fmtprint(f, "%T(%V)", n->type, &n->val);
		}
		return fmtprint(f, "%V", &n->val);

	case ONAME:
		// Special case: name used as local variable in export.
		switch(n->class&~PHEAP){
		case PAUTO:
		case PPARAM:
		case PPARAMOUT:
			// _ becomes ~b%d internally; print as _ for export
			if(fmtmode == FExp && n->sym && n->sym->name[0] == '~' && n->sym->name[1] == 'b')
				return fmtprint(f, "_");
			if(fmtmode == FExp && n->sym && !isblank(n) && n->vargen > 0)
				return fmtprint(f, "%S·%d", n->sym, n->vargen);
		}

		// Special case: explicit name of func (*T) method(...) is turned into pkg.(*T).method,
		// but for export, this should be rendered as (*pkg.T).meth.
		// These nodes have the special property that they are names with a left OTYPE and a right ONAME.
		if(fmtmode == FExp && n->left && n->left->op == OTYPE && n->right && n->right->op == ONAME) {
			if(isptr[n->left->type->etype])
				return fmtprint(f, "(%T).%hhS", n->left->type, n->right->sym);
			else
				return fmtprint(f, "%T.%hhS", n->left->type, n->right->sym);
		}
		//fallthrough
	case OPACK:
	case ONONAME:
		return fmtprint(f, "%S", n->sym);

	case OTYPE:
		if(n->type == T && n->sym != S)
			return fmtprint(f, "%S", n->sym);
		return fmtprint(f, "%T", n->type);

	case OTARRAY:
		if(n->left)
			return fmtprint(f, "[]%N", n->left);
		return fmtprint(f, "[]%N", n->right);  // happens before typecheck

	case OTPAREN:
		return fmtprint(f, "(%N)", n->left);

	case OTMAP:
		return fmtprint(f, "map[%N]%N", n->left, n->right);

	case OTCHAN:
		switch(n->etype) {
		case Crecv:
			return fmtprint(f, "<-chan %N", n->left);
		case Csend:
			return fmtprint(f, "chan<- %N", n->left);
		default:
			if(n->left != N && n->left->op == TCHAN && n->left->sym == S && n->left->etype == Crecv)
				return fmtprint(f, "chan (%N)", n->left);
			else
				return fmtprint(f, "chan %N", n->left);
		}

	case OTSTRUCT:
		return fmtprint(f, "<struct>");

	case OTINTER:
		return fmtprint(f, "<inter>");

	case OTFUNC:
		return fmtprint(f, "<func>");

	case OCLOSURE:
		if(fmtmode == FErr)
			return fmtstrcpy(f, "func literal");
		if(n->nbody)
			return fmtprint(f, "%T { %H }", n->type, n->nbody);
		return fmtprint(f, "%T { %H }", n->type, n->closure->nbody);

	case OCOMPLIT:
		ptrlit = n->right != N && n->right->implicit && n->right->type && isptr[n->right->type->etype];
		if(fmtmode == FErr) {
			if(n->right != N && n->right->type != T && !n->implicit) {
				if(ptrlit)
					return fmtprint(f, "&%T literal", n->right->type->type);
				else
					return fmtprint(f, "%T literal", n->right->type);
			}
			return fmtstrcpy(f, "composite literal");
		}
		if(fmtmode == FExp && ptrlit)
			// typecheck has overwritten OIND by OTYPE with pointer type.
			return fmtprint(f, "(&%T{ %,H })", n->right->type->type, n->list);
		return fmtprint(f, "(%N{ %,H })", n->right, n->list);

	case OPTRLIT:
		if(fmtmode == FExp && n->left->implicit)
			return fmtprint(f, "%N", n->left);
		return fmtprint(f, "&%N", n->left);

	case OSTRUCTLIT:
		if(fmtmode == FExp) {   // requires special handling of field names
			if(n->implicit)
				fmtstrcpy(f, "{");
			else
				fmtprint(f, "(%T{", n->type);
			for(l=n->list; l; l=l->next) {
				fmtprint(f, " %hhS:%N", l->n->left->sym, l->n->right);

				if(l->next)
					fmtstrcpy(f, ",");
				else
					fmtstrcpy(f, " ");
			}
			if(!n->implicit)
				return fmtstrcpy(f, "})");
			return fmtstrcpy(f, "}");
		}
		// fallthrough

	case OARRAYLIT:
	case OMAPLIT:
		if(fmtmode == FErr)
			return fmtprint(f, "%T literal", n->type);
		if(fmtmode == FExp && n->implicit)
			return fmtprint(f, "{ %,H }", n->list);
		return fmtprint(f, "(%T{ %,H })", n->type, n->list);

	case OKEY:
		if(n->left && n->right) {
			if(fmtmode == FExp && n->left->type && n->left->type->etype == TFIELD) {
				// requires special handling of field names
				return fmtprint(f, "%hhS:%N", n->left->sym, n->right);
			} else
				return fmtprint(f, "%N:%N", n->left, n->right);
		}
		if(!n->left && n->right)
			return fmtprint(f, ":%N", n->right);
		if(n->left && !n->right)
			return fmtprint(f, "%N:", n->left);
		return fmtstrcpy(f, ":");

	case OXDOT:
	case ODOT:
	case ODOTPTR:
	case ODOTINTER:
	case ODOTMETH:
	case OCALLPART:
		exprfmt(f, n->left, nprec);
		if(n->right == N || n->right->sym == S)
			return fmtstrcpy(f, ".<nil>");
		return fmtprint(f, ".%hhS", n->right->sym);

	case ODOTTYPE:
	case ODOTTYPE2:
		exprfmt(f, n->left, nprec);
		if(n->right != N)
			return fmtprint(f, ".(%N)", n->right);
		return fmtprint(f, ".(%T)", n->type);

	case OINDEX:
	case OINDEXMAP:
	case OSLICE:
	case OSLICESTR:
	case OSLICEARR:
	case OSLICE3:
	case OSLICE3ARR:
		exprfmt(f, n->left, nprec);
		return fmtprint(f, "[%N]", n->right);

	case OCOPY:
	case OCOMPLEX:
		return fmtprint(f, "%#O(%N, %N)", n->op, n->left, n->right);

	case OCONV:
	case OCONVIFACE:
	case OCONVNOP:
	case OARRAYBYTESTR:
	case OARRAYRUNESTR:
	case OSTRARRAYBYTE:
	case OSTRARRAYRUNE:
	case ORUNESTR:
		if(n->type == T || n->type->sym == S)
			return fmtprint(f, "(%T)(%N)", n->type, n->left);
		if(n->left)
			return fmtprint(f, "%T(%N)", n->type, n->left);
		return fmtprint(f, "%T(%,H)", n->type, n->list);

	case OREAL:
	case OIMAG:
	case OAPPEND:
	case OCAP:
	case OCLOSE:
	case ODELETE:
	case OLEN:
	case OMAKE:
	case ONEW:
	case OPANIC:
	case ORECOVER:
	case OPRINT:
	case OPRINTN:
		if(n->left)
			return fmtprint(f, "%#O(%N)", n->op, n->left);
		if(n->isddd)
			return fmtprint(f, "%#O(%,H...)", n->op, n->list);
		return fmtprint(f, "%#O(%,H)", n->op, n->list);

	case OCALL:
	case OCALLFUNC:
	case OCALLINTER:
	case OCALLMETH:
		exprfmt(f, n->left, nprec);
		if(n->isddd)
			return fmtprint(f, "(%,H...)", n->list);
		return fmtprint(f, "(%,H)", n->list);

	case OMAKEMAP:
	case OMAKECHAN:
	case OMAKESLICE:
		if(n->list) // pre-typecheck
			return fmtprint(f, "make(%T, %,H)", n->type, n->list);
		if(n->right)
			return fmtprint(f, "make(%T, %N, %N)", n->type, n->left, n->right);
		if(n->left)
			return fmtprint(f, "make(%T, %N)", n->type, n->left);
		return fmtprint(f, "make(%T)", n->type);

	// Unary
	case OPLUS:
	case OMINUS:
	case OADDR:
	case OCOM:
	case OIND:
	case ONOT:
	case ORECV:
		if(n->left->op == n->op)
			fmtprint(f, "%#O ", n->op);
		else
			fmtprint(f, "%#O", n->op);
		return exprfmt(f, n->left, nprec+1);

	// Binary
	case OADD:
	case OAND:
	case OANDAND:
	case OANDNOT:
	case ODIV:
	case OEQ:
	case OGE:
	case OGT:
	case OLE:
	case OLT:
	case OLSH:
	case OMOD:
	case OMUL:
	case ONE:
	case OOR:
	case OOROR:
	case ORSH:
	case OSEND:
	case OSUB:
	case OXOR:
		exprfmt(f, n->left, nprec);
		fmtprint(f, " %#O ", n->op);
		exprfmt(f, n->right, nprec+1);
		return 0;

	case OADDSTR:
		for(l=n->list; l; l=l->next) {
			if(l != n->list)
				fmtprint(f, " + ");
			exprfmt(f, l->n, nprec);
		}
		return 0;

	case OCMPSTR:
	case OCMPIFACE:
		exprfmt(f, n->left, nprec);
		fmtprint(f, " %#O ", n->etype);
		exprfmt(f, n->right, nprec+1);
		return 0;
	}

	return fmtprint(f, "<node %O>", n->op);
}

static int
nodefmt(Fmt *f, Node *n)
{
	Type *t;

	t = n->type;

	// we almost always want the original, except in export mode for literals
	// this saves the importer some work, and avoids us having to redo some
	// special casing for package unsafe
	if((fmtmode != FExp || n->op != OLITERAL) && n->orig != N)
		n = n->orig;

	if(f->flags&FmtLong && t != T) {
		if(t->etype == TNIL)
			return fmtprint(f, "nil");
		else
			return fmtprint(f, "%N (type %T)", n, t);
	}

	// TODO inlining produces expressions with ninits. we can't print these yet.

	if(opprec[n->op] < 0)
		return stmtfmt(f, n);

	return exprfmt(f, n, 0);
}

static int dumpdepth;

static void
indent(Fmt *fp)
{
	int i;

	fmtstrcpy(fp, "\n");
	for(i = 0; i < dumpdepth; ++i)
		fmtstrcpy(fp, ".   ");
}

static int
nodedump(Fmt *fp, Node *n)
{
	int recur;

	if(n == N)
		return 0;

	recur = !(fp->flags&FmtShort);

	if(recur) {
		indent(fp);
		if(dumpdepth > 10)
			return fmtstrcpy(fp, "...");

		if(n->ninit != nil) {
			fmtprint(fp, "%O-init%H", n->op, n->ninit);
			indent(fp);
		}
	}

//	fmtprint(fp, "[%p]", n);

	switch(n->op) {
	default:
		fmtprint(fp, "%O%J", n->op, n);
		break;
	case OREGISTER:
	case OINDREG:
		fmtprint(fp, "%O-%R%J", n->op, n->val.u.reg, n);
		break;
	case OLITERAL:
		fmtprint(fp, "%O-%V%J", n->op, &n->val, n);
		break;
	case ONAME:
	case ONONAME:
		if(n->sym != S)
			fmtprint(fp, "%O-%S%J", n->op, n->sym, n);
		else
			fmtprint(fp, "%O%J", n->op, n);
		if(recur && n->type == T && n->ntype) {
			indent(fp);
			fmtprint(fp, "%O-ntype%N", n->op, n->ntype);
		}
		break;
	case OASOP:
		fmtprint(fp, "%O-%O%J", n->op, n->etype, n);
		break;
	case OTYPE:
		fmtprint(fp, "%O %S%J type=%T", n->op, n->sym, n, n->type);
		if(recur && n->type == T && n->ntype) {
			indent(fp);
			fmtprint(fp, "%O-ntype%N", n->op, n->ntype);
		}
		break;
	}

	if(n->sym != S && n->op != ONAME)
		fmtprint(fp, " %S G%d", n->sym, n->vargen);

	if(n->type != T)
		fmtprint(fp, " %T", n->type);

	if(recur) {
		if(n->left)
			fmtprint(fp, "%N", n->left);
		if(n->right)
			fmtprint(fp, "%N", n->right);
		if(n->list) {
			indent(fp);
			fmtprint(fp, "%O-list%H", n->op, n->list);
		}
		if(n->rlist) {
			indent(fp);
			fmtprint(fp, "%O-rlist%H", n->op, n->rlist);
		}
		if(n->ntest) {
			indent(fp);
			fmtprint(fp, "%O-test%N", n->op, n->ntest);
		}
		if(n->nbody) {
			indent(fp);
			fmtprint(fp, "%O-body%H", n->op, n->nbody);
		}
		if(n->nelse) {
			indent(fp);
			fmtprint(fp, "%O-else%H", n->op, n->nelse);
		}
		if(n->nincr) {
			indent(fp);
			fmtprint(fp, "%O-incr%N", n->op, n->nincr);
		}
	}

	return 0;
}

// Fmt "%S": syms
// Flags:  "%hS" suppresses qualifying with package
static int
Sconv(Fmt *fp)
{
	Sym *s;
	int r, sm;
	unsigned long sf;

	if(fp->flags&FmtLong)
		return linksymfmt(fp);

	s = va_arg(fp->args, Sym*);
	if(s == S)
		return fmtstrcpy(fp, "<S>");

	if(s->name && s->name[0] == '_' && s->name[1] == '\0')
		return fmtstrcpy(fp, "_");

	sf = fp->flags;
	sm = setfmode(&fp->flags);
	r = symfmt(fp, s);
	fp->flags = sf;
	fmtmode = sm;
	return r;
}

// Fmt "%T": types.
// Flags: 'l' print definition, not name
//	  'h' omit 'func' and receiver from function types, short type names
//	  'u' package name, not prefix (FTypeId mode, sticky)
static int
Tconv(Fmt *fp)
{
	Type *t;
	int r, sm;
	unsigned long sf;

	t = va_arg(fp->args, Type*);
	if(t == T)
		return fmtstrcpy(fp, "<T>");

	if(t->trecur > 4)
		return fmtstrcpy(fp, "<...>");

	t->trecur++;
	sf = fp->flags;
	sm = setfmode(&fp->flags);

	if(fmtmode == FTypeId && (sf&FmtUnsigned))
		fmtpkgpfx++;
	if(fmtpkgpfx)
		fp->flags |= FmtUnsigned;

	r = typefmt(fp, t);

	if(fmtmode == FTypeId && (sf&FmtUnsigned))
		fmtpkgpfx--;

	fp->flags = sf;
	fmtmode = sm;
	t->trecur--;
	return r;
}

// Fmt '%N': Nodes.
// Flags: 'l' suffix with "(type %T)" where possible
//	  '+h' in debug mode, don't recurse, no multiline output
static int
Nconv(Fmt *fp)
{
	Node *n;
	int r, sm;
	unsigned long sf;

	n = va_arg(fp->args, Node*);
	if(n == N)
		return fmtstrcpy(fp, "<N>");
	sf = fp->flags;
	sm = setfmode(&fp->flags);

	r = -1;
	switch(fmtmode) {
	case FErr:
	case FExp:
		r = nodefmt(fp, n);
		break;
	case FDbg:
		dumpdepth++;
		r = nodedump(fp, n);
		dumpdepth--;
		break;
	default:
		fatal("unhandled %%N mode");
	}

	fp->flags = sf;
	fmtmode = sm;
	return r;
}

// Fmt '%H': NodeList.
// Flags: all those of %N plus ',': separate with comma's instead of semicolons.
static int
Hconv(Fmt *fp)
{
	NodeList *l;
	int r, sm;
	unsigned long sf;
	char *sep;

	l = va_arg(fp->args, NodeList*);

	if(l == nil && fmtmode == FDbg)
		return fmtstrcpy(fp, "<nil>");

	sf = fp->flags;
	sm = setfmode(&fp->flags);
	r = 0;
	sep = "; ";
	if(fmtmode == FDbg)
		sep = "\n";
	else if(fp->flags & FmtComma)
		sep = ", ";

	for(;l; l=l->next) {
		r += fmtprint(fp, "%N", l->n);
		if(l->next)
			r += fmtstrcpy(fp, sep);
	}

	fp->flags = sf;
	fmtmode = sm;
	return r;
}

void
fmtinstallgo(void)
{
	fmtmode = FErr;
	fmtinstall('E', Econv);		// etype opcodes
	fmtinstall('J', Jconv);		// all the node flags
	fmtinstall('H', Hconv);		// node lists
	fmtinstall('L', Lconv);		// line number
	fmtinstall('N', Nconv);		// node pointer
	fmtinstall('O', Oconv);		// node opcodes
	fmtinstall('S', Sconv);		// sym pointer
	fmtinstall('T', Tconv);		// type pointer
	fmtinstall('V', Vconv);		// val pointer
	fmtinstall('Z', Zconv);		// escaped string

	// These are in mparith1.c
	fmtinstall('B', Bconv);	// big numbers
	fmtinstall('F', Fconv);	// big float numbers

}

void
dumplist(char *s, NodeList *l)
{
	print("%s%+H\n", s, l);
}

void
dump(char *s, Node *n)
{
	print("%s [%p]%+N\n", s, n, n);
}