axe_cflow_graph.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * axe_cflow_graph.h
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #ifndef _AXE_CFLOW_GRAPH_H
  10. #define _AXE_CFLOW_GRAPH_H
  11. #include <stdio.h>
  12. #include "axe_struct.h"
  13. #include "collections.h"
  14. extern int cflow_errorcode;
  15. /* a variable of the intermediate code */
  16. typedef struct t_cflow_var
  17. {
  18. int ID; /* variable identifier */
  19. } t_cflow_var;
  20. /* A Node exists only in a basic block. It defines a list of
  21. * def-uses and it is associated with a specific instruction
  22. * inside the code */
  23. typedef struct t_cflow_Node
  24. {
  25. t_cflow_var *def; /* A variable that is defined by this node */
  26. t_cflow_var *uses[3]; /* set of variables that will be used by this node */
  27. t_axe_instruction *instr; /* a pointer to the instruction associated
  28. * with this node */
  29. t_list *in; /* variables that are live-in the current node */
  30. t_list *out; /* variables that are live-out the current node */
  31. } t_cflow_Node;
  32. /* an ordered list of nodes with only one predecessor and one successor */
  33. typedef struct t_basic_block
  34. {
  35. t_list *pred; /* predecessors : a list of basic blocks */
  36. t_list *succ; /* successors : a list of basic blocks */
  37. t_list *nodes; /* an ordered list of instructions */
  38. } t_basic_block;
  39. /* a control flow graph */
  40. typedef struct t_cflow_Graph
  41. {
  42. t_basic_block *startingBlock; /* the starting basic block of code */
  43. t_basic_block *endingBlock; /* the last block of the graph */
  44. t_list *blocks; /* an ordered list of all the basic blocks */
  45. t_list *cflow_variables; /* a list of all the variable identifiers */
  46. } t_cflow_Graph;
  47. /* functions that are used in order to allocate/deallocate instances
  48. * of basic blocks, instruction nodes and flow graphs */
  49. extern t_cflow_Node * allocNode
  50. (t_cflow_Graph *graph, t_axe_instruction *instr);
  51. extern void finalizeNode(t_cflow_Node *node);
  52. extern t_basic_block * allocBasicBlock();
  53. extern void finalizeBasicBlock(t_basic_block *block);
  54. extern t_cflow_Graph * allocGraph();
  55. extern void finalizeGraph(t_cflow_Graph *graph);
  56. /* working with basic blocks */
  57. extern void setPred(t_basic_block *block, t_basic_block *pred);
  58. extern void setSucc(t_basic_block *block, t_basic_block *succ);
  59. extern void insertNode(t_basic_block *block, t_cflow_Node *node);
  60. extern void insertNodeBefore(t_basic_block *block
  61. , t_cflow_Node *before_node, t_cflow_Node *new_node);
  62. extern void insertNodeAfter(t_basic_block *block
  63. , t_cflow_Node *after_node, t_cflow_Node *new_node);
  64. extern t_list * getLiveINVars(t_basic_block *bblock);
  65. extern t_list * getLiveOUTVars(t_basic_block *bblock);
  66. /* working with the control flow graph */
  67. extern void insertBlock(t_cflow_Graph *graph, t_basic_block *block);
  68. extern t_cflow_Graph * createFlowGraph(t_list *instructions);
  69. /* dataflow analysis */
  70. extern void performLivenessAnalysis(t_cflow_Graph *graph);
  71. #endif