forked from flightaware/tsp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsp-clang.tcl
2041 lines (1769 loc) · 73.5 KB
/
tsp-clang.tcl
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
##################################################################
# language specific procs - c
#package require tcc4tcl
package require critcl
# FIXME - strings and string command impls should use Tcl_UniChar arrays, not
# UTF-8 strings.
# FIXME
# for testing, set cache dir and clear cache once
# this is ordinarily in ::tsp::lang_compile
::critcl::cache ./.critcl
::critcl::clean_cache
# force critcl to load so we can capture the original PkgInit bodhy
catch {::critcl::cproc}
variable ::tsp::critcl_pkginit [info body ::critcl::PkgInit]
# BUILTIN_TCL_COMMANDS
# interpreter builtin commands that we can call directly
# note: this is all compiled commands, since some tsp_compiled
# command may defer to the builtin ones.
#
# create native/clang/TSP_cmd.c as:
# tclsh8.6
# source tsp.tcl; set fd [open native/clang/TSP_cmd.c w] ; puts $fd [::tsp::lang_builtin_refs] ; close $fd
# SPILL_LOAD_COMMANDS
# commands that specify variables by name, requiring spill/load
# each element is a list of: command subcommand-or-option start end vartype spill-load-type.
# if variable naem is not previously defined, it will be defined as the first type
# listed in vartype.
# note: this list only for commands not otherwise compiled, and also not for array
# variable names, since they are always kept in the interp ("file stat"),
# and not for introspection commands ("info"). See ::tsp::check_varname_args
# for details
namespace eval ::tsp {
variable BUILTIN_TCL_COMMANDS [list \
after append apply array binary \
break case catch cd chan \
clock close concat continue dict \
encoding eof error eval exec \
exit expr fblocked fconfigure fcopy \
file fileevent flush for foreach \
format gets glob global if \
incr info interp join lappend \
lassign lindex linsert list llength \
lmap load lrange lrepeat lreplace \
lreverse lsearch lset lsort namespace \
open package pid proc puts \
pwd read regexp regsub rename \
return scan seek set socket \
source split string subst switch \
tailcall tell time trace try \
unload unset update uplevel upvar \
variable vwait while yield yieldto \
zlib \
]
variable SPILL_LOAD_COMMANDS [list \
[list binary scan 4 end var load] \
[list dict append 2 2 var spill/load] \
[list dict incr 2 2 var spill/load] \
[list dict lappend 2 2 var spill/load] \
[list dict set 2 2 var spill/load] \
[list dict unset 2 2 var spill/load] \
[list dict update 2 2 var spill/load] \
[list dict with 2 2 var spill/load] \
[list gets "" 2 2 var load] \
[list lassign "" 2 end var load] \
[list lset "" 1 1 var spill/load] \
[list regexp -- +2 end var spill/load] \
[list regsub -- +3 end var spill/load] \
[list scan "" 3 end var load] \
[list vwait "" 1 1 var load] \
]
}
##############################################
# translate a tsp type to a native type, note that
# we shouldn't see 'array' as a proc argument type
#
proc ::tsp::lang_xlate_native_type {tsp_type} {
switch $tsp_type {
boolean {return int}
int {return Tcl_WideInt}
double {return double}
var {return Tcl_Obj*}
array {return Tcl_Obj*}
string {return Tcl_DString*}
}
# rest are also java types
return $tsp_type
}
##############################################
# return native boolean type
#
proc ::tsp::lang_type_boolean {} {
return int
}
##############################################
# return native int type
#
proc ::tsp::lang_type_int {} {
return Tcl_WideInt
}
##############################################
# return native double type
#
proc ::tsp::lang_type_double {} {
return double
}
##############################################
# return native string type: String/DString
#
proc ::tsp::lang_type_string {} {
return Tcl_DString*
}
##############################################
# return native var type
#
proc ::tsp::lang_type_var {} {
return Tcl_Obj*
}
##############################################
# return native null reference
#
proc ::tsp::lang_type_null {} {
return NULL
}
##############################################
# declare a native boolean
#
proc ::tsp::lang_decl_native_boolean {varName} {
return "int $varName = 0;\n"
}
##############################################
# declare a native int
#
proc ::tsp::lang_decl_native_int {varName} {
return "Tcl_WideInt $varName = 0;\n"
}
##############################################
# declare a native double
#
proc ::tsp::lang_decl_native_double {varName} {
return "double $varName = 0;\n"
}
##############################################
# declare a native string
#
proc ::tsp::lang_decl_native_string {varName} {
return "Tcl_DString* $varName;\n"
}
##############################################
# free a native string
#
proc ::tsp::lang_free_native_string {varName} {
return "Tcl_DStringFree(${varName});\n"
}
##############################################
# declare a tclobj
#
proc ::tsp::lang_decl_var {varName} {
return "Tcl_Obj* $varName = NULL;\n"
}
##############################################
# declare a native tclobj, same as ::tsp::lang_decl_var
#
proc ::tsp::lang_decl_native_var {varName} {
return [::tsp::lang_decl_var $varName]
}
##############################################
# new tclobj var as boolean
#
proc ::tsp::lang_new_var_boolean {varName value} {
return "$varName = Tcl_NewBooleanObj($value);\n"
}
##############################################
# new tclobj var as integer
#
proc ::tsp::lang_new_var_int {varName value} {
return "$varName = Tcl_NewWideIntObj((Tcl_WideInt) $value);\n"
}
##############################################
# new tclobj var as double
#
proc ::tsp::lang_new_var_double {varName value} {
return "$varName = Tcl_NewDoubleObj((double) $value);\n"
}
##############################################
# new tclobj var as string obj
# str must either be a quoted string const or a native string var
proc ::tsp::lang_new_var_string {varName str} {
if {[string range $str 0 0] eq "\""} {
return "$varName = Tcl_NewStringObj($str,-1);\n"
} else {
return "$varName = Tcl_NewStringObj(Tcl_DStringValue($str), Tcl_DStringLength($str));\n"
}
}
##############################################
# free a tcl obj var
#
proc ::tsp::lang_free_var {varName} {
return "$varName = NULL\n;"
}
##############################################
# convert a boolean from a string value
#
proc ::tsp::lang_convert_boolean_string {targetVarName sourceVarName errMsg} {
append result "/* ::tsp::lang_convert_boolean_string */\n"
if {[string range $sourceVarName 0 0] eq "\""} {
append result "if ((*rc = Tcl_GetBoolean(interp, $sourceVarName, $targetVarName)) != TCL_OK) \{\n"
} else {
append result "if ((*rc = Tcl_GetBoolean(interp, $sourceVarName, Tcl_DStringValue($targetVarName))) != TCL_OK) \{\n"
}
append result " Tcl_AppendResult(interp, [::tsp::lang_quote_string $errMsg], \"$sourceVarName\", (char *) NULL);\n"
append result " ERROR_EXIT;\n"
append result "\}\n"
return $result
}
##############################################
# convert a boolean from a var value
# note that errMsg should be passed unquoted (done here)
#
proc ::tsp::lang_convert_boolean_var {targetVarName sourceVarName errMsg} {
append result "/* ::tsp::lang_convert_boolean_var */\n"
append result "if ((*rc = Tcl_GetBooleanFromObj(interp, $sourceVarName, &$targetVarName)) != TCL_OK) \{\n"
append result " Tcl_AppendResult(interp, [::tsp::lang_quote_string $errMsg], Tcl_GetString($sourceVarName), (char *) NULL);\n"
append result " ERROR_EXIT;\n"
append result "\}\n"
return $result
}
##############################################
# convert a int from a string value
# note that errMsg should be passed unquoted (done here)
#
proc ::tsp::lang_convert_int_string {targetVarName sourceVarName errMsg} {
append result "/* ::tsp::lang_convert_int_string */\n"
if {[string range $sourceVarName 0 0] eq "\""} {
append result "if ((*rc = TSP_Util_lang_convert_int_string_const(interp, $sourceVarName, &$targetVarName)) != TCL_OK) \{\n"
} else {
append result "if ((*rc = TSP_Util_lang_convert_int_string(interp, $sourceVarName, &$targetVarName)) != TCL_OK) \{\n"
}
#FIXME: see Tcl_GetInt() but convert use Tcl_GetWideIntFromObj instead.
append result " Tcl_AppendResult(interp, [::tsp::lang_quote_string $errMsg], Tcl_GetString($sourceVarName), (char *) NULL);\n"
append result " ERROR_EXIT;\n"
append result "\}\n"
return $result
}
##############################################
# convert a int from a boolean value
#
proc ::tsp::lang_convert_int_boolean {targetVarName sourceVarName {errMsg ""}} {
append result "/* ::tsp::lang_convert_int_boolean */\n"
append result "$targetVarName = $sourceVarName ? 1 : 0;\n"
return $result
}
##############################################
# convert a int from a double value
#
proc ::tsp::lang_convert_int_double {targetVarName sourceVarName {errMsg ""}} {
append result "/* ::tsp::lang_convert_int_double */\n"
append result "$targetVarName = (Tcl_WideInt) $sourceVarName;\n"
return $result
}
##############################################
# convert a double from a string value
# note that errMsg should be passed unquoted (done here)
#
proc ::tsp::lang_convert_double_string {targetVarName sourceVarName errMsg} {
append result "/* ::tsp::lang_convert_double_string */\n"
if {[string range $sourceVarName 0 0] eq "\""} {
append result "if ((*rc = Tcl_GetDouble(interp, $sourceVarName, &$targetVarName)) != TCL_OK) \{\n"
} else {
append result "if ((*rc = Tcl_GetDouble(interp, Tcl_DStringValue($sourceVarName), &$targetVarName)) != TCL_OK) \{\n"
}
append result " Tcl_AppendResult(interp, [::tsp::lang_quote_string $errMsg], \"$sourceVarName\", (char *) NULL);\n"
append result " ERROR_EXIT;\n"
append result "\}\n"
return $result
}
##############################################
# convert an int from a var value
# note that errMsg should be passed unquoted (done here)
#
proc ::tsp::lang_convert_int_var {targetVarName sourceVarName errMsg} {
append result "/* ::tsp::lang_convert_int_var */\n"
append result "if ((*rc = Tcl_GetWideIntFromObj(interp, $sourceVarName, &$targetVarName)) != TCL_OK) \{\n"
append result " Tcl_AppendResult(interp, [::tsp::lang_quote_string $errMsg], \"$sourceVarName\", (char *) NULL);\n"
append result " ERROR_EXIT;\n"
append result "\}\n"
return $result
}
##############################################
# convert a double from a var value
# note that errMsg should be passed unquoted (done here)
#
proc ::tsp::lang_convert_double_var {targetVarName sourceVarName errMsg} {
append result "/* ::tsp::lang_convert_double_var */\n"
append result "if ((*rc = Tcl_GetDoubleFromObj(interp, $sourceVarName, &$targetVarName)) != TCL_OK) \{\n"
append result " Tcl_AppendResult(interp, [::tsp::lang_quote_string $errMsg], \"$sourceVarName\", (char *) NULL);\n"
append result " ERROR_EXIT;\n"
append result "\}\n"
return $result
}
##############################################
# convert a string from a boolean value
#
proc ::tsp::lang_convert_string_boolean {targetVarName sourceVarName {errMsg ""}} {
append result "/* ::tsp::lang_convert_string_boolean */\n"
append result "Tcl_DStringSetLength($targetVarName,0);\n"
append result "Tcl_DStringAppend($targetVarName, ($sourceVarName ? : \"1\" : \"0\"), -1);\n"
return $result
}
##############################################
# convert a string from an int value
#
proc ::tsp::lang_convert_string_int {targetVarName sourceVarName {errMsg ""}} {
append result "/* ::tsp::lang_convert_string_int */\n"
append result "$targetVarName = TSP_Util_lang_convert_string_int(interp, $targetVarName, (TCL_WIDE_INT_TYPE) $sourceVarName);\n"
#FIXME: see UpdateStringOfWideInt()
return $result
}
##############################################
# convert a string from a double value
#
proc ::tsp::lang_convert_string_double {targetVarName sourceVarName {errMsg ""}} {
append result "/* ::tsp::lang_convert_string_double */\n"
append result "$targetVarName = TSP_Util_lang_convert_string_double(interp, $targetVarName, $sourceVarName);\n"
#FIXME: see Tcl_PrintDouble()
return $result
}
##############################################
# convert a string from a string value
#
proc ::tsp::lang_convert_string_string {targetVarName sourceVarName {errMsg ""}} {
append result "/* ::tsp::lang_convert_string_string */\n"
append result "Tcl_DStringSetLength($targetVarName,0);\n"
if {[string index $sourceVarName 0] eq {"}} {
append result "Tcl_DStringAppend($targetVarName, $sourceVarName, -1);\n"
} else {
append result "Tcl_DStringAppend($targetVarName, Tcl_DStringValue($sourceVarName), Tcl_DStringLength($sourceVarName));\n"
}
return $result
}
##############################################
# convert a string from a var value
#
proc ::tsp::lang_convert_string_var {targetVarName sourceVarName {errMsg ""}} {
append result "/* ::tsp::lang_convert_string_var */\n"
append result "Tcl_DStringSetLength($targetVarName, 0);\n"
append result "$targetVarName = TSP_Util_lang_convert_string_var($targetVarName, $sourceVarName);\n"
#FIXME: Tcl_GetStringFromObj, set DString as a result
return $result
}
##############################################
# convert a var from a var value, this is here for
# easy of substitution, calls lang_assign_var_var
#
proc ::tsp::lang_convert_var_var {targetVarName sourceVarName {errMsg ""}} {
return [::tsp::lang_assign_var_var $targetVarName $sourceVarName]
}
##############################################
# get string from a boolean value
# NOTE - return value must be used immediately
#
proc ::tsp::lang_get_string_boolean {sourceVarName} {
return "$sourceVarName ? : \"1\" : \"0\""
}
##############################################
# get a string from an int value
# NOTE - return value must be used immediately
#
proc ::tsp::lang_get_string_int {sourceVarName} {
return "TSP_Util_lang_get_string_int((TCL_WIDE_INT_TYPE) $sourceVarName)"
}
##############################################
# get a string from a double value
# NOTE - return value must be used immediately
#
proc ::tsp::lang_get_string_double {sourceVarName} {
return "TSP_Util_lang_get_string_double($sourceVarName)"
}
##############################################
# get a string from a string value
# NOTE - return value must be used immediately
#
proc ::tsp::lang_get_string_string {sourceVarName} {
return "$sourceVarName"
}
##############################################
# get a string from a var value
# NOTE - return value must be used immediately
#
proc ::tsp::lang_get_string_var {sourceVarName} {
return "TSP_Util_lang_get_string_var($sourceVarName)"
}
##############################################
# preserve / incrRefCount a TclObject variable
#
proc ::tsp::lang_preserve {obj} {
return "Tcl_IncrRefCount($obj);\n"
}
##############################################
# release / decrRefCount a TclObject variable
#
proc ::tsp::lang_release {obj} {
return "Tcl_DecrRefCount($obj);\n"
}
##############################################
# safe release / decrRefCount a TclObject variable
# and set object to null.
# checks for null before release
#
proc ::tsp::lang_safe_release {obj} {
return "if ($obj != NULL) { Tcl_DecrRefCount($obj); $obj = NULL; } \n"
}
##############################################
# quote string
# return a valid string constant with quotes
#
proc ::tsp::lang_quote_string {str} {
append result \"
foreach ch [split $str {}] {
scan $ch %c val
if {$val >=32 && $val <= 127} {
if {$ch eq "\\" || $ch eq "\""} {
append result \\
}
append result $ch
} else {
switch -- $ch {
"\b" {append result \\b}
"\t" {append result \\t}
"\n" {append result \\n}
"\f" {append result \\f}
"\r" {append result \\r}
default {
append result \\x
if {$val < 256} {
append result [format %02x $val]
} else {
append result [format %04x $val]
}
}
}
}
}
append result \"
return $result
}
##############################################
# int constant
# appends long designation for java
proc ::tsp::lang_int_const {n} {
if {[string is wideinteger $n]} {
append n LL
}
return $n
}
##############################################
# double constant
# appends double designation for java
proc ::tsp::lang_double_const {n} {
if {[string is wideinteger $n] || [string is double $n]} {
append n d
}
return $n
}
##############################################
# boolean true constant
#
proc ::tsp::lang_true_const {} {
return $::tsp::VALUE_TRUE
}
##############################################
# boolean false constant
#
proc ::tsp::lang_false_const {} {
return $::tsp::VALUE_FALSE
}
##############################################
# assign empty/zero
#
proc ::tsp::lang_assign_empty_zero {var type} {
set code "/* ::tsp::lang_assign_empty_zero */\n"
switch $type {
boolean {append code "$var = 0;\n"}
int -
double {append code "$var = 0;\n"}
string {append code "Tcl_DStringSetLength($var, 0);\n"}
var {append code [::tsp::lang_safe_release $var]
append code [::tsp::lang_new_var_string $var {""}]
}
}
return $code
}
##############################################
# assign a TclObject from a interp array with a scalar or var index
# note that errMsg should be passed unquoted (done here)
# arrVar and index are tcl objects
#
proc ::tsp::lang_assign_var_array_idxvar {targetObj arrVar idxVar errMsg} {
append result "/* ::tsp::lang_assign_var_array_idxvar */\n"
append result "$targetObj = Tcl_ObjGetVar2(interp, $arrVar, $idxVar, TCL_LEAVE_ERR_MSG);\n"
append result "if ($targetObj == NULL) \{\n"
append result " /* Tcl_AppendResult(interp, [::tsp::lang_quote_string $errMsg], (char *) NULL);*/\n"
append result " *rc = TCL_ERROR;\n"
append result " ERROR_EXIT;\n"
append result "\}\n"
return $result
}
##############################################
# assign a TclObject from a interp array with a text index or var index
# arrVar and idxTxtVar are tcl objects
# note that errMsg should be passed unquoted (done here)
#
proc ::tsp::lang_assign_var_array_idxtext {targetObj arrVar idxTxtVar errMsg} {
append result "/* ::tsp::lang_array_get_array_idxtext */\n"
append result "$targetObj = Tcl_ObjGetVar2(interp, $arrVar, $idxTxtVar, TCL_LEAVE_ERR_MSG);\n"
append result "if ($targetObj == NULL) \{\n"
append result " /* Tcl_AppendResult(interp, [::tsp::lang_quote_string $errMsg], (char *) NULL);*/\n"
append result " *rc = TCL_ERROR;\n"
append result " ERROR_EXIT;\n"
append result "\}\n"
return $result
return $result
}
##############################################
# assign a native string from a literal constant
# note that errMsg should be passed unquoted (done here)
#
proc ::tsp::lang_assign_string_const {targetVarName sourceText} {
append result "/* ::tsp::lang_assign_string_const */\n"
append result "Tcl_DStringSetLength($targetVarName,0);\n"
append result "Tcl_DStringAppend($targetVarName, [::tsp::lang_quote_string $sourceText], -1);\n"
return $result
}
##############################################
# assign a tclobj var from a boolean constant
#
proc ::tsp::lang_assign_var_boolean {targetVarName sourceVarName {preserve 1}} {
append result "/* ::tsp::lang_assign_var_boolean */\n"
append result "$targetVarName = TSP_Util_lang_assign_var_boolean($targetVarName, (int) $sourceVarName);\n"
return $result
}
##############################################
# assign a tclobj var from an int constant
#
proc ::tsp::lang_assign_var_int {targetVarName sourceVarName {preserve 1}} {
append result "/* ::tsp::lang_assign_var_int */\n"
append result "$targetVarName = TSP_Util_lang_assign_var_int($targetVarName, (TCL_WIDE_INT_TYPE) $sourceVarName);\n"
return $result
}
##############################################
# assign a tclobj var from an double constant
#
proc ::tsp::lang_assign_var_double {targetVarName sourceVarName {preserve 1}} {
append result "/* ::tsp::lang_assign_var_double */\n"
append result "$targetVarName = TSP_Util_lang_assign_var_double($targetVarName, (double) $sourceVarName);\n"
return $result
}
##############################################
# assign a tclobj var from an native string
#
proc ::tsp::lang_assign_var_string {targetVarName sourceVarName {preserve 1}} {
append result "/* ::tsp::lang_assign_var_string */\n"
if {[string range $sourceVarName 0 0] eq {"}} {
append result "$targetVarName = TSP_Util_lang_assign_var_string_const($targetVarName, $sourceVarName);\n"
} else {
append result "$targetVarName = TSP_Util_lang_assign_var_string($targetVarName, $sourceVarName);\n"
}
return $result
}
##############################################
# assign a tclobj var from a tclobj var
#
proc ::tsp::lang_assign_var_var {targetVarName sourceVarName {preserve 1}} {
append result "/* ::tsp::lang_assign_var_var */\n"
append result "$targetVarName = TSP_Util_lang_assign_var_var($targetVarName, $sourceVarName);\n"
return $result
}
##############################################
# assign a tclobj var to an interp array using name1/name2
# targetArrayStr targetIdxStr must already be valid tcl objects
# the var is the object to assign into the array,
# which is preserved and released
#
proc ::tsp::lang_assign_array_var {targetArrayVar targetIdxVar var} {
append result "/* ::tsp::lang_assign_array_var */\n"
append result "TSP_Util_lang_assign_array_var(interp, $targetArrayVar, $targetIdxVar, $var);\n"
return $result
}
##############################################
# append a string to a native string
# source should either be a native string variable or
# quoted string constant
#
proc ::tsp::lang_append_string {targetVarName source} {
append result "/* ::tsp::lang_append_string */\n"
if {[string range $source 0 0] eq "\""} {
append result "Tcl_DStringAppend($targetVarName, $source, -1);\n"
} else {
append result "Tcl_DStringAppend($targetVarName, Tcl_DStringValue($source), Tcl_DStringLength($source));\n"
}
return $result
}
##############################################
# append a string to a var
# source should either be a native string variable or
# quoted string constant
#
proc ::tsp::lang_append_var {targetVarName source} {
append result "/* ::tsp::lang_append_var */\n"
if {[string range $source 0 0] eq "\""} {
append result "Tcl_AppendToObj($targetVarName, $source, -1);\n"
} else {
append result "Tcl_AppendToObj($targetVarName, Tcl_DStringValue($source), Tcl_DStringLength($source));\n"
}
return $result
}
##############################################
# append a TclObject var to list
#
proc ::tsp::lang_lappend_var {targetVarName sourceVarName} {
append result "/* ::tsp::lang_lappend_var */\n"
append result "Tcl_ListObjAppendElement(interp, $targetVarName, $sourceVarName);\n"
return $result
}
##############################################
# duplicate a TclObject var if shared, or assign as
# empty if null
#
proc ::tsp::lang_dup_var_if_shared {targetVarName} {
append result "/* ::tsp::lang_dup_var_if_shared */\n"
append result "if ($targetVarName != NULL) \{\n"
append result " if (Tcl_IsShared($targetVarName)) \{\n"
append result " [::tsp::lang_release $targetVarName]"
append result " $targetVarName = Tcl_DuplicateObj($targetVarName);\n"
append result " [::tsp::lang_preserve $targetVarName]"
append result " \}\n"
append result "\} else \{\n"
append result " [::tsp::lang_new_var_string $targetVarName {""}]"
append result " [::tsp::lang_preserve $targetVarName]"
append result "\}\n"
return $result
}
##############################################
# allocate a TclObject objv array
#
proc ::tsp::lang_alloc_objv_array {compUnitDict size} {
#FIXME: track argObjvArray size, only free/alloc when too small
upvar $compUnitDict compUnit
set cmdLevel [dict get $compUnit cmdLevel]
::tsp::addArgsPerLevel compUnit $cmdLevel $size
#append result "if (argObjvArray_$cmdLevel != NULL) \{\n"
#append result " ckfree(argObjvArray_$cmdLevel);\n"
#append result "\}\n"
#append result "argObjvArray_$cmdLevel = ckalloc($size * sizeof(Tcl_Obj *));\n"
#append result "argObjc_$cmdLevel = $size;\n"
#return $result
return ""
}
##############################################
# assign a TclObject var to a TclObject objv array
#
proc ::tsp::lang_assign_objv {compUnitDict n max obj} {
upvar $compUnitDict compUnit
set cmdLevel [dict get $compUnit cmdLevel]
::tsp::addArgsPerLevel compUnit $cmdLevel $max
return "argObjvArray_$cmdLevel\[$n\] = $obj;\n"
}
##############################################
# invoke a builtin tcl command
# assumes argObjvArray has been constructed
#
proc ::tsp::lang_invoke_builtin {compUnitDict cmd max} {
upvar $compUnitDict compUnit
set cmdLevel [dict get $compUnit cmdLevel]
append code "\n/* ::tsp::lang_invoke_builtin */\n"
append code "if ((*rc = (TSP_Cmd_builtin_$cmd) ((ClientData)NULL, interp, $max, argObjvArray_$cmdLevel)) != TCL_OK) \{\n"
append code " ERROR_EXIT;\n"
append code "\}\n"
append code [::tsp::lang_safe_release _tmpVar_cmdResultObj]
append code "_tmpVar_cmdResultObj = Tcl_GetObjResult(interp);\n"
append code [::tsp::lang_preserve _tmpVar_cmdResultObj] \n
return [list _tmpVar_cmdResultObj $code]
}
##############################################
# allocate a TclObject objv list
#
proc ::tsp::lang_alloc_objv_list {varName} {
return "$varName = Tcl_NewListObj(0, NULL);\n"
}
##############################################
# invoke a tcl command via the interp
# assumes argObjvArray has been constructed
#
proc ::tsp::lang_invoke_tcl {compUnitDict max} {
upvar $compUnitDict compUnit
set cmdLevel [dict get $compUnit cmdLevel]
append code "\n/* ::tsp::lang_invoke_tcl */\n"
append code "if ((*rc = Tcl_EvalObjv(interp, $max, argObjvArray_$cmdLevel, 0)) != TCL_OK) \{\n"
append code " ERROR_EXIT;\n"
append code "\}\n"
append code [::tsp::lang_safe_release _tmpVar_cmdResultObj]
append code "_tmpVar_cmdResultObj = Tcl_GetObjResult(interp);\n"
append code [::tsp::lang_preserve _tmpVar_cmdResultObj] \n
return [list _tmpVar_cmdResultObj $code]
}
##############################################
# invoke a tcl command that is a previously compiled tsp proc
# argList should only be native types
# any args that are Tcl vars should be preserved/released
#
proc ::tsp::lang_invoke_tsp_compiled {cmdName procType returnVar argList preserveArgList} {
if {$procType eq "void"} {
set returnVar ""
set returnVarAssign ""
} else {
set returnVar $returnVar
set returnVarAssign "$returnVar = ([::tsp::lang_type_$procType]) "
}
set invokeArgs "interp, rc"
if {[string length $argList]} {
append invokeArgs ", [join $argList ", "]"
}
append code "/* ::tsp::lang_invoke_tsp_compiled */\n"
if {$procType eq "var"} {
append code [::tsp::lang_safe_release $returnVar]
} elseif {$procType eq "string"} {
append code "Tcl_DStringSetLength($returnVar, 0);\n"
}
append code "${returnVarAssign}TSP_UserDirect_${cmdName}($invokeArgs);\n"
append code "if (*rc != TCL_OK) \{\n"
append code " ERROR_EXIT;\n"
append code "\}\n"
return $code
}
##############################################
# release var/string/temp var
#
proc ::tsp::lang_release_vars {compUnitDict} {
upvar $compUnitDict compUnit
set buf ""
set varList [dict get $compUnit vars]
foreach var $varList {
set type [dict get $compUnit var $var ]
switch $type {
var {
append buf [::tsp::lang_safe_release $var]
}
string {
append buf "Tcl_DStringFree($var);\n"
}
}
}
return $buf
}
##############################################
# standard vars - define variables used at runtime, but not
# those defined as internal method arguments, block defined
# or native array types
#
proc ::tsp::lang_standard_vars {compUnitDict} {
upvar $compUnitDict compUnit
foreach var {_tmpVar_cmdResultObj argObjvList} {
}
}
##############################################
# create_compilable - create class/function definition with
# tcl interface and with direct interface
#
proc ::tsp::lang_create_compilable {compUnitDict code} {
upvar $compUnitDict compUnit
set name [dict get $compUnit name]
# create a list of proc argument names, prepended with __
set procArgs ""
foreach arg [dict get $compUnit args] {
lappend procArgs __$arg
}
# get the list of proc argument types
set procArgTypes [dict get $compUnit argTypes]
# create the java proc arguments
set nativeArgs [join $procArgs ", "]
if {[string length $nativeArgs]} {
set nativeArgs ", $nativeArgs"
}
set nativeTypedArgs ""
set intfProcArgs ""
set intfProcInit ""
set intfVarAssignments ""
set procVarPreserves ""
set procArgsCleanup ""
set comma ""
set i 0
set procVarsDecls ""
set procVarsCleanup ""
set procStringsInit ""
set procStringsCleanup ""
set declStringsCleanup ""
set procStringsAlloc ""
set copyStringArgs ""
set procConstDecls ""
set procConstInit ""
# create: assignments from proc args to scoped variables;
# declarations of scoped variables
# cleanup code for arguments (var types)
# code to preserve var types in proc method
foreach arg $procArgs {
set type [lindex $procArgTypes $i]
# rest of foreach needs i+1
incr i
set nativeType [::tsp::lang_xlate_native_type $type]
if {$type eq "string"} {
# strings are passed as pointers
append nativeTypedArgs $comma $nativeType " " Caller_$arg
append intfProcArgs "Tcl_DString Cmd_$arg;\n"
append intfProcArgs [::tsp::lang_decl_native_$type $arg]
append intfProcInit "Tcl_DStringInit(&Cmd_$arg);\n"
append intfProcInit "$arg = &Cmd_$arg;\n"
# and define as proc variables
append procVarsDecls [::tsp::lang_decl_native_string $arg]
append procStringsAlloc "Tcl_DString Proc_$arg;\n"
append procStringsInit "Tcl_DStringInit(&Proc_$arg);\n"
append procStringsInit "$arg = &Proc_$arg;\n"
append procStringsCleanup "Tcl_DStringFree($arg);\n"
append declStringsCleanup "Tcl_DStringFree($arg);\n"
# and copy from argument
append copyStringArgs "Tcl_DStringAppend($arg, Tcl_DStringValue(Caller_$arg), Tcl_DStringLength(Caller_$arg));\n"
} else {
append nativeTypedArgs $comma $nativeType " " $arg
append intfProcArgs [::tsp::lang_decl_native_$type $arg]
}
if {$type eq "var"} {
append intfVarAssignments [::tsp::lang_assign_var_var $arg objv\[$i\]]
append procVarPreserves [::tsp::lang_preserve $arg]
} else {
append intfVarAssignments [::tsp::lang_convert_${type}_var $arg objv\[$i\] "can't convert arg $i to $type"]
}
set comma ", "
}
if {[string length $nativeTypedArgs]} {
set nativeTypedArgs ", $nativeTypedArgs"
}
# create the proc method return assignment operation and declaration
set numProcArgs [llength $procArgs]
set returnType [dict get $compUnit returns]
set nativeReturnType [::tsp::lang_xlate_native_type $returnType]
set missing_return "Tcl_SetResult(interp, \"end of proc encountered without 'return' command, proc type: $returnType\", TCL_STATIC); goto error_exit;"
switch $returnType {
var {
set returnVar returnValue
set returnValueCmd "returnValue = "
set returnVarDecl [::tsp::lang_decl_native_$returnType returnValue]
set returnVarAssignment "returnValue = "
set intfReturnSetResult "Tcl_SetObjResult(interp, returnValue);"
set returnAlloc ""
set returnInit ""
set returnCleanup $missing_return
}
string {
# note that string return type returns a pointer to an alloc'ed Tcl_DString
set returnVar returnValue
set returnValueCmd "returnValue = "
set returnVarDecl [::tsp::lang_decl_native_$returnType returnValue]
set returnVarAssignment "returnValue = "
set intfReturnSetResult "Tcl_DStringResult(interp, returnValue); ckfree((char*) returnValue);"
set returnAlloc "returnValue = (Tcl_DString*) ckalloc(sizeof(Tcl_DString));"
set returnInit "Tcl_DStringInit(returnValue);"
set returnCleanup $missing_return
}
boolean {