@tsplc/compiler 0.5.17 → 0.5.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/clock.js.map CHANGED
@@ -3,9 +3,9 @@
3
3
  "sources": ["../stdlib/plc-time.ts", "../stdlib/clock.ts"],
4
4
  "sourcesContent": [
5
5
  "/** Milliseconds since the process started, as the PLC's `TIME()` counts them in a UDINT, which telemetry's `time` carries. */\nexport const plcTime = (): number => Math.floor(performance.now()) >>> 0;\n",
6
- "import { plcTime } from \"./plc-time\";\n\n/** Milliseconds since the process started, as the PLC's `now()` counts them in a DINT; used when a program runs under Bun. */\nexport const now = (): number => plcTime() | 0;\n"
6
+ "import { plcTime } from \"./plc-time\";\n\n/**\n * Milliseconds since the process started, as the PLC's `now()` counts them in a DINT; used when a\n * program runs under Bun. Each call reads the clock: holding one time for a scan is the Bun\n * runtime's to do, as only it knows when a scan runs.\n */\nexport const now = (): number => plcTime() | 0;\n"
7
7
  ],
8
- "mappings": ";AACO,IAAM,UAAU,MAAc,KAAK,MAAM,YAAY,IAAI,CAAC,MAAM;;;ACEhE,IAAM,MAAM,MAAc,QAAQ,IAAI;",
8
+ "mappings": ";AACO,IAAM,UAAU,MAAc,KAAK,MAAM,YAAY,IAAI,CAAC,MAAM;;;ACMhE,IAAM,MAAM,MAAc,QAAQ,IAAI;",
9
9
  "debugId": "6EA220CF1886449764756E2164756E21",
10
10
  "names": []
11
11
  }
package/dist/codesys.js CHANGED
@@ -105,7 +105,6 @@ import {
105
105
  statementExpressions,
106
106
  subexpressions
107
107
  } from "@tsplc/compiler/ir";
108
- import { scanClockType } from "@tsplc/debug/manifest";
109
108
  import {
110
109
  floatMaximum,
111
110
  isElementary
@@ -139,7 +138,6 @@ var operatorTokens = {
139
138
  bitXor: "XOR"
140
139
  };
141
140
  var isInfix = (operator) => Object.hasOwn(operatorTokens, operator);
142
- var scanClock = `TO_${scanClockType}(TIME_TO_UDINT(TIME()))`;
143
141
  function scalarLiteral(value, type) {
144
142
  if (value.kind === "bool") {
145
143
  return value.value ? "TRUE" : "FALSE";
@@ -157,11 +155,13 @@ function scalarLiteral(value, type) {
157
155
  class NumericLowering {
158
156
  types;
159
157
  allocate;
158
+ scanClock;
160
159
  objects = [];
161
160
  helpers = new Map;
162
- constructor(types, allocate) {
161
+ constructor(types, allocate, scanClock) {
163
162
  this.types = types;
164
163
  this.allocate = allocate;
164
+ this.scanClock = scanClock;
165
165
  }
166
166
  prepare(value) {
167
167
  for (const operand of subexpressions(value)) {
@@ -197,7 +197,7 @@ class NumericLowering {
197
197
  return `TIME_TO_UDINT(${operands[0]})`;
198
198
  }
199
199
  if (value.operator === "clock") {
200
- return scanClock;
200
+ return required(this.scanClock);
201
201
  }
202
202
  if (isElementary(value.operator)) {
203
203
  return `${elementaryNames[value.operator]}(${operands[0]})`;
@@ -1371,6 +1371,55 @@ import {
1371
1371
  coreTables as coreTables8
1372
1372
  } from "@tsplc/compiler/ir";
1373
1373
 
1374
+ // packages/compiler/src/codesys/scan-clock.ts
1375
+ import {
1376
+ nestedBodies as nestedBodies2,
1377
+ statementExpressions as statementExpressions2,
1378
+ subexpressions as subexpressions2
1379
+ } from "@tsplc/compiler/ir";
1380
+ import { scanClockType } from "@tsplc/debug/manifest";
1381
+ var runtimeClock = `TO_${scanClockType}(TIME_TO_UDINT(TIME()))`;
1382
+ var readsClock = (value) => value.kind === "unary" && value.operator === "clock" || subexpressions2(value).some(readsClock);
1383
+ var anyReadsClock = (items) => items.some((item) => statementExpressions2(item).some(readsClock) || nestedBodies2(item).some(anyReadsClock));
1384
+ function planScanClock(core, tasks, objectNames) {
1385
+ if (![...core.functions, ...core.blocks].some((routine) => anyReadsClock(routine.body))) {
1386
+ return;
1387
+ }
1388
+ const list = objectNames.allocateObject("GVL", "TsplcScan");
1389
+ const fields = new Names;
1390
+ const now = fields.allocate("clock");
1391
+ return {
1392
+ list,
1393
+ now: `${list}.${now}`,
1394
+ interrupted: new Map(tasks.length > 1 ? tasks.map((task) => [
1395
+ task.name,
1396
+ `${list}.${fields.allocate(`interrupted_${task.name}`)}`
1397
+ ]) : [])
1398
+ };
1399
+ }
1400
+ var field = (qualified) => qualified.slice(qualified.indexOf(".") + 1);
1401
+ var emitScanClock = (clock) => ({
1402
+ id: "tsplc:scanClock",
1403
+ kind: "globalList",
1404
+ name: clock.list,
1405
+ declaration: `{attribute 'qualified_only'}
1406
+ VAR_GLOBAL
1407
+ ${[clock.now, ...clock.interrupted.values()].map((name) => ` ${field(name)} : ${scanClockType};
1408
+ `).join("")}END_VAR
1409
+ `
1410
+ });
1411
+ var latchScanClock = (clock, task) => {
1412
+ const interrupted = clock.interrupted.get(task);
1413
+ return [
1414
+ ...interrupted ? [`${interrupted} := ${clock.now};`] : [],
1415
+ `${clock.now} := ${runtimeClock};`
1416
+ ];
1417
+ };
1418
+ var restoreScanClock = (clock, task) => {
1419
+ const interrupted = clock.interrupted.get(task);
1420
+ return interrupted ? [`${clock.now} := ${interrupted};`] : [];
1421
+ };
1422
+
1374
1423
  // packages/compiler/src/codesys/statements.ts
1375
1424
  import {
1376
1425
  coreTables as coreTables7,
@@ -1457,7 +1506,7 @@ import {
1457
1506
  instancedBlock as instancedBlock3,
1458
1507
  isBlockRoutine,
1459
1508
  isTemporary,
1460
- nestedBodies as nestedBodies3
1509
+ nestedBodies as nestedBodies4
1461
1510
  } from "@tsplc/compiler/ir";
1462
1511
  import { neverWrittenSlot } from "@tsplc/debug/manifest";
1463
1512
 
@@ -1546,9 +1595,9 @@ function storageLayout(core) {
1546
1595
  // packages/compiler/src/codesys/task-access.ts
1547
1596
  import {
1548
1597
  coreTables as coreTables2,
1549
- nestedBodies as nestedBodies2,
1550
- statementExpressions as statementExpressions2,
1551
- subexpressions as subexpressions2
1598
+ nestedBodies as nestedBodies3,
1599
+ statementExpressions as statementExpressions3,
1600
+ subexpressions as subexpressions3
1552
1601
  } from "@tsplc/compiler/ir";
1553
1602
  function routineTasks(core, deployment) {
1554
1603
  const tasks = new Map;
@@ -1573,7 +1622,7 @@ function routineTasks(core, deployment) {
1573
1622
  if (called) {
1574
1623
  visit(called);
1575
1624
  }
1576
- for (const body of nestedBodies2(item)) {
1625
+ for (const body of nestedBodies3(item)) {
1577
1626
  inspect(body);
1578
1627
  }
1579
1628
  }
@@ -1627,7 +1676,7 @@ function storageAccess(fn, core, scheduledHost) {
1627
1676
  if (value.kind === "borrow") {
1628
1677
  result.add(value.place.root);
1629
1678
  }
1630
- subexpressions2(value).forEach(expression);
1679
+ subexpressions3(value).forEach(expression);
1631
1680
  };
1632
1681
  const body = (items) => {
1633
1682
  for (const item of items) {
@@ -1640,7 +1689,7 @@ function storageAccess(fn, core, scheduledHost) {
1640
1689
  if (item.kind === "blockUpdate") {
1641
1690
  result.add(item.instance);
1642
1691
  }
1643
- statementExpressions2(item).forEach(expression);
1692
+ statementExpressions3(item).forEach(expression);
1644
1693
  if (item.kind === "for") {
1645
1694
  result.add(item.counter);
1646
1695
  }
@@ -1652,7 +1701,7 @@ function storageAccess(fn, core, scheduledHost) {
1652
1701
  seen.add(called.id);
1653
1702
  body(called.body);
1654
1703
  }
1655
- nestedBodies2(item).forEach(body);
1704
+ nestedBodies3(item).forEach(body);
1656
1705
  }
1657
1706
  };
1658
1707
  seen.add(fn.id);
@@ -1821,7 +1870,7 @@ class DebugAnalysis {
1821
1870
  if (item.kind === "invoke") {
1822
1871
  this.callSites.set(item.function, (this.callSites.get(item.function) ?? 0) + weight);
1823
1872
  }
1824
- for (const body of nestedBodies3(item)) {
1873
+ for (const body of nestedBodies4(item)) {
1825
1874
  this.countCalls(body, item.kind === "for" ? callsInLoop : weight);
1826
1875
  }
1827
1876
  }
@@ -1850,7 +1899,7 @@ class DebugAnalysis {
1850
1899
  result.add(root);
1851
1900
  }
1852
1901
  }
1853
- for (const body of nestedBodies3(item)) {
1902
+ for (const body of nestedBodies4(item)) {
1854
1903
  for (const root of this.statementWrites(body)) {
1855
1904
  result.add(root);
1856
1905
  }
@@ -1871,7 +1920,7 @@ class DebugAnalysis {
1871
1920
  this.walk(settings, routine, item.else, here, guard([item.condition]));
1872
1921
  }
1873
1922
  if (item.kind === "switch") {
1874
- for (const body of nestedBodies3(item)) {
1923
+ for (const body of nestedBodies4(item)) {
1875
1924
  this.walk(settings, routine, body, here, guard([item.value]));
1876
1925
  }
1877
1926
  }
@@ -1992,7 +2041,7 @@ class DebugAnalysis {
1992
2041
  if (target && this.isTemporary(routine, target)) {
1993
2042
  definitions.set(target, [...definitions.get(target) ?? [], item]);
1994
2043
  }
1995
- for (const body of nestedBodies3(item)) {
2044
+ for (const body of nestedBodies4(item)) {
1996
2045
  define(body);
1997
2046
  }
1998
2047
  }
@@ -2189,10 +2238,10 @@ import {
2189
2238
  instanceFields,
2190
2239
  isBlockRoutine as isBlockRoutine2,
2191
2240
  jobErrors as jobErrors2,
2192
- nestedBodies as nestedBodies5,
2241
+ nestedBodies as nestedBodies6,
2193
2242
  standardBlocks,
2194
- statementExpressions as statementExpressions3,
2195
- subexpressions as subexpressions3
2243
+ statementExpressions as statementExpressions4,
2244
+ subexpressions as subexpressions4
2196
2245
  } from "@tsplc/compiler/ir";
2197
2246
  import { encodeConstant } from "@tsplc/debug/constant";
2198
2247
  import {
@@ -2206,7 +2255,7 @@ import {
2206
2255
  modbusServerName
2207
2256
  } from "@tsplc/typegen/hardware";
2208
2257
  // packages/compiler/package.json
2209
- var version = "0.5.17";
2258
+ var version = "0.5.19";
2210
2259
 
2211
2260
  // packages/compiler/src/codesys/global-lists.ts
2212
2261
  import {
@@ -2398,7 +2447,7 @@ import {
2398
2447
  httpErrors as httpErrors2,
2399
2448
  instancedBlock as instancedBlock5,
2400
2449
  jobErrors,
2401
- nestedBodies as nestedBodies4
2450
+ nestedBodies as nestedBodies5
2402
2451
  } from "@tsplc/compiler/ir";
2403
2452
  import {
2404
2453
  serverBaseUrl
@@ -2668,7 +2717,7 @@ function telemetryCalls(core) {
2668
2717
  if (item.kind === "telemetry") {
2669
2718
  statements.push({ routine, call: item });
2670
2719
  }
2671
- for (const body of nestedBodies4(item)) {
2720
+ for (const body of nestedBodies5(item)) {
2672
2721
  find(routine, body);
2673
2722
  }
2674
2723
  }
@@ -3357,7 +3406,7 @@ function reads(variables, body, sources) {
3357
3406
  if (value.kind === "borrow") {
3358
3407
  return;
3359
3408
  }
3360
- subexpressions3(value).forEach(expression);
3409
+ subexpressions4(value).forEach(expression);
3361
3410
  if (value.kind !== "load") {
3362
3411
  return;
3363
3412
  }
@@ -3369,8 +3418,8 @@ function reads(variables, body, sources) {
3369
3418
  };
3370
3419
  const statements = (items) => {
3371
3420
  for (const item of items) {
3372
- statementExpressions3(item).forEach(expression);
3373
- nestedBodies5(item).forEach(statements);
3421
+ statementExpressions4(item).forEach(expression);
3422
+ nestedBodies6(item).forEach(statements);
3374
3423
  }
3375
3424
  };
3376
3425
  statements(body);
@@ -4148,8 +4197,11 @@ function emitScanProgram({ plan, symbols, instrumentation }, ioChannels, program
4148
4197
  const ring = task && instrumentation.rings.get(task.name);
4149
4198
  const clock = task && instrumentation.scanClocks.get(task.name);
4150
4199
  const lines = [
4200
+ ...task && symbols.scanClock ? latchScanClock(symbols.scanClock, task.name) : [],
4151
4201
  ...ring ? [`${instrumentation.global}.${ring}();`] : [],
4152
- ...clock ? [`${instrumentation.global}.${clock} := ${scanClock};`] : [],
4202
+ ...clock ? [
4203
+ `${instrumentation.global}.${clock} := ${symbols.scanClock?.now ?? runtimeClock};`
4204
+ ] : [],
4153
4205
  ...channels.filter((channel) => channel.input).map(({ target, wire, inverted }) => `${target} := ${inverted ? "NOT " : ""}${wire};`),
4154
4206
  stateful ? call : `plc_discard := ${call}`,
4155
4207
  ...channels.filter((channel) => !channel.input).map(({ target, wire }) => `${wire} := ${target};`)
@@ -4193,7 +4245,10 @@ function emitTaskWrappers({ plan, symbols }, objects) {
4193
4245
  return plan.deployment.tasks.map((task) => {
4194
4246
  const { wrapped, wrapper } = taskPrograms(plan, task);
4195
4247
  const name = symbols.names.allocateObject("PRG", wrapper.label);
4196
- const calls = wrapped.map((program) => `${required(programObjects.get(program)).name}();`);
4248
+ const calls = [
4249
+ ...wrapped.map((program) => `${required(programObjects.get(program)).name}();`),
4250
+ ...symbols.scanClock ? restoreScanClock(symbols.scanClock, task.name) : []
4251
+ ];
4197
4252
  return {
4198
4253
  id: wrapper.id,
4199
4254
  kind: "program",
@@ -4294,11 +4349,11 @@ import { anyIndex, patternOf } from "@tsplc/debug/paths";
4294
4349
  // packages/compiler/src/codesys/explain/parameters.ts
4295
4350
  import {
4296
4351
  isBlockRoutine as isBlockRoutine3,
4297
- nestedBodies as nestedBodies6
4352
+ nestedBodies as nestedBodies7
4298
4353
  } from "@tsplc/compiler/ir";
4299
4354
  var parameterKey = (routine, index) => `${routine}#${index}`;
4300
4355
  var parameterIndex = (routine, root) => isBlockRoutine3(routine) ? -1 : routine.parameters.findIndex((parameter) => parameter.id === root);
4301
- var statementsOf = (items) => items.flatMap((item) => [item, ...nestedBodies6(item).flatMap(statementsOf)]);
4356
+ var statementsOf = (items) => items.flatMap((item) => [item, ...nestedBodies7(item).flatMap(statementsOf)]);
4302
4357
  function writtenThroughParameters(routines) {
4303
4358
  const propagate = (written) => {
4304
4359
  const added = routines.flatMap((routine) => statementsOf(routine.body).flatMap((item) => {
@@ -5337,10 +5392,10 @@ import {
5337
5392
  instancedBlock as instancedBlock7,
5338
5393
  isLogicalIo as isLogicalIo5,
5339
5394
  isVerifiedCore,
5340
- nestedBodies as nestedBodies7,
5395
+ nestedBodies as nestedBodies8,
5341
5396
  shareRoutines,
5342
- statementExpressions as statementExpressions4,
5343
- subexpressions as subexpressions4
5397
+ statementExpressions as statementExpressions5,
5398
+ subexpressions as subexpressions5
5344
5399
  } from "@tsplc/compiler/ir";
5345
5400
  import {
5346
5401
  cyclicTasksSchema,
@@ -5833,13 +5888,13 @@ function validateConstants(core, report) {
5833
5888
  if (value.kind === "constant") {
5834
5889
  constant(value.value, value.type, value.span);
5835
5890
  }
5836
- subexpressions4(value).forEach(constantsInExpression);
5891
+ subexpressions5(value).forEach(constantsInExpression);
5837
5892
  };
5838
5893
  const constantsInBody = (items) => {
5839
5894
  for (const item of items) {
5840
- statementExpressions4(item).forEach(constantsInExpression);
5895
+ statementExpressions5(item).forEach(constantsInExpression);
5841
5896
  if (item.kind !== "switch") {
5842
- nestedBodies7(item).forEach(constantsInBody);
5897
+ nestedBodies8(item).forEach(constantsInBody);
5843
5898
  continue;
5844
5899
  }
5845
5900
  for (const branch of item.cases) {
@@ -5930,10 +5985,12 @@ function globalSymbols(plan) {
5930
5985
  const persistent = new Set(core.variables.filter((item) => item.storage !== "constant").map((item) => item.id));
5931
5986
  const programRoutines = new Set(core.functions.filter((fn) => !fn.owner && [...accessedStorage(fn, core)].some((id) => persistent.has(id))).map((fn) => fn.id));
5932
5987
  const globalNames = planGlobalNames(core, plan.programNames);
5988
+ const scanClock = planScanClock(core, plan.deployment.tasks, globalNames.names);
5933
5989
  return {
5934
5990
  ...globalNames,
5935
5991
  blocks: coreTables13(core).blocks,
5936
- numeric: new NumericLowering(globalNames.typeMap, (label) => globalNames.names.allocate(label)),
5992
+ scanClock,
5993
+ numeric: new NumericLowering(globalNames.typeMap, (label) => globalNames.names.allocate(label), scanClock?.now),
5937
5994
  programRoutines
5938
5995
  };
5939
5996
  }
@@ -6337,6 +6394,7 @@ function emissionContext(plan) {
6337
6394
  ...global.targetTypes.objects,
6338
6395
  ...globalLists.objects,
6339
6396
  ...ioList.objects,
6397
+ ...global.scanClock ? [emitScanClock(global.scanClock)] : [],
6340
6398
  ...network ? [network] : []
6341
6399
  ];
6342
6400
  const routineNames = allocateFunctionNames(plan, global);
@@ -6496,5 +6554,5 @@ export {
6496
6554
  publicationSelectors
6497
6555
  };
6498
6556
 
6499
- //# debugId=76D518B752F3DAED64756E2164756E21
6557
+ //# debugId=86B7ACE89A01116464756E2164756E21
6500
6558
  //# sourceMappingURL=codesys.js.map