12
12
#define NUM_OPERATORS 45
13
13
#define NUM_EXPRESSION_TYPES 4
14
14
#define EXPRESSION_CHAIN_STACK_SIZE 100
15
+ #define MAX_STACK_FRAMES 100
15
16
16
17
struct LexTokenStat {
17
18
const char * name ;
@@ -274,6 +275,13 @@ const char *OperatorSymbols[NUM_OPERATORS] = {
274
275
")"
275
276
};
276
277
278
+ struct StackFrameStats {
279
+ unsigned int LvalueAllocation ;
280
+ unsigned int TotalAllocation ;
281
+ unsigned int CumulativeLvalueAllocation ;
282
+ unsigned int CumulativeTotalAllocation ;
283
+ };
284
+
277
285
278
286
void stats_print_expression (enum ExpressionType Type , enum LexToken Op , enum BaseType TopType , enum BaseType BottomType );
279
287
void stats_traverse_expressions_tree (struct ExpressionChainItem * Node );
@@ -298,9 +306,14 @@ struct ExpressionChainNode *CurrentExpression = NULL;
298
306
struct ExpressionChainItem ExpressionChainsRoot = {{0 }};
299
307
struct ExpressionChainItem * ExpressionChainTreePosition = NULL ;
300
308
union ExpressionHash ExpressionChainStack [EXPRESSION_CHAIN_STACK_SIZE ];
301
- int ExpressionChainStackTop = 0 ;
302
- int TotalExpressions = 0 ;
303
- int TotalExpressionChains = 0 ;
309
+ unsigned int ExpressionChainStackTop = 0 ;
310
+ unsigned int TotalExpressions = 0 ;
311
+ unsigned int TotalExpressionChains = 0 ;
312
+ struct StackFrameStats StackFrameAllocations [MAX_STACK_FRAMES ] = {{0 }};
313
+ unsigned int MaxStackFrameLvalueAllocation = 0 ;
314
+ unsigned int MaxStackFrameTotalAllocation = 0 ;
315
+ unsigned int MaxCumulativeLvalueAllocation = 0 ;
316
+ unsigned int MaxCumulativeTotalAllocation = 0 ;
304
317
305
318
306
319
void stats_log_statement (enum LexToken token , struct ParseState * parser )
@@ -634,8 +647,20 @@ void stats_log_stack_frame_add(struct ParseState *parser, const char *funcName)
634
647
if (StackFramesDepth > StackFramesMaxDepth ) {
635
648
StackFramesMaxDepth = StackFramesDepth ;
636
649
}
637
- if (parser -> pc -> PrintStats ) {
638
- fprintf (stderr , "Adding stack frame for '%s()' (current depth %u, max %u) at %s:%d:%d\n" ,
650
+
651
+ StackFrameAllocations [StackFramesDepth ].TotalAllocation = 0 ;
652
+ StackFrameAllocations [StackFramesDepth ].LvalueAllocation = 0 ;
653
+ StackFrameAllocations [StackFramesDepth ].CumulativeTotalAllocation = StackFrameAllocations [StackFramesDepth - 1 ].CumulativeTotalAllocation ;
654
+ StackFrameAllocations [StackFramesDepth ].CumulativeLvalueAllocation = StackFrameAllocations [StackFramesDepth - 1 ].CumulativeLvalueAllocation ;
655
+
656
+ if (parser -> pc -> PrintStats || parser -> pc -> PrintMemory ) {
657
+ fprintf (stderr , "\n" );
658
+ for (int i = 0 ; i < StackFramesDepth - 1 ; i ++ )
659
+ fprintf (stderr , " " );
660
+ fprintf (stderr , "***\n" );
661
+ for (int i = 0 ; i < StackFramesDepth - 1 ; i ++ )
662
+ fprintf (stderr , " " );
663
+ fprintf (stderr , "Adding stack frame for '%s()' (new depth %u, max depth %u) at %s:%d:%d\n" ,
639
664
funcName , StackFramesDepth , StackFramesMaxDepth , parser -> FileName , parser -> Line , parser -> CharacterPos );
640
665
}
641
666
}
@@ -646,9 +671,87 @@ void stats_log_stack_frame_pop(struct ParseState *parser)
646
671
{
647
672
if (parser -> pc -> CollectStats ) {
648
673
StackFramesDepth -- ;
649
- if (parser -> pc -> PrintStats ) {
650
- fprintf (stderr , "Popping stack frame (current depth %u, max %u) at %s:%d:%d\n" ,
674
+ if (parser -> pc -> PrintStats || parser -> pc -> PrintMemory ) {
675
+ for (int i = 0 ; i < StackFramesDepth ; i ++ )
676
+ fprintf (stderr , " " );
677
+ fprintf (stderr , "Popping stack frame (new depth %u, max depth %u) at %s:%d:%d\n" ,
651
678
StackFramesDepth , StackFramesMaxDepth , parser -> FileName , parser -> Line , parser -> CharacterPos );
679
+ for (int i = 0 ; i < StackFramesDepth ; i ++ )
680
+ fprintf (stderr , " " );
681
+ fprintf (stderr , "***\n\n" );
682
+ }
683
+ }
684
+ }
685
+
686
+
687
+ void stats_log_stack_allocation (struct ParseState * parser , int Size , int IsLValue )
688
+ {
689
+ if (parser -> pc -> CollectStats && (parser -> Mode == RunModeRun ) && (strcmp (parser -> FileName , "startup" ) != 0 )) {
690
+
691
+ StackFrameAllocations [StackFramesDepth ].TotalAllocation += Size ;
692
+ if (StackFrameAllocations [StackFramesDepth ].TotalAllocation > MaxStackFrameTotalAllocation )
693
+ MaxStackFrameTotalAllocation = StackFrameAllocations [StackFramesDepth ].TotalAllocation ;
694
+
695
+ StackFrameAllocations [StackFramesDepth ].CumulativeTotalAllocation += Size ;
696
+ if (StackFrameAllocations [StackFramesDepth ].CumulativeTotalAllocation > MaxCumulativeTotalAllocation )
697
+ MaxCumulativeTotalAllocation = StackFrameAllocations [StackFramesDepth ].CumulativeTotalAllocation ;
698
+
699
+ if (IsLValue ) {
700
+ StackFrameAllocations [StackFramesDepth ].LvalueAllocation += Size ;
701
+ if (StackFrameAllocations [StackFramesDepth ].LvalueAllocation > MaxStackFrameLvalueAllocation )
702
+ MaxStackFrameLvalueAllocation = StackFrameAllocations [StackFramesDepth ].LvalueAllocation ;
703
+
704
+ StackFrameAllocations [StackFramesDepth ].CumulativeLvalueAllocation += Size ;
705
+ if (StackFrameAllocations [StackFramesDepth ].CumulativeLvalueAllocation > MaxCumulativeLvalueAllocation )
706
+ MaxCumulativeLvalueAllocation = StackFrameAllocations [StackFramesDepth ].CumulativeLvalueAllocation ;
707
+ }
708
+
709
+ if (parser -> pc -> PrintMemory ) {
710
+ for (int i = 0 ; i < StackFramesDepth ; i ++ )
711
+ fprintf (stderr , " " );
712
+ fprintf (stderr , "%s:%d:%d Allocated %d bytes on stack (total %d/%d) %s\n" ,
713
+ parser -> FileName , parser -> Line , parser -> CharacterPos , Size ,
714
+ StackFrameAllocations [StackFramesDepth ].TotalAllocation ,
715
+ StackFrameAllocations [StackFramesDepth ].CumulativeTotalAllocation ,
716
+ IsLValue ? "(lvalue)" : "" );
717
+ }
718
+ }
719
+ }
720
+
721
+
722
+ void stats_log_stack_pop (struct ParseState * parser , struct Value * Var )
723
+ {
724
+ if (parser -> pc -> CollectStats && (parser -> Mode == RunModeRun ) && (strcmp (parser -> FileName , "startup" ) != 0 )) {
725
+ int Size = Var -> Typ -> Sizeof ;
726
+
727
+ StackFrameAllocations [StackFramesDepth ].TotalAllocation -= Size ;
728
+ StackFrameAllocations [StackFramesDepth ].CumulativeTotalAllocation -= Size ;
729
+ if (Var -> IsLValue ) {
730
+ StackFrameAllocations [StackFramesDepth ].LvalueAllocation -= Size ;
731
+ StackFrameAllocations [StackFramesDepth ].CumulativeLvalueAllocation -= Size ;
732
+ }
733
+
734
+ if (parser -> pc -> PrintMemory ) {
735
+ for (int i = 0 ; i < StackFramesDepth ; i ++ )
736
+ fprintf (stderr , " " );
737
+ fprintf (stderr , "%s:%d:%d Popped %d bytes off stack (total %d/%d) %s\n" ,
738
+ parser -> FileName , parser -> Line , parser -> CharacterPos , Size ,
739
+ StackFrameAllocations [StackFramesDepth ].TotalAllocation ,
740
+ StackFrameAllocations [StackFramesDepth ].CumulativeTotalAllocation ,
741
+ Var -> IsLValue ? "(lvalue)" : "" );
742
+ }
743
+ }
744
+ }
745
+
746
+
747
+ void stats_log_variable_definition (struct ParseState * parser , char * Ident , struct ValueType * Typ )
748
+ {
749
+ if (parser -> pc -> CollectStats ) {
750
+ if (parser -> pc -> PrintMemory ) {
751
+ for (int i = 0 ; i < StackFramesDepth ; i ++ )
752
+ fprintf (stderr , " " );
753
+ fprintf (stderr , "%s:%d:%d Defining variable '%s' of size %d bytes...\n" ,
754
+ parser -> FileName , parser -> Line , parser -> CharacterPos , Ident , Typ -> Sizeof );
652
755
}
653
756
}
654
757
}
@@ -914,3 +1017,24 @@ void stats_print_expression_chains(void)
914
1017
915
1018
printf ("\n" );
916
1019
}
1020
+
1021
+
1022
+ void stats_print_stack_info (void )
1023
+ {
1024
+ printf ("Maximum stack frame depth: %d\n" , StackFramesMaxDepth );
1025
+ printf ("Maximum individual stack frame size: %d bytes\n" , MaxStackFrameTotalAllocation );
1026
+ printf ("Maximum cumulative stack frame size: %d bytes\n" , MaxCumulativeTotalAllocation );
1027
+ printf ("Maximum individual stack frame size (lvalues only): %d bytes\n" , MaxStackFrameLvalueAllocation );
1028
+ printf ("Maximum cumulative stack frame size (lvalues only): %d bytes\n" , MaxCumulativeLvalueAllocation );
1029
+ }
1030
+
1031
+
1032
+ void stats_print_stack_info_csv (void )
1033
+ {
1034
+ printf ("%d,%d,%d,%d,%d\n" ,
1035
+ StackFramesMaxDepth ,
1036
+ MaxStackFrameTotalAllocation ,
1037
+ MaxCumulativeTotalAllocation ,
1038
+ MaxStackFrameLvalueAllocation ,
1039
+ MaxCumulativeLvalueAllocation );
1040
+ }
0 commit comments