bootstrap.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. ### Configuration for creating the bootstrap model using conformance_bottom.
  2. import glob
  3. import hashlib
  4. import tempfile
  5. import gzip
  6. def bootstrap():
  7. root = ["__hierarchy"]
  8. task_manager = "task_manager"
  9. initial_code_manager = "bootstrap/initial_code_manager.alb"
  10. initial_code_task = "bootstrap/initial_code_task.alb"
  11. bootstrap_files = glob.glob("bootstrap/*.alc") + [initial_code_manager, initial_code_task]
  12. task_data = [ "input",
  13. "output",
  14. "globals",
  15. "frame",
  16. ]
  17. task_frame = [ "evalstack",
  18. "symbols",
  19. "returnvalue",
  20. ]
  21. primitives = { "integer_addition": ["Integer", "Integer", "Integer"],
  22. "integer_subtraction": ["Integer", "Integer", "Integer"],
  23. "integer_multiplication": ["Integer", "Integer", "Integer"],
  24. "integer_division": ["Integer", "Integer", "Integer"],
  25. "integer_gt": ["Boolean", "Integer", "Integer"],
  26. "integer_lt": ["Boolean", "Integer", "Integer"],
  27. "integer_neg": ["Integer", "Integer"],
  28. "float_addition": ["Float", "Float", "Float"],
  29. "float_subtraction": ["Float", "Float", "Float"],
  30. "float_multiplication": ["Float", "Float", "Float"],
  31. "float_division": ["Float", "Float", "Float"],
  32. "float_gt": ["Boolean", "Float", "Float"],
  33. "float_lt": ["Boolean", "Float", "Float"],
  34. "float_neg": ["Float", "Float"],
  35. "bool_and": ["Boolean", "Boolean", "Boolean"],
  36. "bool_or": ["Boolean", "Boolean", "Boolean"],
  37. "bool_not": ["Boolean", "Boolean"],
  38. "string_join": ["String", "String", "String"],
  39. "string_get": ["String", "String", "Integer"],
  40. "string_len": ["Integer", "String"],
  41. "string_split": ["Element", "String", "String"],
  42. "value_eq": ["Boolean", "Element", "Element"],
  43. "value_neq": ["Boolean", "Element", "Element"],
  44. "cast_i2f": ["Float", "Integer"],
  45. "cast_i2s": ["String", "Integer"],
  46. "cast_i2b": ["Boolean", "Integer"],
  47. "cast_f2i": ["Integer", "Float"],
  48. "cast_f2b": ["Boolean", "Float"],
  49. "cast_f2s": ["String", "Float"],
  50. "cast_s2i": ["Integer", "String"],
  51. "cast_s2f": ["Float", "String"],
  52. "cast_s2b": ["Boolean", "String"],
  53. "cast_b2i": ["Integer", "Boolean"],
  54. "cast_b2f": ["Float", "Boolean"],
  55. "cast_b2s": ["String", "Boolean"],
  56. "cast_e2s": ["String", "Element"],
  57. "cast_a2s": ["String", "Action"],
  58. "cast_v2s": ["String", "Element"],
  59. "cast_id2s": ["String", "Element"],
  60. "list_read": ["Element", "Element", "Integer"],
  61. "list_append": ["Element", "Element", "Element"],
  62. "list_insert": ["Element", "Element", "Integer", "Element"],
  63. "list_delete": ["Element", "Element", "Integer"],
  64. "list_len": ["Integer", "Element"],
  65. "dict_add": ["Element", "Element", "Element", "Element"],
  66. "dict_delete": ["Element", "Element", "Element"],
  67. "dict_delete_node": ["Element", "Element", "Element"],
  68. "dict_read": ["Element", "Element", "Element"],
  69. "dict_read_edge": ["Element", "Element", "Element"],
  70. "dict_read_node": ["Element", "Element", "Element"],
  71. "dict_len": ["Integer", "Element"],
  72. "dict_in": ["Boolean", "Element", "Element"],
  73. "dict_in_node": ["Boolean", "Element", "Element"],
  74. "dict_keys": ["Element", "Element"],
  75. "set_add": ["Element", "Element", "Element"],
  76. "set_pop": ["Element", "Element"],
  77. "set_remove": ["Element", "Element", "Element"],
  78. "set_remove_node": ["Element", "Element", "Element"],
  79. "set_in": ["Boolean", "Element", "Element"],
  80. "set_in_node": ["Boolean", "Element", "Element"],
  81. "is_physical_int": ["Boolean", "Element"],
  82. "is_physical_boolean": ["Boolean", "Element"],
  83. "is_physical_string": ["Boolean", "Element"],
  84. "is_physical_action": ["Boolean", "Element"],
  85. "is_physical_float": ["Boolean", "Element"],
  86. "create_node": ["Element"],
  87. "create_edge": ["Element", "Element", "Element"],
  88. "create_value": ["Element", "Element"],
  89. "is_edge": ["Boolean", "Element"],
  90. "read_nr_out": ["Integer", "Element"],
  91. "read_out": ["Element", "Element", "Integer"],
  92. "read_nr_in": ["Integer", "Element"],
  93. "read_in": ["Element", "Element", "Integer"],
  94. "read_edge_src": ["Element", "Element"],
  95. "read_edge_dst": ["Element", "Element"],
  96. "delete_element": ["Element", "Element"],
  97. "element_eq": ["Boolean", "Element", "Element"],
  98. "element_neq": ["Boolean", "Element", "Element"],
  99. "read_root": ["Element"],
  100. "read_taskroot": ["Element"],
  101. "deserialize": ["Element", "String"],
  102. "log": ["String", "String"],
  103. "time": ["Float"],
  104. "hash": ["String", "String"],
  105. }
  106. jit_primitives = {
  107. "get_jit_enabled": ["Boolean"],
  108. "set_jit_enabled": ["Void", "Boolean"]
  109. }
  110. ### Actual script to generate the file
  111. import os
  112. import sys
  113. class Writer(object):
  114. def __init__(self, file_a, file_b):
  115. self.file_a = file_a
  116. self.file_b = file_b
  117. def write(self, text, both=True):
  118. self.file_a.write(text)
  119. if both:
  120. self.file_b.write(text)
  121. try:
  122. with gzip.GzipFile("bootstrap/bootstrap.m.gz", "wb", mtime=0) as fa:
  123. with gzip.GzipFile("bootstrap/minimal.m.gz", "wb", mtime=0) as fb:
  124. f = Writer(fa, fb)
  125. # Create the root first
  126. f.write("Node root()\n")
  127. # Create all children of the root
  128. for node in root:
  129. f.write("Node %s()\n" % node)
  130. f.write("Edge _%s(root, %s)\n" % (node, node))
  131. f.write('Node __%s("%s")\n' % (node, node))
  132. f.write("Edge ___%s(_%s, __%s)\n" % (node, node, node))
  133. def declare_primitive_class(primitive_class_name, primitive_decls):
  134. f.write("Node %s()\n" % primitive_class_name)
  135. f.write("Edge _%s(__hierarchy, %s)\n" % (primitive_class_name, primitive_class_name))
  136. f.write('Node __%s("%s")\n' % (primitive_class_name, primitive_class_name))
  137. f.write("Edge ___%s(_%s, __%s)\n" % (primitive_class_name, primitive_class_name, primitive_class_name))
  138. # Define all primitive functions
  139. for function, parameters in primitive_decls.iteritems():
  140. f.write("Node _func_signature_%s()\n" % function)
  141. f.write("Node _func_params_%s()\n" % function)
  142. f.write("Node _func_body_%s()\n" % function)
  143. f.write("Edge _%s_%s(%s, _func_signature_%s)\n" % (primitive_class_name, function, primitive_class_name, function))
  144. f.write('Node _name_%s("%s")\n' % (function, function))
  145. f.write("Edge _%s_name_%s(_%s_%s, _name_%s)\n" % (primitive_class_name, function, primitive_class_name, function, function))
  146. f.write('Node _body_%s("body")\n' % function)
  147. f.write("Edge _signature_body_%s(_func_signature_%s, _func_body_%s)\n" % (function, function, function))
  148. f.write("Edge _signature_body_str_%s(_signature_body_%s, _body_%s)\n" % (function, function, function))
  149. f.write('Node _params_%s("params")\n' % function)
  150. f.write("Edge _signature_params_%s(_func_signature_%s, _func_params_%s)\n" % (function, function, function))
  151. f.write("Edge _signature_params_str_%s(_signature_params_%s, _params_%s)\n" % (function, function, function))
  152. parameter_names = "abcdefghijklmnopqrstuvwxyz"
  153. for number, param in enumerate(parameters[1:]):
  154. param_encoding = "%s_%s" % (function, parameter_names[number])
  155. f.write("Node _func_params_%s()\n" % (param_encoding))
  156. f.write('Node _name_%s("%s")\n' % (param_encoding, parameter_names[number]))
  157. f.write("Edge _param_link_%s(_func_params_%s, _func_params_%s)\n" % (param_encoding, function, param_encoding))
  158. f.write("Edge _param_link_str_%s(_param_link_%s, _name_%s)\n" % (param_encoding, param_encoding, param_encoding))
  159. f.write('Node _name_str_%s("name")\n' % param_encoding)
  160. f.write("Edge _param_name_%s(_func_params_%s, _name_%s)\n" % (param_encoding, param_encoding, param_encoding))
  161. f.write("Edge _param_name_str_%s(_param_name_%s, _name_str_%s)\n" % (param_encoding, param_encoding, param_encoding))
  162. declare_primitive_class('primitives', primitives)
  163. declare_primitive_class('jit', jit_primitives)
  164. # Create the initial task
  165. f.write("Node task_root()\n")
  166. for data in task_data:
  167. f.write("Node task_%s()\n" % data)
  168. f.write('Node ___task_%s("%s")\n' % (data, data))
  169. f.write("Edge _task_%s(task_root, task_%s)\n" % (data, data))
  170. f.write("Edge __task_%s(_task_%s, ___task_%s)\n" % (data, data, data))
  171. for data in task_frame:
  172. f.write("Node task_%s()\n" % data)
  173. f.write('Node ___task_%s("%s")\n' % (data, data))
  174. f.write("Edge _task_%s(task_frame, task_%s)\n" % (data, data))
  175. f.write("Edge __task_%s(_task_%s, ___task_%s)\n" % (data, data, data))
  176. # Add last_input and last_output links
  177. for data in ["input", "output"]:
  178. f.write('Node ___task_last_%s("last_%s")\n' % (data, data))
  179. f.write("Edge _task_last_%s(task_root, task_%s)\n" % (data, data))
  180. f.write("Edge __task_last_%s(_task_last_%s, ___task_last_%s)\n" % (data, data, data))
  181. # Bind task to the root
  182. f.write('Node ___new_task("%s")\n' % task_manager)
  183. f.write("Edge _new_task(root, task_root)\n")
  184. f.write("Edge __new_task(_new_task, ___new_task)\n")
  185. def compile_code_AL(filename, target, prepend="", main=False, symbols=None):
  186. import sys
  187. sys.path.append("interface/HUTN/")
  188. from hutn_compiler.compiler import main as compile_code
  189. code = compile_code(filename, "interface/HUTN/grammars/actionlanguage.g", "BS", ["--debug", "--prepend:%s" % prepend, "--main" if main else "--not-main"], symbols=symbols)
  190. return code.replace("auto_initial_IP", target)
  191. # Create all library code
  192. # But first create the structure to hold compiled data
  193. f.write("Node __objects()\n", both=False)
  194. f.write('Node __objects_name("objects")\n', both=False)
  195. f.write("Edge __obj_link(__hierarchy, __objects)\n", both=False)
  196. f.write("Edge _name_obj_link(__obj_link, __objects_name)\n", both=False)
  197. # Compile all files and add to structure manually
  198. for bootstrap_file in bootstrap_files:
  199. # Compile the subfile
  200. bootstrap_file = bootstrap_file.replace("\\", "/")
  201. print("[COMP] %s" % bootstrap_file)
  202. symbols = {}
  203. f.write(compile_code_AL(bootstrap_file, "initial_IP", prepend=bootstrap_file, symbols=symbols, main = bootstrap_file in [initial_code_manager, initial_code_task]), both=False)
  204. # Now link the code with the compilation manager structure
  205. f.write("Node elem()\n", both=False)
  206. f.write('Node initializers("initializers")\n', both=False)
  207. f.write('Node hash("hash_md5")\n', both=False)
  208. f.write("Edge _(__objects, elem)\n", both=False)
  209. f.write('Node filename("%s")\n' % bootstrap_file, both=False)
  210. f.write("Edge _(_, filename)\n", both=False)
  211. f.write("Edge _(elem, %s_initial_IP)\n" % bootstrap_file, both=False)
  212. f.write("Edge _(_, initializers)\n", both=False)
  213. md5 = hashlib.md5()
  214. md5.update(open(bootstrap_file, 'r').read())
  215. f.write('Node hash_value("%s")\n' % md5.hexdigest(), both=False)
  216. f.write("Edge _(elem, hash_value)\n", both=False)
  217. f.write("Edge _(_, hash)\n", both=False)
  218. f.write('Node symbols("symbols")\n', both=False)
  219. f.write('Node __symbols()\n', both=False)
  220. f.write('Edge _(elem, __symbols)\n', both=False)
  221. f.write('Edge _(_, symbols)\n', both=False)
  222. for k, v in symbols.items():
  223. f.write('Node v(%s)\n' % v, both=False)
  224. f.write('Node k("%s")\n' % k, both=False)
  225. f.write('Edge _(__symbols, v)\n', both=False)
  226. f.write('Edge _(_, k)\n', both=False)
  227. # Create code for initial task
  228. print("[BOOT] task_manager")
  229. f.write('Node _IP_str("IP")\n', both=False)
  230. f.write("Edge _task_frame(task_frame, %s_initial_IP)\n" % initial_code_manager, both=False)
  231. f.write("Edge __task_frame(_task_frame, _IP_str)\n", both=False)
  232. f.write('Node __phase("init")\n', both=False)
  233. f.write('Node __phase_str("phase")\n', both=False)
  234. f.write("Edge _task_phase(task_frame, __phase)\n", both=False)
  235. f.write("Edge __task_phase(_task_phase, __phase_str)\n", both=False)
  236. # Create code for new tasks to start at
  237. print("[BOOT] new_task")
  238. f.write('Node __IP_str("__IP")\n', both=False)
  239. f.write("Edge _task_IP(__hierarchy, %s_initial_IP)\n" % initial_code_task, both=False)
  240. f.write("Edge __task_IP(_task_IP, __IP_str)\n", both=False)
  241. except:
  242. os.remove("bootstrap/bootstrap.m.gz")
  243. os.remove("bootstrap/minimal.m.gz")
  244. raise