@@ -55,6 +55,7 @@ type BuildConfig struct {
55
55
ldFlags []string
56
56
tags string
57
57
wasmAbi string
58
+ heapSize int64
58
59
testConfig compiler.TestConfig
59
60
}
60
61
@@ -240,10 +241,16 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
240
241
// Prepare link command.
241
242
executable := filepath .Join (dir , "main" )
242
243
tmppath := executable // final file
243
- ldflags : = append (ldflags , "-o" , executable , objfile , "-L" , root )
244
+ ldflags = append (ldflags , "-o" , executable , objfile , "-L" , root )
244
245
if spec .RTLib == "compiler-rt" {
245
246
ldflags = append (ldflags , librt )
246
247
}
248
+ if spec .GOARCH == "wasm" {
249
+ // Round heap size to next multiple of 65536 (the WebAssembly page
250
+ // size).
251
+ heapSize := (config .heapSize + (65536 - 1 )) &^ (65536 - 1 )
252
+ ldflags = append (ldflags , "--initial-memory=" + strconv .FormatInt (heapSize , 10 ))
253
+ }
247
254
248
255
// Compile extra files.
249
256
for i , path := range spec .ExtraFiles {
@@ -540,6 +547,30 @@ func Run(pkgName, target string, config *BuildConfig) error {
540
547
})
541
548
}
542
549
550
+ // parseSize converts a human-readable size (with k/m/g suffix) into a plain
551
+ // number.
552
+ func parseSize (s string ) (int64 , error ) {
553
+ s = strings .ToLower (strings .TrimSpace (s ))
554
+ if len (s ) == 0 {
555
+ return 0 , errors .New ("no size provided" )
556
+ }
557
+ multiply := int64 (1 )
558
+ switch s [len (s )- 1 ] {
559
+ case 'k' :
560
+ multiply = 1 << 10
561
+ case 'm' :
562
+ multiply = 1 << 20
563
+ case 'g' :
564
+ multiply = 1 << 30
565
+ }
566
+ if multiply != 1 {
567
+ s = s [:len (s )- 1 ]
568
+ }
569
+ n , err := strconv .ParseInt (s , 0 , 64 )
570
+ n *= multiply
571
+ return n , err
572
+ }
573
+
543
574
func usage () {
544
575
fmt .Fprintln (os .Stderr , "TinyGo is a Go compiler for small places." )
545
576
fmt .Fprintln (os .Stderr , "version:" , version )
@@ -598,6 +629,7 @@ func main() {
598
629
cFlags := flag .String ("cflags" , "" , "additional cflags for compiler" )
599
630
ldFlags := flag .String ("ldflags" , "" , "additional ldflags for linker" )
600
631
wasmAbi := flag .String ("wasm-abi" , "js" , "WebAssembly ABI conventions: js (no i64 params) or generic" )
632
+ heapSize := flag .String ("heap-size" , "1M" , "default heap size in bytes (only supported by WebAssembly)" )
601
633
602
634
if len (os .Args ) < 2 {
603
635
fmt .Fprintln (os .Stderr , "No command-line arguments supplied." )
@@ -633,6 +665,13 @@ func main() {
633
665
os .Exit (1 )
634
666
}
635
667
668
+ var err error
669
+ if config .heapSize , err = parseSize (* heapSize ); err != nil {
670
+ fmt .Fprintln (os .Stderr , "Could not read heap size:" , * heapSize )
671
+ usage ()
672
+ os .Exit (1 )
673
+ }
674
+
636
675
os .Setenv ("CC" , "clang -target=" + * target )
637
676
638
677
switch command {
0 commit comments