compiler.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict"
  2. var createThunk = require("./lib/thunk.js")
  3. function Procedure() {
  4. this.argTypes = []
  5. this.shimArgs = []
  6. this.arrayArgs = []
  7. this.arrayBlockIndices = []
  8. this.scalarArgs = []
  9. this.offsetArgs = []
  10. this.offsetArgIndex = []
  11. this.indexArgs = []
  12. this.shapeArgs = []
  13. this.funcName = ""
  14. this.pre = null
  15. this.body = null
  16. this.post = null
  17. this.debug = false
  18. }
  19. function compileCwise(user_args) {
  20. //Create procedure
  21. var proc = new Procedure()
  22. //Parse blocks
  23. proc.pre = user_args.pre
  24. proc.body = user_args.body
  25. proc.post = user_args.post
  26. //Parse arguments
  27. var proc_args = user_args.args.slice(0)
  28. proc.argTypes = proc_args
  29. for(var i=0; i<proc_args.length; ++i) {
  30. var arg_type = proc_args[i]
  31. if(arg_type === "array" || (typeof arg_type === "object" && arg_type.blockIndices)) {
  32. proc.argTypes[i] = "array"
  33. proc.arrayArgs.push(i)
  34. proc.arrayBlockIndices.push(arg_type.blockIndices ? arg_type.blockIndices : 0)
  35. proc.shimArgs.push("array" + i)
  36. if(i < proc.pre.args.length && proc.pre.args[i].count>0) {
  37. throw new Error("cwise: pre() block may not reference array args")
  38. }
  39. if(i < proc.post.args.length && proc.post.args[i].count>0) {
  40. throw new Error("cwise: post() block may not reference array args")
  41. }
  42. } else if(arg_type === "scalar") {
  43. proc.scalarArgs.push(i)
  44. proc.shimArgs.push("scalar" + i)
  45. } else if(arg_type === "index") {
  46. proc.indexArgs.push(i)
  47. if(i < proc.pre.args.length && proc.pre.args[i].count > 0) {
  48. throw new Error("cwise: pre() block may not reference array index")
  49. }
  50. if(i < proc.body.args.length && proc.body.args[i].lvalue) {
  51. throw new Error("cwise: body() block may not write to array index")
  52. }
  53. if(i < proc.post.args.length && proc.post.args[i].count > 0) {
  54. throw new Error("cwise: post() block may not reference array index")
  55. }
  56. } else if(arg_type === "shape") {
  57. proc.shapeArgs.push(i)
  58. if(i < proc.pre.args.length && proc.pre.args[i].lvalue) {
  59. throw new Error("cwise: pre() block may not write to array shape")
  60. }
  61. if(i < proc.body.args.length && proc.body.args[i].lvalue) {
  62. throw new Error("cwise: body() block may not write to array shape")
  63. }
  64. if(i < proc.post.args.length && proc.post.args[i].lvalue) {
  65. throw new Error("cwise: post() block may not write to array shape")
  66. }
  67. } else if(typeof arg_type === "object" && arg_type.offset) {
  68. proc.argTypes[i] = "offset"
  69. proc.offsetArgs.push({ array: arg_type.array, offset:arg_type.offset })
  70. proc.offsetArgIndex.push(i)
  71. } else {
  72. throw new Error("cwise: Unknown argument type " + proc_args[i])
  73. }
  74. }
  75. //Make sure at least one array argument was specified
  76. if(proc.arrayArgs.length <= 0) {
  77. throw new Error("cwise: No array arguments specified")
  78. }
  79. //Make sure arguments are correct
  80. if(proc.pre.args.length > proc_args.length) {
  81. throw new Error("cwise: Too many arguments in pre() block")
  82. }
  83. if(proc.body.args.length > proc_args.length) {
  84. throw new Error("cwise: Too many arguments in body() block")
  85. }
  86. if(proc.post.args.length > proc_args.length) {
  87. throw new Error("cwise: Too many arguments in post() block")
  88. }
  89. //Check debug flag
  90. proc.debug = !!user_args.printCode || !!user_args.debug
  91. //Retrieve name
  92. proc.funcName = user_args.funcName || "cwise"
  93. //Read in block size
  94. proc.blockSize = user_args.blockSize || 64
  95. return createThunk(proc)
  96. }
  97. module.exports = compileCwise